Skip to content

Commit dba1423

Browse files
committed
feat: test
1 parent 672d7de commit dba1423

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/DrawerChild.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { IDrawerChildProps } from './IDrawerPropTypes';
99
import {
1010
addEventListener,
1111
dataToArray,
12+
getParent,
1213
getTouchParentScroll,
1314
isNumeric,
1415
removeEventListener,
@@ -96,7 +97,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
9697
this.passive = passiveSupported ? { passive: false } : false;
9798
}
9899
const { open, getContainer, showMask, autoFocus } = this.props;
99-
const container = getContainer && getContainer();
100+
const container = getParent(getContainer);
100101
this.drawerId = `drawer_id_${Number(
101102
(Date.now() + Math.random())
102103
.toString()
@@ -123,7 +124,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
123124
public componentDidUpdate(prevProps: IDrawerChildProps) {
124125
const { open, getContainer, scrollLocker, showMask, autoFocus } =
125126
this.props;
126-
const container = getContainer && getContainer();
127+
const container = getParent(getContainer);
127128
if (open !== prevProps.open) {
128129
if (container && container.parentNode === document.body) {
129130
currentDrawer[this.drawerId] = !!open;
@@ -296,7 +297,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
296297

297298
private toggleScrollingToDrawerAndBody = (right: number) => {
298299
const { getContainer, showMask, open } = this.props;
299-
const container = getContainer && getContainer();
300+
const container = getParent(getContainer);
300301
// 处理 body 滚动
301302
if (container && container.parentNode === document.body && showMask) {
302303
const eventArray = ['touchstart'];
@@ -428,7 +429,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
428429
if (windowIsUndefined) {
429430
return;
430431
}
431-
const container = getContainer && getContainer();
432+
const container = getParent(getContainer);
432433
const parent = container ? (container.parentNode as HTMLElement) : null;
433434
this.levelDom = [];
434435
if (level === 'all') {

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,17 @@ export const getTouchParentScroll = (
123123
}
124124
return false;
125125
};
126+
127+
type GetContainer = string | HTMLElement | (() => HTMLElement) | false | null;
128+
129+
export function getParent(getContainer?: GetContainer) {
130+
const container =
131+
typeof getContainer === 'function' ? getContainer() : getContainer;
132+
133+
const element =
134+
typeof container === 'string'
135+
? document.querySelector(container)
136+
: container;
137+
138+
return element;
139+
}

0 commit comments

Comments
 (0)