Skip to content
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

feat: support drawerRender #471

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ storybook

# dumi
.dumi/tmp
.dumi/tmp-production
.dumi/tmp-production
pnpm-lock.yaml
8 changes: 8 additions & 0 deletions docs/demo/drawerRender.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: drawerRender
nav:
title: Demo
path: /demo
---

<code src="../examples/drawerRender.tsx"></code>
35 changes: 35 additions & 0 deletions docs/examples/drawerRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState } from 'react';
import Drawer from 'rc-drawer';
import motionProps from './motion';

const Demo = () => {
const [open, setOpen] = useState(false);
const onTouchEnd = () => {
setOpen(false);
};
const onSwitch = () => {
setOpen(c => !c);
};
return (
<div>
<Drawer
open={open}
onClose={onTouchEnd}
drawerRender={dom => (
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
<div id="test" style={{ height: '100%' }}>
{dom}
</div>
)}
// Motion
{...motionProps}
>
content
</Drawer>
<div>
<button onClick={onSwitch}>打开</button>
</div>
</div>
);
};
export default Demo;
33 changes: 19 additions & 14 deletions src/DrawerPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface DrawerPopupProps

// styles
styles?: DrawerStyles;
drawerRender?: (node: React.ReactNode) => React.ReactNode;
}

function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
Expand Down Expand Up @@ -121,6 +122,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
onKeyUp,

styles,
drawerRender,
} = props;

// ================================ Refs ================================
Expand Down Expand Up @@ -291,6 +293,22 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
leavedClassName={`${prefixCls}-content-wrapper-hidden`}
>
{({ className: motionClassName, style: motionStyle }, motionRef) => {
const content = (
<DrawerPanel
id={id}
containerRef={motionRef}
prefixCls={prefixCls}
className={classNames(className, drawerClassNames?.content)}
style={{
...style,
...styles?.content,
}}
{...pickAttrs(props, { aria: true })}
{...eventHandlers}
>
{children}
</DrawerPanel>
);
return (
<div
className={classNames(
Expand All @@ -305,20 +323,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
}}
{...pickAttrs(props, { data: true })}
>
<DrawerPanel
id={id}
containerRef={motionRef}
prefixCls={prefixCls}
className={classNames(className, drawerClassNames?.content)}
style={{
...style,
...styles?.content,
}}
{...pickAttrs(props, { aria: true })}
{...eventHandlers}
>
{children}
</DrawerPanel>
{drawerRender ? drawerRender(content) : content}
</div>
);
}}
Expand Down
7 changes: 7 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,11 @@ describe('rc-drawer-menu', () => {
);
unmount();
});
it('should support drawerRender', () => {
const { unmount } = render(
<Drawer drawerRender={dom => <div id="test">{dom}</div>} open />,
);
expect(document.querySelector('#test')).toBeTruthy();
unmount();
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"rc-drawer": ["src/index.ts"]
}
},
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
crazyair marked this conversation as resolved.
Show resolved Hide resolved
}