-
Notifications
You must be signed in to change notification settings - Fork 42
/
Button.tsx
142 lines (127 loc) · 4.12 KB
/
Button.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
import type { DSize } from '../../utils/types';
import React from 'react';
import { LoadingOutlined } from '@react-devui/icons';
import { checkNodeExist, getClassName } from '@react-devui/utils';
import { useWave, useGeneralContext } from '../../hooks';
import { registerComponentMate, TTANSITION_DURING_SLOW } from '../../utils';
import { DBaseDesign } from '../_base-design';
import { DCollapseTransition } from '../_transition';
import { useComponentConfig, usePrefixConfig } from '../root';
export interface DButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
dType?: 'primary' | 'secondary' | 'outline' | 'dashed' | 'text' | 'link';
dTheme?: 'primary' | 'success' | 'warning' | 'danger';
// TODO: Should add `aria-busy` for loading state?
dLoading?: boolean;
dBlock?: boolean;
dVariant?: 'circle' | 'round';
dSize?: DSize;
dIcon?: React.ReactNode;
dIconRight?: boolean;
}
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DButton' as const });
function Button(props: DButtonProps, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element | null {
const {
children,
dType = 'primary',
dTheme = 'primary',
dLoading = false,
dBlock = false,
dVariant,
dSize,
dIcon,
dIconRight = false,
...restProps
} = useComponentConfig(COMPONENT_NAME, props);
//#region Context
const dPrefix = usePrefixConfig();
const { gSize, gDisabled } = useGeneralContext();
//#endregion
const [waveNode, wave] = useWave();
const size = dSize ?? gSize;
const disabled = restProps.disabled || dLoading || gDisabled;
const buttonIcon = (loading: boolean, iconRef?: React.RefObject<HTMLDivElement>, style?: React.CSSProperties) => (
<div
ref={iconRef}
className={getClassName(`${dPrefix}button__icon`, {
[`${dPrefix}button__icon--right`]: dIconRight,
})}
style={style}
>
{loading ? <LoadingOutlined dSpin /> : dIcon}
</div>
);
const iconNode = checkNodeExist(dIcon) ? (
buttonIcon(dLoading)
) : (
<DCollapseTransition
dOriginalSize={{
width: '',
}}
dCollapsedStyle={{
width: 0,
}}
dIn={dLoading}
dDuring={TTANSITION_DURING_SLOW}
dHorizontal
dStyles={{
entering: {
transition: ['width', 'padding', 'margin'].map((attr) => `${attr} ${TTANSITION_DURING_SLOW}ms linear`).join(', '),
},
leaving: {
transition: ['width', 'padding', 'margin'].map((attr) => `${attr} ${TTANSITION_DURING_SLOW}ms linear`).join(', '),
},
leaved: { display: 'none' },
}}
dDestroyWhenLeaved
>
{(collapseRef, collapseStyle) => buttonIcon(true, collapseRef, collapseStyle)}
</DCollapseTransition>
);
const contentNode = <div>{children}</div>;
return (
<DBaseDesign
dComposeDesign={{
disabled: disabled,
}}
dFormDesign={false}
>
{({ render: renderBaseDesign }) =>
renderBaseDesign(
<button
{...restProps}
ref={ref}
className={getClassName(restProps.className, `${dPrefix}button`, `${dPrefix}button--${dType}`, `t-${dTheme}`, {
[`${dPrefix}button--${dVariant}`]: dVariant,
[`${dPrefix}button--${size}`]: size,
[`${dPrefix}button--block`]: dBlock,
[`${dPrefix}button--icon`]: !children,
'is-loading': dLoading,
})}
type={restProps.type ?? 'button'}
disabled={disabled}
onClick={(e) => {
restProps.onClick?.(e);
if (dType === 'primary' || dType === 'secondary' || dType === 'outline' || dType === 'dashed') {
wave(`var(--${dPrefix}color-${dTheme})`);
}
}}
>
{dIconRight ? (
<>
{contentNode}
{iconNode}
</>
) : (
<>
{iconNode}
{contentNode}
</>
)}
{waveNode}
</button>
)
}
</DBaseDesign>
);
}
export const DButton = React.forwardRef(Button);