Skip to content
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

feat(modal): add antd modal method and useModal #552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ export { default as Image } from './image';
export { default as Input } from './input';
export { default as KeyEventListener } from './keyEventListener';
export { default as MarkdownRender } from './markdownRender';
export { default as Modal } from './modal';
export { default as Modal } from './modal/modal';
export { default as NotFound } from './notFound';
export { default as ProgressBar } from './progressBar';
export { default as ProgressLine } from './progressLine';
71 changes: 24 additions & 47 deletions src/modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
import React from 'react';
import { Alert, type AlertProps, Modal, type ModalProps } from 'antd';
import classNames from 'classnames';
import { omit } from 'lodash-es';
import { Modal as AntdModal } from 'antd';
import { ModalStaticFunctions } from 'antd/lib/modal/confirm';

import './index.scss';
import InternalModal from './modal';

export interface IModalProps extends ModalProps {
size?: 'small' | 'default' | 'middle' | 'large';
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
}
type ModalType = typeof InternalModal &
ModalStaticFunctions & {
useModal: typeof AntdModal.useModal;
destroyAll: () => void;
config: typeof AntdModal.config;
};

const getWidthFromSize = (size: IModalProps['size']) => {
if (size === 'small') return 400;
if (size === 'middle') return 640;
if (size === 'large') return 900;
return 520;
};
const { useModal, info, success, error, warning, confirm, destroyAll, config } = AntdModal;

const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
if (typeof banner === 'object') return React.isValidElement(banner);
return true;
};
const Modal = InternalModal as ModalType;

export default function InternalModal({
bodyStyle,
banner,
size = 'default',
children,
width,
className,
...rest
}: IModalProps) {
const finalWidth = width ?? getWidthFromSize(size);
Object.assign(Modal, {
useModal,
info,
success,
error,
warning,
confirm,
destroyAll,
config,
});

return (
<Modal
className={classNames('dtc-modal', className)}
bodyStyle={{ padding: 0, ...bodyStyle }}
width={finalWidth}
{...rest}
>
{banner && (
<Alert
message={isValidBanner(banner) ? banner : banner.message}
banner
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
/>
)}
<section className="dtc-modal-body">{children}</section>
</Modal>
);
}
export { IModalProps } from './modal';

export default Modal;
53 changes: 53 additions & 0 deletions src/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import { Alert, type AlertProps, Modal as AntdModal, type ModalProps } from 'antd';
import classNames from 'classnames';
import { omit } from 'lodash-es';

import './index.scss';

export interface IModalProps extends ModalProps {
size?: 'small' | 'default' | 'middle' | 'large';
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
}

const getWidthFromSize = (size: IModalProps['size']) => {
if (size === 'small') return 400;
if (size === 'middle') return 640;
if (size === 'large') return 900;
return 520;
};

const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
if (typeof banner === 'object') return React.isValidElement(banner);
return true;
};

export default function Modal({
bodyStyle,
banner,
size = 'default',
children,
width,
className,
...rest
}: IModalProps) {
const finalWidth = width ?? getWidthFromSize(size);

return (
<AntdModal
className={classNames('dtc-modal', className)}
bodyStyle={{ padding: 0, ...bodyStyle }}
width={finalWidth}
{...rest}
>
{banner && (
<Alert
message={isValidBanner(banner) ? banner : banner.message}
banner
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
/>
)}
<section className="dtc-modal-body">{children}</section>
</AntdModal>
);
}
Loading