Skip to content

Commit

Permalink
fix: Drawer default props (#370)
Browse files Browse the repository at this point in the history
* fix: Drawer default props

close ant-design/ant-design#39711
close ant-design/ant-design#39777

* test: add test case for defaut props
  • Loading branch information
afc163 committed Dec 24, 2022
1 parent 571a092 commit e534376
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,26 @@ export type Placement = 'left' | 'top' | 'right' | 'bottom';
export interface DrawerProps
extends Omit<DrawerPopupProps, 'prefixCls' | 'inline' | 'scrollLocker'> {
prefixCls?: string;

open?: boolean;
onClose?: (e: React.MouseEvent | React.KeyboardEvent) => void;
destroyOnClose?: boolean;
getContainer?: PortalProps['getContainer'];
}

// Default Value.
// Since spread with default value will make this all over components.
// Let's maintain this in one place.
const defaultProps = {
open: false,
prefixCls: 'rc-drawer',
placement: 'right' as Placement,
autoFocus: true,
keyboard: true,
width: 378,
mask: true,
maskClosable: true,
};

const Drawer: React.FC<DrawerProps> = drawerProps => {
const props = {
...defaultProps,
...drawerProps,
};
const Drawer: React.FC<DrawerProps> = props => {
const {
open,
open = false,
prefixCls = 'rc-drawer',
placement = 'right' as Placement,
autoFocus = true,
keyboard = true,
width = 378,
mask = true,
maskClosable = true,
getContainer,
forceRender,
prefixCls,
afterOpenChange,
destroyOnClose,
mask,
} = props;

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

const sharedDrawerProps = {
const drawerPopupProps = {
...props,
open,
prefixCls,
placement,
autoFocus,
keyboard,
width,
mask,
maskClosable,
inline: getContainer === false,
afterOpenChange: internalAfterOpenChange,
};

Expand All @@ -78,7 +72,7 @@ const Drawer: React.FC<DrawerProps> = drawerProps => {
getContainer={getContainer}
autoLock={mask && (open || animatedVisible)}
>
<DrawerPopup {...sharedDrawerProps} inline={getContainer === false} />
<DrawerPopup {...drawerPopupProps} />
</Portal>
);
};
Expand Down
16 changes: 16 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ describe('rc-drawer-menu', () => {
).toBeTruthy();
});

it('default props should work', () => {
const { container, unmount } = render(
<Drawer
open
placement={undefined}
width={undefined}
getContainer={false}
/>,
);
expect(container.querySelector('.rc-drawer-right')).toBeTruthy();
expect(
container.querySelector('.rc-drawer-content-wrapper').style.width,
).toBe('378px');
unmount();
});

describe('push', () => {
const placementList: {
placement: DrawerProps['placement'];
Expand Down

0 comments on commit e534376

Please sign in to comment.