Skip to content

Commit 8c7e38c

Browse files
authored
chore: push support boolean (#279)
* chore: push support boolean * test: more test case
1 parent 842c755 commit 8c7e38c

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/DrawerPopup.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ import type { DrawerContextProps } from './context';
1010

1111
export type Placement = 'left' | 'right' | 'top' | 'bottom';
1212

13+
export interface PushConfig {
14+
distance?: number | string;
15+
}
16+
1317
export interface DrawerPopupProps {
1418
prefixCls: string;
1519
open?: boolean;
1620
inline?: boolean;
17-
push?: { distance?: number | string };
21+
push?: boolean | PushConfig;
1822
forceRender?: boolean;
1923
autoFocus?: boolean;
2024
keyboard?: boolean;
@@ -94,11 +98,23 @@ export default function DrawerPopup(props: DrawerPopupProps) {
9498
} = props;
9599

96100
// ============================ Push ============================
97-
const { distance } = push || {};
98101
const [pushed, setPushed] = React.useState(false);
99102

100103
const parentContext = React.useContext(DrawerContext);
101-
const pushDistance = distance ?? parentContext?.pushDistance ?? 180;
104+
105+
// Merge push distance
106+
let pushConfig: PushConfig;
107+
if (push === false) {
108+
pushConfig = {
109+
distance: 0,
110+
};
111+
} else if (push === true) {
112+
pushConfig = {};
113+
} else {
114+
pushConfig = push || {};
115+
}
116+
const pushDistance =
117+
pushConfig?.distance ?? parentContext?.pushDistance ?? 180;
102118

103119
const mergedContext = React.useMemo<DrawerContextProps>(
104120
() => ({

tests/index.spec.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ describe('rc-drawer-menu', () => {
9191
});
9292
});
9393
});
94+
95+
it('disable push', () => {
96+
const { container } = render(
97+
<Drawer push={false} open getContainer={false}>
98+
<Drawer open />
99+
</Drawer>,
100+
);
101+
102+
expect(container.querySelector('.rc-drawer-content-wrapper')).toHaveStyle(
103+
{
104+
transform: '',
105+
},
106+
);
107+
});
108+
109+
it('truthy', () => {
110+
const { container } = render(
111+
<Drawer push open getContainer={false}>
112+
<Drawer open />
113+
</Drawer>,
114+
);
115+
116+
expect(container.querySelector('.rc-drawer-content-wrapper')).toHaveStyle(
117+
{
118+
transform: 'translateX(-180px)',
119+
},
120+
);
121+
});
94122
});
95123

96124
describe('mask', () => {

0 commit comments

Comments
 (0)