-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.tsx
184 lines (161 loc) · 6.62 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React, { CSSProperties, useEffect, useState } from 'react';
import { CloseOutlined } from '@ant-design/icons';
import { Alert, AlertProps, Spin, Tabs } from 'antd';
import classNames from 'classnames';
import { omit } from 'lodash-es';
import RcDrawer, { DrawerProps as AntdDrawerProps } from 'rc-drawer';
import motionProps from './motion';
import './style.scss';
interface Tab {
readonly key: string;
readonly title: React.ReactNode;
}
type readOnlyTab = readonly Tab[];
type TabKey<T extends readOnlyTab> = T[number]['key'];
export enum DrawerType {
Form = 'form',
Normal = 'normal',
}
enum DrawerSize {
Small = 720,
Default = 1000,
Large = 1256,
}
interface BaseDrawerProps extends Omit<AntdDrawerProps, 'placement' | 'children'> {
size?: 'small' | 'default' | 'large';
loading?: boolean;
bodyClassName?: string;
title?: React.ReactNode;
bodyStyle?: CSSProperties;
footer?: React.ReactNode;
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
type?: DrawerType;
}
export interface DrawerProps<T extends readOnlyTab = any> extends BaseDrawerProps {
tabs?: T;
defaultKey?: TabKey<T>;
activeKey?: TabKey<T>;
children?: React.ReactNode | ((key: TabKey<T>) => React.ReactNode);
onChange?: (key: TabKey<T>) => void;
}
function isFunction<T extends readOnlyTab>(
props: DrawerProps<T>
): props is DrawerProps<T> & { children: (key: string) => React.ReactNode } {
return typeof props.children === 'function';
}
function isTabMode<T extends readOnlyTab>(props: DrawerProps<T>): props is DrawerProps<T> {
return 'tabs' in props;
}
function isControlled<T extends readOnlyTab>(props: DrawerProps<T>): props is DrawerProps<T> {
return 'activeKey' in props;
}
const getWidthFromSize = (size: DrawerProps['size']) => {
if (size === 'small') return `${(DrawerSize.Small / 1440) * 100}%`;
if (size === 'large') return `${(DrawerSize.Large / 1440) * 100}%`;
return `${(DrawerSize.Default / 1440) * 100}%`;
};
const isValidBanner = (banner: DrawerProps['banner']): banner is AlertProps['message'] => {
if (typeof banner === 'object') return React.isValidElement(banner);
return true;
};
const Drawer = <T extends readOnlyTab>(props: DrawerProps<T>) => {
const drawerPrefixCls = 'dtc-drawer';
const {
open,
loading = false,
bodyClassName,
mask = false,
maskClosable = true,
bodyStyle,
title,
width,
type = DrawerType.Normal,
size = 'default',
footer,
banner,
onClose,
...rest
} = props;
const finalWidth = width ?? getWidthFromSize(size);
const isFormType = type === DrawerType.Form;
const [internalTabKey, setInternalTabKey] = useState<TabKey<T>>('');
useEffect(() => {
open &&
isTabMode(props) &&
setInternalTabKey(props.defaultKey ?? props.tabs?.[0]?.key ?? '');
}, [open]);
const currentKey = isControlled(props) ? props.activeKey : internalTabKey;
const renderButton = () => {
return (
<img
className={`${drawerPrefixCls}-icon`}
onClick={onClose}
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAABwCAYAAAAE0LDPAAAAAXNSR0IArs4c6QAAAnlJREFUaEPtmk9IVHEQx78jufpw7ykouuEaBG6dEmrXOmRRt476VgvyEngKlNqQMu3PoSU7iCBq4CW2olNdRMRr7HasnnZb0y57WGODzUDjt/RIonbeyzfQYd51h/m8+c7395u3MOSs5Xexr4dKRLt5AhYJtbPRaNO7velo/4Bf6YhoB8B0R3vzNSLaNr8ECnBRRLTc0d583kBEAJU3J5o6HG0ZkgTs1OBATAxgqqghTIoCiPBeFABQSRggZFOxg/anC0ElYq9JlUglYhVgA9RFKhGrABugLlKJWAXYAHWRSsQqwAaoi1QiVgE2gHVRejKDUKgWQ1cvmj/XbMLfA3jA42dwVvPo7+tBd/xo8ICNzQLG7y3AskIYvz2IcEO9LwhbgcmWebGCpeUcEvEYBvrOBg8ol7cxOjaP4lYJqZEkIm2NniGeKjDZ3mQ/YPbJa7S1NiI1YntuuGeAgTx8lMHqx3XYvWdwOnHMUxW+AJufC7hzdwH1VggTt64gHLZYiC+Ayfb85QoWl3KIn+jEpeS54AHlb98xOjaHYrGEG8M2DkWaqkJ8V2CyZd86mJl7hdaWg7h5PVm14f8EMJD0zxM+ePkCuo4f+WsV/ycgm3MwMy8k0d4mp4ZtRIJusmvTxMlODNgB21T8oLlXRbK3B6cS3maDZxeJXnbudb315Wvl9AZ+XbsDpzseQ3/QA+fTRgET9wVHpvmqcNbWK6PSjEy/D9vkB+mnaLDq5D5b/L6x7+8iBbBNVolUItYDKpFKxCrABqiLVCJWATZAXaQSsQqwAeoilYhVgA1QFzESCS8wia9giS6Rma1B0TU40UU+sVVEoWXK6uugPwBjVkdxauLcgwAAAABJRU5ErkJggg=="
alt="closeBtn"
/>
);
};
const handleChangeKey = (key: TabKey<T>) => {
!isControlled(props) && setInternalTabKey(key);
isTabMode(props) && props.onChange?.(key);
};
return (
<RcDrawer
open={open}
placement="right"
mask={isFormType ? true : mask}
maskClosable={isFormType ? false : maskClosable}
width={finalWidth}
prefixCls={drawerPrefixCls}
onClose={onClose}
{...rest}
{...motionProps}
>
{!isFormType && renderButton()}
<Spin wrapperClassName={`${drawerPrefixCls}-nested-loading`} spinning={loading}>
{title && (
<div className={`${drawerPrefixCls}-header`}>
{title}
{isFormType && (
<CloseOutlined
className={`${drawerPrefixCls}-header--icon`}
onClick={onClose}
/>
)}
</div>
)}
{banner && (
<Alert
message={isValidBanner(banner) ? banner : (banner as any).message}
banner
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
/>
)}
{isTabMode(props) && (
<Tabs
destroyInactiveTabPane
activeKey={currentKey}
onChange={handleChangeKey}
className={`${drawerPrefixCls}-tabs`}
>
{props.tabs?.map((tab: { key: string; title: React.ReactNode }) => (
<Tabs.TabPane tab={tab.title} key={tab.key} />
))}
</Tabs>
)}
<div
className={classNames(`${drawerPrefixCls}-body`, bodyClassName)}
style={bodyStyle}
>
{isFunction(props)
? props.children?.(currentKey ?? '')
: (props.children as React.ReactNode)}
</div>
{footer ? (
<div className={classNames(`${drawerPrefixCls}-footer`)}>{footer}</div>
) : null}
</Spin>
</RcDrawer>
);
};
export default Drawer;