-
-
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): dialog.confirm func
- Loading branch information
Showing
8 changed files
with
232 additions
and
43 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/frontend/component/src/ui/dialog/confirm-dialog.css.ts
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,30 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const confirmModalContent = style({ | ||
marginTop: '12px', | ||
marginBottom: '20px', | ||
height: '100%', | ||
overflowY: 'auto', | ||
padding: '0 4px', | ||
}); | ||
export const confirmModalContainer = style({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
}); | ||
export const modalFooter = style({ | ||
display: 'flex', | ||
justifyContent: 'flex-end', | ||
alignItems: 'center', | ||
paddingTop: '40px', | ||
marginTop: 'auto', | ||
gap: '20px', | ||
selectors: { | ||
'&.modalFooterWithChildren': { | ||
paddingTop: '20px', | ||
}, | ||
'&.reverse': { | ||
flexDirection: 'row-reverse', | ||
justifyContent: 'flex-start', | ||
}, | ||
}, | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/frontend/component/src/ui/dialog/confirm-dialog.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,45 @@ | ||
import { DialogTrigger } from '@radix-ui/react-dialog'; | ||
import clsx from 'clsx'; | ||
import { useCallback } from 'react'; | ||
|
||
import { Button } from '../button'; | ||
import type { ConfirmModalProps } from '../modal'; | ||
import * as styles from './confirm-dialog.css'; | ||
|
||
export const ConfirmModalInner = ({ | ||
children, | ||
confirmButtonOptions, | ||
// FIXME: we need i18n | ||
cancelText = 'Cancel', | ||
cancelButtonOptions, | ||
reverseFooter, | ||
onConfirm, | ||
onCancel, | ||
}: ConfirmModalProps) => { | ||
const onConfirmClick = useCallback(() => { | ||
Promise.resolve(onConfirm?.()).catch(err => { | ||
console.error(err); | ||
}); | ||
}, [onConfirm]); | ||
|
||
return ( | ||
<> | ||
{children ? ( | ||
<div className={styles.confirmModalContent}>{children}</div> | ||
) : null} | ||
<div | ||
className={clsx(styles.modalFooter, { | ||
modalFooterWithChildren: !!children, | ||
reverse: reverseFooter, | ||
})} | ||
> | ||
<DialogTrigger asChild> | ||
<Button onClick={onCancel} {...cancelButtonOptions}> | ||
{cancelText} | ||
</Button> | ||
</DialogTrigger> | ||
<Button onClick={onConfirmClick} {...confirmButtonOptions}></Button> | ||
</div> | ||
</> | ||
); | ||
}; |
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 was deleted.
Oops, something went wrong.
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,111 @@ | ||
import clsx from 'clsx'; | ||
|
||
import type { ConfirmModalProps, OpenConfirmModalOptions } from '../modal'; | ||
import { ConfirmModalInner } from './confirm-dialog'; | ||
import { confirmModalContainer } from './confirm-dialog.css'; | ||
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 ?? `internal:${_internalId++}`; | ||
dialogs$.next({ | ||
[id]: { | ||
...dialogs$.value[id], | ||
...info, | ||
id, | ||
open: true, | ||
}, | ||
}); | ||
return id; | ||
}; | ||
|
||
/** | ||
* Close specific dialog | ||
*/ | ||
dialog.close = (id: Dialog['id']) => { | ||
if (id.startsWith('internal:')) { | ||
return dialog.destroy(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); | ||
}; | ||
|
||
dialog.update = (id: Dialog['id'], info: Partial<DialogOptions>) => { | ||
const _dialog = dialogs$.value[id]; | ||
if (!_dialog) { | ||
throw new Error(`Dialog with id ${id} not found`); | ||
} | ||
dialogs$.next({ | ||
...dialogs$.value, | ||
[id]: { | ||
// TODO: merge deeply | ||
..._dialog, | ||
...info, | ||
}, | ||
}); | ||
}; | ||
|
||
/** | ||
* Open confirm dialog | ||
* @returns | ||
*/ | ||
dialog.confirm = ( | ||
props: ConfirmModalProps & { id?: Dialog['id'] }, | ||
options?: OpenConfirmModalOptions | ||
) => { | ||
const { autoClose = true, onSuccess } = options ?? {}; | ||
const { onConfirm: _onConfirm, width = 480, ...otherProps } = props; | ||
|
||
const setLoading = (value: boolean) => { | ||
dialog.confirm({ | ||
id, | ||
...props, | ||
confirmButtonOptions: { | ||
...props.confirmButtonOptions, | ||
loading: value, | ||
}, | ||
}); | ||
}; | ||
|
||
const onConfirm = () => { | ||
setLoading(true); | ||
return Promise.resolve(_onConfirm?.()) | ||
.then(() => onSuccess?.()) | ||
.catch(console.error) | ||
.finally(() => autoClose && dialog.close(id)); | ||
}; | ||
|
||
const id = dialog({ | ||
...props, | ||
width, | ||
contentOptions: { | ||
...props.contentOptions, | ||
className: clsx( | ||
confirmModalContainer, | ||
props.confirmButtonOptions?.className | ||
), | ||
}, | ||
component: ( | ||
<ConfirmModalInner onConfirm={onConfirm} {...otherProps}> | ||
{props.children} | ||
</ConfirmModalInner> | ||
), | ||
}); | ||
|
||
return id; | ||
}; |
1 change: 1 addition & 0 deletions
1
...rontend/component/src/ui/dialog/index.tsx → ...frontend/component/src/ui/dialog/index.ts
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './dialog'; | ||
export * from './dialog-center'; | ||
export * from './state'; |
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