Skip to content

Commit

Permalink
feat: support drawerRender
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed May 29, 2024
1 parent 469280c commit e6ae61c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
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>
40 changes: 40 additions & 0 deletions docs/examples/drawerRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* 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}
afterOpenChange={(c: boolean) => {
console.log('transitionEnd: ', c);
}}
placement="right"
// width={400}
width="60%"
// Motion
drawerRender={dom => (
<div id="test" style={{ height: '100%' }}>
{dom}
</div>
)}
>
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('#id')).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"]
}

0 comments on commit e6ae61c

Please sign in to comment.