import * as React from 'react'; import RcDrawer from 'rc-drawer-menu'; import { isNull, isNumber } from 'util'; type EventType = | React.MouseEvent | React.MouseEvent; export interface IDrawerProps { closable?: boolean; // @todo 下一步增加 destroyOnClose?: boolean; getContainer?: HTMLElement; maskClosable?: boolean; mask?: boolean; maskStyle?: React.CSSProperties; style?: React.CSSProperties; title?: React.ReactNode; visible?: boolean; width?: number | string; wrapClassName?: string; // @todo 下一步增加 zIndex?: number; prefixCls?: string; placement?: 'left' | 'right'; onClose?: (e: EventType) => void; } export interface IDrawerState { visible?: boolean; } export default class Drawer extends React.Component< IDrawerProps, IDrawerState > { static defaultProps = { prefixCls: 'ant-drawer', width: 325, closable: true, }; static getDerivedStateFromProps( nextProps: IDrawerProps, prevState: IDrawerState, ) { const nextState: IDrawerState = {}; if (!isNull(nextProps.visible) && nextProps.visible !== prevState.visible) { nextState.visible = nextProps.visible; } return nextState; } constructor(props: IDrawerProps) { super(props); this.state = { visible: false, }; } close = (e: EventType) => { if (!isNull(this.props.visible)) { if (this.props.onClose) { this.props.onClose(e); } return; } this.setState({ visible: false, }); } onMaskClick = (e: EventType) => { if (!this.props.maskClosable) { return; } this.close(e); } renderBody = () => { const { prefixCls, title, closable } = this.props; let header; if (title) { header = (
{title}
); } let closer; if (closable) { closer = ( ); } const containerStyle = { width: this.props.width }; return (
{header} {closer}
{this.props.children}
); } render() { let { width } = this.props; if (isNumber(width)) { width = `${width}px`; } return ( {this.renderBody()} ); } }