Skip to content

feat(slidepane): support tab control #451

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 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 src/slidePane/demos/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default () => {
},
] as const
}
activeKey="changelog"
defaultKey="changelog"
onChange={(key) => console.log('currentKey', key)}
>
{(key) => {
switch (key) {
Expand Down
44 changes: 44 additions & 0 deletions src/slidePane/demos/tabsControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useState } from 'react';
import { Button } from 'antd';
import { SlidePane } from 'dt-react-component';

export default () => {
const [visible, setVisible] = useState(false);
const [activeKey, setActiveKey] = useState<'basicInfo' | 'changelog'>('changelog');

return (
<>
<Button onClick={() => setVisible(true)}>打开</Button>
<SlidePane
open={visible}
onClose={() => setVisible(false)}
title={'Title'}
tabs={
[
{
key: 'basicInfo',
title: '基本信息',
},
{
key: 'changelog',
title: '变更记录',
},
] as const
}
activeKey={activeKey}
onChange={(key) => setActiveKey(key)}
>
{(key) => {
switch (key) {
case 'basicInfo':
return <div>基本信息</div>;
case 'changelog':
return <div>变更记录</div>;
default:
break;
}
}}
</SlidePane>
</>
);
};
25 changes: 14 additions & 11 deletions src/slidePane/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ demo:
<code src="./demos/basic_top.tsx" title="抽屉距顶部高度"></code>
<code src="./demos/customTitle.tsx" title="自定义 Title"></code>
<code src="./demos/tabs.tsx" title="展示 tabs"></code>
<code src="./demos/tabsControl.tsx" title="展示 tabs 且 tabs 受控"></code>
<code src="./demos/footer.tsx" title="展示 footer"></code>
<code src="./demos/basicBanner.tsx" title="支持 banner"></code>
<code src="./demos/basicBannerProps.tsx" title="支持传 banner 的 Props 属性"></code>
Expand All @@ -31,17 +32,19 @@ demo:

[AlertProps](https://4x-ant-design.antgroup.com/components/alert-cn/#API)

| 参数 | 说明 | 类型 | 默认值 |
| ------------- | ------------------------------ | ----------------------------------------------------- | --------- |
| activeKeys | 右侧面板的内容的 Tabs 的选中项 | `string` | - |
| banner | 提示 | `React.ReactNode \| AlertProps` | - |
| bodyClassName | 内容容器的类名 | `string` | - |
| bodyStyle | 内容容器的样式 | `CSSProperties` | - |
| children | 右侧面板展示内容 | `(key: string) => React.ReactNode \| React.ReactNode` | - |
| footer | 右侧面板的底部内容 | `React.ReactNode` | - |
| size | 尺寸 | `'small' \| 'default' \| 'large'` | `default` |
| tabs | 右侧面板的内容的 Tabs | `{ key: string; title: React.ReactNode }[]` | - |
| title | 右侧面板的 title | `React.ReactNode` | - |
| 参数 | 说明 | 类型 | 默认值 |
| ------------- | ---------------------------------- | ----------------------------------------------------- | --------- |
| activeKey | 右侧面板的内容的 Tabs 的选中项 | `string` | - |
| banner | 提示 | `React.ReactNode \| AlertProps` | - |
| bodyClassName | 内容容器的类名 | `string` | - |
| bodyStyle | 内容容器的样式 | `CSSProperties` | - |
| children | 右侧面板展示内容 | `(key: string) => React.ReactNode \| React.ReactNode` | - |
| defaultKey | 右侧面板的内容的 Tabs 的默认选中项 | `string` | - |
| footer | 右侧面板的底部内容 | `React.ReactNode` | - |
| size | 尺寸 | `small \| default \| large` | `default` |
| tabs | 右侧面板的内容的 Tabs | `{ key: string; title: React.ReactNode }[]` | - |
| title | 右侧面板的 title | `React.ReactNode` | - |
| onChange | 右侧面板的 Tabs 切换回调 | `(key: string) => void` | - |

:::info
其余属性继承 [antd4.x 的 Drawer](https://4x.ant.design/components/drawer-cn/#API)
Expand Down
29 changes: 23 additions & 6 deletions src/slidePane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ interface NormalSlidePane extends Omit<DrawerProps, 'placement'> {

interface TabsSlidePane<T extends readOnlyTab> extends Omit<NormalSlidePane, 'children'> {
tabs?: T;
defaultKey?: TabKey<T>;
activeKey?: TabKey<T>;
children?: (key: TabKey<T>) => React.ReactNode;
onChange?: (key: TabKey<T>) => void;
}

function isFunction(props: any): props is TabsSlidePane<Tab[]> {
return typeof props.children === 'function';
}

function isTabMode(props: any): props is TabsSlidePane<Tab[]> {
return typeof props.tabs !== 'undefined';
}

function isControlled(props: any): props is TabsSlidePane<Tab[]> {
return props.activeKey !== undefined;
}

export type SlidePaneProps<T extends readOnlyTab> = TabsSlidePane<T> | NormalSlidePane;

const getWidthFromSize = (size: NormalSlidePane['size']) => {
Expand Down Expand Up @@ -74,14 +84,16 @@ const SlidePane = <T extends readOnlyTab>(props: SlidePaneProps<T>) => {
const composeOpen = open || visible;
const finalWidth = width ?? getWidthFromSize(size);

const [tabKey, setTabKey] = useState('');
const [internalTabKey, setInternalTabKey] = useState('');

useEffect(() => {
composeOpen &&
isFunction(props) &&
setTabKey(props.activeKey || props.tabs?.[0]?.key || '');
isTabMode(props) &&
setInternalTabKey(props.defaultKey ?? props.tabs?.[0]?.key ?? '');
}, [composeOpen]);

const currentKey = isControlled(props) ? props.activeKey : internalTabKey;

const renderButton = () => {
return (
<img
Expand All @@ -93,6 +105,11 @@ const SlidePane = <T extends readOnlyTab>(props: SlidePaneProps<T>) => {
);
};

const handleChangeKey = (key: TabKey<T>) => {
!isControlled(props) && setInternalTabKey(key);
isTabMode(props) && props.onChange?.(key);
};

return (
<RcDrawer
open={composeOpen}
Expand All @@ -117,8 +134,8 @@ const SlidePane = <T extends readOnlyTab>(props: SlidePaneProps<T>) => {
{isFunction(props) && (
<Tabs
destroyInactiveTabPane
activeKey={tabKey}
onChange={setTabKey}
activeKey={currentKey}
onChange={handleChangeKey}
className={`${slidePrefixCls}-tabs`}
>
{props.tabs?.map((tab: { key: string; title: React.ReactNode }) => (
Expand All @@ -130,7 +147,7 @@ const SlidePane = <T extends readOnlyTab>(props: SlidePaneProps<T>) => {
className={classNames(`${slidePrefixCls}-body`, bodyClassName)}
style={bodyStyle}
>
{typeof children === 'function' ? children(tabKey) : children}
{typeof children === 'function' ? children(currentKey ?? '') : children}
</div>
{footer ? (
<div className={classNames(`${slidePrefixCls}-footer`)}>{footer}</div>
Expand Down