-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modal): add modal component (#1400)
Co-authored-by: ZhaoChen <ittisennsinn@gmail.com>
- Loading branch information
1 parent
30a89a6
commit 74a0d9e
Showing
43 changed files
with
1,411 additions
and
562 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
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,121 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
import { useLocale, usePrefixCls } from '@gio-design/utils'; | ||
import RcDialog from 'rc-dialog'; | ||
import { CloseOutlined } from '@gio-design/icons'; | ||
import { ButtonProps } from '../../legacy/button'; | ||
import { IModalProps, ModalLocale } from './interface'; | ||
import ModalPrefixClsContext from './ModalContext'; | ||
import Title from './Title'; | ||
import Footer from './Footer'; | ||
import defaultLocale from './locales/zh-CN'; | ||
|
||
const Modal: React.FC<IModalProps> = ({ | ||
prefixCls: customPrefixCls, | ||
size = 'small', | ||
className, | ||
wrapClassName, | ||
useBack, | ||
title, | ||
additionalFooter, | ||
onBack, | ||
closeAfterOk, | ||
dropCloseButton, | ||
okText: customizeOKText, | ||
closeText: customizeCloseText, | ||
okButtonProps, | ||
closeButtonProps, | ||
onOk, | ||
onClose, | ||
pending, | ||
...restProps | ||
}: IModalProps) => { | ||
const prefix = usePrefixCls('modal', customPrefixCls); | ||
const locale = useLocale('Modal'); | ||
const { closeText, okText } = { | ||
...defaultLocale, | ||
...locale, | ||
} as ModalLocale; | ||
|
||
const modalCls = classnames(className, { | ||
[`${prefix}--small`]: size === 'small', | ||
[`${prefix}--middle`]: size === 'middle', | ||
[`${prefix}--full`]: size === 'full', | ||
}); | ||
const wrapperCls = classnames(wrapClassName, `${prefix}__wrapper`); | ||
const closeCls = classnames(`${prefix}__close`, { | ||
[`${prefix}__close--disabled`]: pending, | ||
}); | ||
|
||
const useOkBtn = !!onOk && typeof onOk === 'function'; | ||
let useFooter = useOkBtn || !dropCloseButton || !!additionalFooter; | ||
if ('footer' in restProps && (restProps.footer === false || restProps.footer === null)) { | ||
useFooter = false; | ||
} | ||
const okBtnProps: ButtonProps = { | ||
loading: pending, | ||
disabled: pending, | ||
...okButtonProps, | ||
}; | ||
const closeBtnProps: ButtonProps = { | ||
disabled: pending, | ||
...closeButtonProps, | ||
}; | ||
|
||
const handleOk = async (e: React.MouseEvent<HTMLElement>) => { | ||
if (onOk && typeof onOk === 'function') { | ||
e.persist(); | ||
try { | ||
await Promise.resolve(onOk(e)); | ||
if (closeAfterOk) { | ||
onClose?.(e); | ||
} | ||
} catch (error) { | ||
const err = error ?? 'onOk 执行 reject 或抛出错误。'; | ||
console.error(err); | ||
} | ||
} | ||
}; | ||
|
||
const handleClose = (e: React.SyntheticEvent<HTMLElement, Event>) => { | ||
if (!pending) { | ||
onClose?.(e as React.MouseEvent<HTMLElement, MouseEvent>); | ||
} | ||
}; | ||
return ( | ||
<ModalPrefixClsContext.Provider value={prefix}> | ||
<RcDialog | ||
keyboard | ||
{...restProps} | ||
maskClosable={!useFooter} | ||
onClose={handleClose} | ||
transitionName="zoom" | ||
maskTransitionName="fade" | ||
prefixCls={prefix} | ||
className={modalCls} | ||
wrapClassName={wrapperCls} | ||
closable={title !== false} | ||
closeIcon={<CloseOutlined className={closeCls} />} | ||
title={title !== false && <Title onBack={onBack} useBack={useBack} title={title} />} | ||
footer={ | ||
useFooter && ( | ||
<Footer | ||
okText={customizeOKText ?? okText} | ||
closeText={customizeCloseText ?? closeText} | ||
okButtonProps={okBtnProps} | ||
closeButtonProps={closeBtnProps} | ||
footer={restProps.footer} | ||
additionalFooter={additionalFooter} | ||
onOk={handleOk} | ||
onClose={handleClose} | ||
useOk={useOkBtn} | ||
useClose={!dropCloseButton} | ||
/> | ||
) | ||
} | ||
/> | ||
</ModalPrefixClsContext.Provider> | ||
); | ||
}; | ||
|
||
export default Modal; |
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
File renamed without changes.
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
src/modal/__test__/Steps.tsx → src/legacy/modal/__test__/Steps.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
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
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.
74a0d9e
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.
Successfully deployed to the following URLs: