-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): new dialog func to manage Modal
- Loading branch information
Showing
12 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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
25 changes: 25 additions & 0 deletions
25
packages/frontend/component/src/ui/dialog/dialog-center.tsx
This file contains 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,25 @@ | ||
import { useLiveData } from '@toeverything/infra'; | ||
|
||
import { Modal } from '../modal'; | ||
import { dialog } from './dialog'; | ||
import { dialogs$ } from './state'; | ||
import type { Dialog } from './types'; | ||
|
||
export const DialogCenter = () => { | ||
const dialogs = useLiveData(dialogs$); | ||
|
||
const onOpenChange = (info: Dialog, open: boolean) => { | ||
info.onOpenChange?.(open); | ||
if (!open) { | ||
dialog.close(info.id); | ||
} | ||
}; | ||
|
||
return Object.entries(dialogs).map(([id, info]) => { | ||
return ( | ||
<Modal key={id} onOpenChange={v => onOpenChange(info, v)} {...info}> | ||
{info.component} | ||
</Modal> | ||
); | ||
}); | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/frontend/component/src/ui/dialog/dialog.stories.tsx
This file contains 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,20 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { Button } from '../button'; | ||
import { dialog } from './dialog'; | ||
|
||
export default { | ||
title: 'UI/Dialog', | ||
}; | ||
|
||
const Template = (_: any) => { | ||
const openModal = useCallback(() => { | ||
dialog({ | ||
component: <div>Modal Content</div>, | ||
}); | ||
}, []); | ||
|
||
return <Button onClick={openModal}>Open Modal</Button>; | ||
}; | ||
|
||
export const Basic = Template.bind(undefined); |
This file contains 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 { dialogs$ } from './state'; | ||
import type { Dialog, DialogOptions } from './types'; | ||
|
||
let _internalId = 0; | ||
|
||
/** | ||
* Create/Open a dialog | ||
*/ | ||
export const dialog = (info: DialogOptions) => { | ||
const id = info.id ?? `${_internalId++}`; | ||
dialogs$.next({ [id]: { id, open: true, ...info } }); | ||
}; | ||
|
||
/** | ||
* Close specific dialog | ||
*/ | ||
dialog.close = (id: Dialog['id']) => { | ||
dialogs$.next({ | ||
...dialogs$.value, | ||
[id]: { ...dialogs$.value[id], open: false }, | ||
}); | ||
}; | ||
|
||
/** | ||
* Destroy specific dialog | ||
*/ | ||
dialog.destroy = (id: Dialog['id']) => { | ||
const { [id]: _, ...rest } = dialogs$.value; | ||
dialogs$.next(rest); | ||
}; | ||
|
||
// TODO: impl dialog.confirm |
This file contains 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,2 @@ | ||
export * from './dialog'; | ||
export * from './dialog-center'; |
This file contains 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,5 @@ | ||
import { LiveData } from '@toeverything/infra'; | ||
|
||
import type { Dialog } from './types'; | ||
|
||
export const dialogs$ = new LiveData<Record<Dialog['id'], Dialog>>({}); |
This file contains 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,17 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
import type { ModalProps } from '../modal'; | ||
|
||
export interface DialogOptions extends Omit<ModalProps, 'open'> { | ||
component: ReactNode; | ||
/** | ||
* Unique identifier for the dialog. | ||
* If specified, the dialog will be reused | ||
*/ | ||
id?: string; | ||
} | ||
|
||
export interface Dialog extends DialogOptions { | ||
id: string; | ||
open: boolean; | ||
} |
This file contains 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 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 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