Skip to content

Commit e534376

Browse files
authored
fix: Drawer default props (#370)
* fix: Drawer default props close ant-design/ant-design#39711 close ant-design/ant-design#39777 * test: add test case for defaut props
1 parent 571a092 commit e534376

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/Drawer.tsx

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,26 @@ export type Placement = 'left' | 'top' | 'right' | 'bottom';
1010
export interface DrawerProps
1111
extends Omit<DrawerPopupProps, 'prefixCls' | 'inline' | 'scrollLocker'> {
1212
prefixCls?: string;
13-
1413
open?: boolean;
1514
onClose?: (e: React.MouseEvent | React.KeyboardEvent) => void;
1615
destroyOnClose?: boolean;
1716
getContainer?: PortalProps['getContainer'];
1817
}
1918

20-
// Default Value.
21-
// Since spread with default value will make this all over components.
22-
// Let's maintain this in one place.
23-
const defaultProps = {
24-
open: false,
25-
prefixCls: 'rc-drawer',
26-
placement: 'right' as Placement,
27-
autoFocus: true,
28-
keyboard: true,
29-
width: 378,
30-
mask: true,
31-
maskClosable: true,
32-
};
33-
34-
const Drawer: React.FC<DrawerProps> = drawerProps => {
35-
const props = {
36-
...defaultProps,
37-
...drawerProps,
38-
};
19+
const Drawer: React.FC<DrawerProps> = props => {
3920
const {
40-
open,
21+
open = false,
22+
prefixCls = 'rc-drawer',
23+
placement = 'right' as Placement,
24+
autoFocus = true,
25+
keyboard = true,
26+
width = 378,
27+
mask = true,
28+
maskClosable = true,
4129
getContainer,
4230
forceRender,
43-
prefixCls,
4431
afterOpenChange,
4532
destroyOnClose,
46-
mask,
4733
} = props;
4834

4935
const [animatedVisible, setAnimatedVisible] = React.useState(false);
@@ -65,9 +51,17 @@ const Drawer: React.FC<DrawerProps> = drawerProps => {
6551
return null;
6652
}
6753

68-
const sharedDrawerProps = {
54+
const drawerPopupProps = {
6955
...props,
56+
open,
7057
prefixCls,
58+
placement,
59+
autoFocus,
60+
keyboard,
61+
width,
62+
mask,
63+
maskClosable,
64+
inline: getContainer === false,
7165
afterOpenChange: internalAfterOpenChange,
7266
};
7367

@@ -78,7 +72,7 @@ const Drawer: React.FC<DrawerProps> = drawerProps => {
7872
getContainer={getContainer}
7973
autoLock={mask && (open || animatedVisible)}
8074
>
81-
<DrawerPopup {...sharedDrawerProps} inline={getContainer === false} />
75+
<DrawerPopup {...drawerPopupProps} />
8276
</Portal>
8377
);
8478
};

tests/index.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ describe('rc-drawer-menu', () => {
4040
).toBeTruthy();
4141
});
4242

43+
it('default props should work', () => {
44+
const { container, unmount } = render(
45+
<Drawer
46+
open
47+
placement={undefined}
48+
width={undefined}
49+
getContainer={false}
50+
/>,
51+
);
52+
expect(container.querySelector('.rc-drawer-right')).toBeTruthy();
53+
expect(
54+
container.querySelector('.rc-drawer-content-wrapper').style.width,
55+
).toBe('378px');
56+
unmount();
57+
});
58+
4359
describe('push', () => {
4460
const placementList: {
4561
placement: DrawerProps['placement'];

0 commit comments

Comments
 (0)