Skip to content

fix: Drawer default props #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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