2018-08-05 07:08:34 +08:00
|
|
|
|
import classNames from 'classnames';
|
2022-05-25 11:03:13 +08:00
|
|
|
|
import RcDrawer from 'rc-drawer';
|
2022-07-25 21:04:43 +08:00
|
|
|
|
import type { DrawerProps as RcDrawerProps } from 'rc-drawer';
|
|
|
|
|
import type { CSSMotionProps } from 'rc-motion';
|
2022-05-25 11:03:13 +08:00
|
|
|
|
import * as React from 'react';
|
2022-03-23 22:46:15 +08:00
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-06 23:39:00 +08:00
|
|
|
|
import { NoFormStyle } from '../form/context';
|
2022-07-25 21:04:43 +08:00
|
|
|
|
import { getTransitionName } from '../_util/motion';
|
2018-12-22 21:21:42 +08:00
|
|
|
|
import { tuple } from '../_util/type';
|
2022-08-23 16:22:00 +08:00
|
|
|
|
import warning from '../_util/warning';
|
2022-09-21 16:35:01 +08:00
|
|
|
|
import DrawerPanel from './DrawerPanel';
|
|
|
|
|
import type { DrawerPanelProps } from './DrawerPanel';
|
2018-07-02 21:49:06 +08:00
|
|
|
|
|
2022-03-24 23:33:51 +08:00
|
|
|
|
// CSSINJS
|
|
|
|
|
import useStyle from './style';
|
|
|
|
|
|
2021-06-11 08:39:17 +08:00
|
|
|
|
const SizeTypes = tuple('default', 'large');
|
|
|
|
|
type sizeType = typeof SizeTypes[number];
|
|
|
|
|
|
2020-07-12 18:47:14 +08:00
|
|
|
|
export interface PushState {
|
|
|
|
|
distance: string | number;
|
|
|
|
|
}
|
2022-08-01 17:06:19 +08:00
|
|
|
|
|
|
|
|
|
// Drawer diff props: 'open' | 'motion' | 'maskMotion' | 'wrapperClassName'
|
2022-09-21 16:35:01 +08:00
|
|
|
|
export interface DrawerProps extends RcDrawerProps, Omit<DrawerPanelProps, 'prefixCls'> {
|
2022-08-01 17:06:19 +08:00
|
|
|
|
size?: sizeType;
|
|
|
|
|
|
2022-08-23 16:22:00 +08:00
|
|
|
|
open?: boolean;
|
2022-08-01 17:06:19 +08:00
|
|
|
|
|
2022-08-23 16:22:00 +08:00
|
|
|
|
afterOpenChange?: (open: boolean) => void;
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
|
/** @deprecated Please use `open` instead */
|
|
|
|
|
visible?: boolean;
|
|
|
|
|
/** @deprecated Please use `afterOpenChange` instead */
|
|
|
|
|
afterVisibleChange?: (open: boolean) => void;
|
2020-10-24 14:47:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 18:47:14 +08:00
|
|
|
|
const defaultPushState: PushState = { distance: 180 };
|
2019-07-17 19:55:59 +08:00
|
|
|
|
|
2022-08-23 16:22:00 +08:00
|
|
|
|
function Drawer(props: DrawerProps) {
|
|
|
|
|
const {
|
|
|
|
|
rootClassName,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
size = 'default',
|
|
|
|
|
mask = true,
|
|
|
|
|
push = defaultPushState,
|
|
|
|
|
open,
|
2022-09-06 21:46:49 +08:00
|
|
|
|
afterOpenChange,
|
2022-08-23 16:22:00 +08:00
|
|
|
|
onClose,
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
getContainer: customizeGetContainer,
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
|
visible,
|
|
|
|
|
afterVisibleChange,
|
|
|
|
|
|
2022-08-23 16:22:00 +08:00
|
|
|
|
...rest
|
|
|
|
|
} = props;
|
|
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
|
const { getPopupContainer, getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
|
|
|
|
|
|
2022-07-29 15:56:11 +08:00
|
|
|
|
// Style
|
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
|
const getContainer =
|
|
|
|
|
// 有可能为 false,所以不能直接判断
|
|
|
|
|
customizeGetContainer === undefined && getPopupContainer
|
|
|
|
|
? () => getPopupContainer(document.body)
|
|
|
|
|
: customizeGetContainer;
|
|
|
|
|
|
|
|
|
|
const drawerClassName = classNames(
|
|
|
|
|
{
|
|
|
|
|
'no-mask': !mask,
|
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
|
},
|
2022-08-01 23:20:04 +08:00
|
|
|
|
rootClassName,
|
2022-07-29 15:56:11 +08:00
|
|
|
|
hashId,
|
2022-07-25 21:04:43 +08:00
|
|
|
|
);
|
|
|
|
|
|
2022-08-23 16:22:00 +08:00
|
|
|
|
// ========================== Warning ===========================
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
[
|
|
|
|
|
['visible', 'open'],
|
|
|
|
|
['afterVisibleChange', 'afterOpenChange'],
|
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
|
|
|
|
warning(
|
|
|
|
|
!(deprecatedName in props),
|
|
|
|
|
'Drawer',
|
2022-09-06 21:46:49 +08:00
|
|
|
|
`\`${deprecatedName}\` is deprecated, please use \`${newName}\` instead.`,
|
2022-08-23 16:22:00 +08:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 21:04:43 +08:00
|
|
|
|
// ============================ Size ============================
|
|
|
|
|
const mergedWidth = React.useMemo(() => width ?? (size === 'large' ? 736 : 378), [width, size]);
|
|
|
|
|
const mergedHeight = React.useMemo(
|
|
|
|
|
() => height ?? (size === 'large' ? 736 : 378),
|
|
|
|
|
[height, size],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// =========================== Motion ===========================
|
|
|
|
|
const maskMotion: CSSMotionProps = {
|
|
|
|
|
motionName: getTransitionName(prefixCls, 'mask-motion'),
|
|
|
|
|
motionAppear: true,
|
|
|
|
|
motionEnter: true,
|
|
|
|
|
motionLeave: true,
|
2022-08-17 11:16:01 +08:00
|
|
|
|
motionDeadline: 500,
|
2022-07-25 21:04:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const panelMotion: RcDrawerProps['motion'] = motionPlacement => ({
|
|
|
|
|
motionName: getTransitionName(prefixCls, `panel-motion-${motionPlacement}`),
|
|
|
|
|
motionAppear: true,
|
|
|
|
|
motionEnter: true,
|
|
|
|
|
motionLeave: true,
|
2022-08-17 11:16:01 +08:00
|
|
|
|
motionDeadline: 500,
|
2022-07-25 21:04:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// =========================== Render ===========================
|
2022-07-29 15:56:11 +08:00
|
|
|
|
return wrapSSR(
|
2022-07-25 21:04:43 +08:00
|
|
|
|
<NoFormStyle status override>
|
|
|
|
|
<RcDrawer
|
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
onClose={onClose}
|
2022-08-01 23:20:04 +08:00
|
|
|
|
maskMotion={maskMotion}
|
|
|
|
|
motion={panelMotion}
|
2022-07-25 21:04:43 +08:00
|
|
|
|
{...rest}
|
2022-09-06 21:46:49 +08:00
|
|
|
|
open={open ?? visible}
|
2022-07-25 21:04:43 +08:00
|
|
|
|
mask={mask}
|
|
|
|
|
push={push}
|
|
|
|
|
width={mergedWidth}
|
|
|
|
|
height={mergedHeight}
|
|
|
|
|
rootClassName={drawerClassName}
|
|
|
|
|
getContainer={getContainer}
|
2022-09-06 21:46:49 +08:00
|
|
|
|
afterOpenChange={afterOpenChange ?? afterVisibleChange}
|
2022-07-25 21:04:43 +08:00
|
|
|
|
>
|
2022-09-21 16:35:01 +08:00
|
|
|
|
<DrawerPanel prefixCls={prefixCls} {...rest} onClose={onClose} />
|
2022-07-25 21:04:43 +08:00
|
|
|
|
</RcDrawer>
|
2022-07-29 15:56:11 +08:00
|
|
|
|
</NoFormStyle>,
|
2022-07-25 21:04:43 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2020-10-24 14:47:44 +08:00
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
Drawer.displayName = 'Drawer';
|
|
|
|
|
}
|
2019-01-03 17:10:04 +08:00
|
|
|
|
|
2022-09-21 16:35:01 +08:00
|
|
|
|
function PurePanel({
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
style,
|
|
|
|
|
className,
|
2022-10-11 20:59:40 +08:00
|
|
|
|
placement = 'right',
|
2022-09-21 16:35:01 +08:00
|
|
|
|
...restProps
|
|
|
|
|
}: Omit<DrawerPanelProps, 'prefixCls' | 'drawerStyle'> & {
|
|
|
|
|
prefixCls?: string;
|
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
|
className?: string;
|
2022-10-11 20:59:40 +08:00
|
|
|
|
placement?: DrawerProps['placement'];
|
2022-09-21 16:35:01 +08:00
|
|
|
|
}) {
|
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
|
|
|
|
|
|
|
|
|
|
// Style
|
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
|
|
|
|
return wrapSSR(
|
2022-10-11 20:59:40 +08:00
|
|
|
|
<div
|
|
|
|
|
className={classNames(
|
|
|
|
|
prefixCls,
|
|
|
|
|
`${prefixCls}-pure`,
|
|
|
|
|
`${prefixCls}-${placement}`,
|
|
|
|
|
hashId,
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
style={style}
|
|
|
|
|
>
|
2022-09-21 16:35:01 +08:00
|
|
|
|
<DrawerPanel prefixCls={prefixCls} {...restProps} />
|
|
|
|
|
</div>,
|
2022-08-01 23:20:04 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Drawer._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
|
|
2022-03-23 22:46:15 +08:00
|
|
|
|
export default Drawer;
|