Drawer 的 overlay false 的 closeOnOutside 整理

This commit is contained in:
2betop 2020-05-09 14:01:30 +08:00
parent c89994e64a
commit 987d818bf2
1 changed files with 31 additions and 22 deletions

View File

@ -35,8 +35,6 @@ export interface DrawerProps {
classnames: ClassNamesFn; classnames: ClassNamesFn;
onExited?: () => void; onExited?: () => void;
onEntered?: () => void; onEntered?: () => void;
disableOnClickOutside: () => void;
enableOnClickOutside: () => void;
} }
export interface DrawerState {} export interface DrawerState {}
const fadeStyles: { const fadeStyles: {
@ -48,35 +46,34 @@ const fadeStyles: {
export class Drawer extends React.Component<DrawerProps, DrawerState> { export class Drawer extends React.Component<DrawerProps, DrawerState> {
static defaultProps: Pick< static defaultProps: Pick<
DrawerProps, DrawerProps,
| 'container' 'container' | 'position' | 'size' | 'overlay'
| 'position'
| 'size'
| 'overlay'
| 'disableOnClickOutside'
| 'enableOnClickOutside'
> = { > = {
container: document.body, container: document.body,
position: 'left', position: 'left',
size: 'md', size: 'md',
overlay: true, overlay: true
disableOnClickOutside: noop,
enableOnClickOutside: noop
}; };
contentDom: any; modalDom: HTMLElement;
contentDom: HTMLElement;
isRootClosed = false;
componentDidMount() { componentDidMount() {
if (this.props.show) { if (this.props.show) {
this.handleEntered(); this.handleEntered();
} }
document.body.addEventListener('click', this.handleRootClick, true);
document.body.addEventListener('click', this.handleRootClickCapture, true);
document.body.addEventListener('click', this.handleRootClick);
} }
componentWillUnmount() { componentWillUnmount() {
if (this.props.show) { if (this.props.show) {
this.handleExited(); this.handleExited();
} }
document.body.removeEventListener('click', this.handleRootClick, true);
document.body.removeEventListener('click', this.handleRootClick);
document.body.addEventListener('click', this.handleRootClickCapture, true);
} }
contentRef = (ref: any) => (this.contentDom = ref); contentRef = (ref: any) => (this.contentDom = ref);
@ -96,6 +93,7 @@ export class Drawer extends React.Component<DrawerProps, DrawerState> {
}; };
modalRef = (ref: any) => { modalRef = (ref: any) => {
this.modalDom = ref;
if (ref) { if (ref) {
addModal(this); addModal(this);
(ref as HTMLElement).classList.add( (ref as HTMLElement).classList.add(
@ -106,16 +104,27 @@ export class Drawer extends React.Component<DrawerProps, DrawerState> {
} }
}; };
@autobind
handleRootClickCapture(e: MouseEvent) {
const target = e.target as HTMLElement;
const closeOnOutside = this.props.closeOnOutside;
const isLeftButton =
(e.button === 1 && window.event !== null) || e.button === 0;
this.isRootClosed = !!(
isLeftButton &&
closeOnOutside &&
target &&
this.modalDom &&
!this.modalDom.contains(target)
);
}
@autobind @autobind
handleRootClick(e: MouseEvent) { handleRootClick(e: MouseEvent) {
const {classPrefix: ns, closeOnOutside, onHide, show} = this.props; const {onHide} = this.props;
if (
e.defaultPrevented || this.isRootClosed && !e.defaultPrevented && onHide(e);
(e.target as HTMLElement).closest(`.${ns}Drawer-content`)
) {
return;
}
closeOnOutside && show && onHide && onHide(e);
} }
render() { render() {