-
Notifications
You must be signed in to change notification settings - Fork 0
feat(DialogStack): add new component #421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
feerzlay
wants to merge
1
commit into
master
Choose a base branch
from
feature/dialog-stack-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/react/src/components/DialogStackV2/DialogStack.api.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Meta } from '@storybook/blocks'; | ||
| import LinkTo from '@storybook/addon-links/react'; | ||
| import { TableInterface } from '~storybook/components/TableInterface'; | ||
| import { TableFunction } from '~storybook/components/TableFunction'; | ||
|
|
||
| <Meta title="Components API/DialogStack" /> | ||
|
|
||
| # DialogStack API | ||
|
|
||
| ```js | ||
| import { DialogStack, useDialogStackV2 as useDialogStack } from '@esfront/react'; | ||
| ``` | ||
|
|
||
| ## Props | ||
|
|
||
| <TableInterface name="DialogStackProps" variant="props" /> | ||
|
|
||
| <br /> | ||
|
|
||
| ## `useDialogStack` | ||
|
|
||
| <TableFunction name="useDialogStackV2" /> | ||
|
|
||
| <TableInterface name="DialogStackContextValue" variant="props" /> | ||
|
|
||
| <br /> | ||
|
|
||
| ## Demos | ||
|
|
||
| <ul> | ||
| <li> | ||
| <LinkTo kind="components-Dialog" story="demo"> | ||
| <code>Dialog</code> | ||
| </LinkTo> | ||
| </li> | ||
| </ul> |
80 changes: 80 additions & 0 deletions
80
packages/react/src/components/DialogStackV2/DialogStack.state.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { ReactElement } from 'react'; | ||
|
|
||
| import { DialogProps } from '../Dialog'; | ||
|
|
||
| export type DialogStackComponentInterface = Pick<DialogProps, 'open' | 'BackdropProps' | 'TransitionProps'>; | ||
|
|
||
| export type DialogData = { | ||
| id: number | string; | ||
| open: boolean; | ||
| onExited: () => void; | ||
| component: ReactElement<DialogStackComponentInterface>; | ||
| }; | ||
|
|
||
| export class DialogStackState { | ||
| private _dialogs: DialogData[] = []; | ||
|
|
||
| private subscribers: Array<(data: DialogData[]) => void> = []; | ||
| private dialogId = 0; | ||
|
|
||
| public subscribe = (callback: (data: DialogData[]) => void) => { | ||
| this.subscribers.push(callback); | ||
|
|
||
| return () => { | ||
| this.subscribers = this.subscribers.filter((s) => s !== callback); | ||
| }; | ||
| }; | ||
|
|
||
| public get dialogs() { | ||
| return this._dialogs; | ||
| } | ||
|
|
||
| public set dialogs(value: DialogData[]) { | ||
| this._dialogs = value; | ||
|
|
||
| for (const s of this.subscribers) { | ||
| s(this._dialogs); | ||
| } | ||
| } | ||
|
|
||
| public closeDialogById = (id: number | string) => { | ||
| const index = this.dialogs.findIndex((e) => e.id === id); | ||
|
|
||
| if (index !== -1) { | ||
| const newValue = this.dialogs.slice(); | ||
| newValue[index].open = false; | ||
| this.dialogs = newValue; | ||
| } | ||
| }; | ||
|
|
||
| public open = ( | ||
| dialog: (props: { close: (data?: any) => void }) => ReactElement<DialogStackComponentInterface>, | ||
| params?: { id?: string } | ||
| ) => { | ||
| const dialogId = params?.id || this.dialogId++; | ||
| let close: (data?: any) => void; | ||
|
|
||
| const afterClosed = new Promise<any>((resolve) => { | ||
| close = (data?: any) => { | ||
| const index = this.dialogs.findIndex((e) => e.id === dialogId); | ||
|
|
||
| if (index !== -1 && this.dialogs[index].open) { | ||
| this.closeDialogById(dialogId); | ||
| resolve(data); | ||
| } | ||
| }; | ||
|
|
||
| const onExited = () => { | ||
| this.dialogs = this.dialogs.filter((dialog) => dialog.id !== dialogId); | ||
| }; | ||
|
|
||
| this.dialogs = [...this.dialogs, { id: dialogId, open: true, onExited, component: dialog({ close }) }]; | ||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-ignore | ||
| return { id: dialogId, close, afterClosed }; | ||
| }; | ||
| } | ||
|
|
||
| export const dialogStackState = new DialogStackState(); |
52 changes: 52 additions & 0 deletions
52
packages/react/src/components/DialogStackV2/DialogStack.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { cloneElement, Fragment, isValidElement, useEffect, useState } from 'react'; | ||
|
|
||
| import { DialogData, dialogStackState } from './DialogStack.state'; | ||
|
|
||
| export const DialogStack = () => { | ||
| const [dialogs, setDialogs] = useState<DialogData[]>([]); | ||
|
|
||
| useEffect(() => { | ||
| const unsubscribe = dialogStackState.subscribe(setDialogs); | ||
|
|
||
| return () => { | ||
| unsubscribe(); | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <> | ||
| {dialogs.map((dialog, index) => { | ||
| if (!isValidElement(dialog.component)) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Fragment key={dialog.id}> | ||
| {cloneElement(dialog.component, { | ||
| open: dialog.open, | ||
| BackdropProps: { | ||
| style: { | ||
| opacity: index < dialogs.filter((dialog) => dialog.open).length - 1 ? '0' : '', | ||
| ...dialog.component.props.BackdropProps?.style, | ||
| }, | ||
| ...dialog.component.props.BackdropProps, | ||
| }, | ||
| TransitionProps: { | ||
| ...dialog.component.props.TransitionProps, | ||
| onExited: (node: HTMLElement) => { | ||
| if (dialog.component.props.TransitionProps?.onExited) { | ||
| dialog.component.props.TransitionProps.onExited(node); | ||
| } | ||
|
|
||
| dialog.onExited(); | ||
| }, | ||
| }, | ||
| } as never)} | ||
| </Fragment> | ||
| ); | ||
| })} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| DialogStack.dialogId = 1; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { DialogStack } from './DialogStack'; | ||
| export type { DialogData, DialogStackComponentInterface } from './DialogStack.state'; | ||
| export { DialogStackState, dialogStackState } from './DialogStack.state'; | ||
| export { useDialogStackV2 } from './useDialogStack'; |
32 changes: 32 additions & 0 deletions
32
packages/react/src/components/DialogStackV2/useDialogStack.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { ReactElement, useEffect, useRef } from 'react'; | ||
|
|
||
| import { DialogStackComponentInterface, dialogStackState } from './DialogStack.state'; | ||
|
|
||
| export const useDialogStackV2 = () => { | ||
| const dialogs = useRef<Array<number | string>>([]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| dialogs.current.forEach((id) => { | ||
| dialogStackState.closeDialogById(id); | ||
| }); | ||
| }; | ||
| }, []); | ||
|
|
||
| return { | ||
| close: dialogStackState.closeDialogById, | ||
| open: ( | ||
| dialog: (props: { close: (data?: any) => void }) => ReactElement<DialogStackComponentInterface>, | ||
| params?: { id?: string } | ||
| ) => { | ||
| const result = dialogStackState.open(dialog, params); | ||
| dialogs.current.push(result.id); | ||
|
|
||
| result.afterClosed.then(() => { | ||
| dialogs.current = dialogs.current.filter((id) => id !== result.id); | ||
| }); | ||
|
|
||
| return result; | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.