-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
203 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
|
||
import { DialogProps } from '@/dialog/dialog'; | ||
import show from '@/dialog/show'; | ||
|
||
export type DialogAlertProps = Omit<DialogProps, 'visible' | 'closeOnAction' | 'actions'> & { | ||
confirmText?: React.ReactNode; | ||
onConfirm?: () => void; | ||
}; | ||
|
||
const alert = (props: DialogAlertProps) => { | ||
const { confirmText = '确认' } = props; | ||
|
||
return show({ | ||
...props, | ||
closeOnAction: true, | ||
actions: [ | ||
{ | ||
key: 'confirm', | ||
text: confirmText, | ||
color: 'primary', | ||
}, | ||
], | ||
onAction: props.onConfirm, | ||
onClose: () => { | ||
props.onClose?.(); | ||
}, | ||
}); | ||
}; | ||
|
||
export default alert; |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import InternalDialog from '@/dialog/dialog'; | ||
import alert from '@/dialog/alert'; | ||
|
||
export type { DialogProps } from '@/dialog/dialog'; | ||
export type { DialogAlertProps } from '@/dialog/alert'; | ||
|
||
type InternalDialogType = typeof InternalDialog; | ||
|
||
export interface DialogInterface extends InternalDialogType { | ||
alert: typeof alert; | ||
} | ||
|
||
const Dialog = InternalDialog as DialogInterface; | ||
|
||
Dialog.alert = alert; | ||
|
||
export default 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,12 @@ | ||
import React from 'react'; | ||
|
||
import Dialog, { DialogProps } from '@/dialog/dialog'; | ||
import renderImperatively from '@/utils/render-imperatively'; | ||
|
||
export type DialogShowProps = Omit<DialogProps, 'visible'>; | ||
|
||
function show(props: DialogShowProps) { | ||
const handler = renderImperatively(<Dialog {...props} />); | ||
} | ||
|
||
export default show; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import { renderToBody } from '@/utils/render'; | ||
|
||
export interface ElementProps { | ||
visible?: boolean; | ||
onClose?: () => void; | ||
afterClose?: () => void; | ||
} | ||
|
||
const renderImperatively = (element: React.ReactElement<ElementProps>) => { | ||
const Wraper = () => { | ||
const [visible, setVisible] = React.useState(false); | ||
|
||
const onClose = () => { | ||
setVisible(false); | ||
}; | ||
|
||
const afterClose = () => { | ||
unmount(); | ||
}; | ||
|
||
React.useEffect(() => { | ||
setVisible(true); | ||
}, []); | ||
|
||
return React.cloneElement(element, { ...element.props, visible, onClose, afterClose }); | ||
}; | ||
|
||
const unmount = renderToBody(<Wraper />); | ||
}; | ||
|
||
export default renderImperatively; |
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,23 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
|
||
export const render = (element: React.ReactElement, container: HTMLElement) => { | ||
const root = ReactDOM.createRoot(container); | ||
root.render(element); | ||
|
||
const unmount = () => { | ||
document.body.removeChild(container); | ||
root.unmount(); | ||
}; | ||
|
||
return unmount; | ||
}; | ||
|
||
export const renderToBody = (element: React.ReactElement) => { | ||
const container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
|
||
const unmount = render(element, container); | ||
|
||
return unmount; | ||
}; |
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