-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathTokenInput.tsx
250 lines (227 loc) · 7.07 KB
/
TokenInput.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { Button, Popover, Input, InputNumber } from 'ant-design-vue';
import classNames from 'ant-design-vue/es/_util/classNames';
import type { PropType } from 'vue';
import { defineComponent, toRefs, computed, ref, watch } from 'vue';
import { debounce } from 'lodash';
import ColorPanel from './ColorPanel';
import ColorPreview from './ColorPreview';
import type { MutableTheme } from './interface';
import { useInjectLocaleContext } from './locale';
import isColor from './utils/isColor';
import makeStyle from './utils/makeStyle';
const useStyle = makeStyle('TokenInput', token => ({
'.previewer-token-input': {
[`${token.rootCls}-input-group-addon, ${token.rootCls}-input-number-group-addon`]: {
border: '0 !important',
color: `rgba(0, 0, 0, 0.25) !important`,
fontSize: `${token.fontSizeSM}px !important`,
padding: '0 !important',
backgroundColor: 'transparent !important',
'&:first-child': {
paddingInlineStart: 0,
},
'&:last-child': {
paddingInlineEnd: 0,
},
},
[`${token.rootCls}-input-group-wrapper, ${token.rootCls}-input-number-group-wrapper`]: {
padding: 0,
height: 24,
width: '100%',
input: {
fontSize: token.fontSizeSM,
lineHeight: token.lineHeightSM,
padding: `2px ${token.paddingXS}px`,
height: 24,
},
},
[`${token.rootCls}-input-group-wrapper ${token.rootCls}-input, ${token.rootCls}-input-number-group-wrapper ${token.rootCls}-input-number`]:
{
background: 'white',
borderRadius: `${token.borderRadiusLG}px !important`,
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
},
'&&-light': {
[`${token.rootCls}-input-group-addon, ${token.rootCls}-input-number-group-addon`]: {
backgroundColor: token.colorBgContainer,
},
[`${token.rootCls}-input-group-wrapper ${token.rootCls}-input,
${token.rootCls}-input-number-group-wrapper ${token.rootCls}-input-number-input`]: {
background: token.colorFillAlter,
},
},
'&&-readonly': {
input: {
cursor: 'text',
color: token.colorText,
},
},
},
}));
export type TokenInputProps = {
theme?: MutableTheme;
value?: string | number;
onChange?: (value: string | number) => void;
light?: boolean;
readonly?: boolean;
onReset?: () => void;
canReset?: boolean;
hideTheme?: boolean;
};
const TokenInput = defineComponent({
name: 'TokenInput',
inheritAttrs: false,
props: {
theme: { type: Object as PropType<MutableTheme> },
value: { type: [String, Number] },
onChange: { type: Function as PropType<(value: string | number) => void> },
light: { type: Boolean },
readonly: { type: Boolean },
onReset: { type: Function as PropType<() => void> },
canReset: { type: Boolean },
hideTheme: { type: Boolean },
},
setup(props, { attrs }) {
const { value, theme, light, readonly, canReset: customCanReset, hideTheme } = toRefs(props);
const valueRef = ref<number | string>(value.value || '');
const tokenValue = ref<string | number>(value.value || '');
const canReset = computed(() => customCanReset.value ?? valueRef.value !== tokenValue.value);
const locale = useInjectLocaleContext();
const [wrapSSR, hashId] = useStyle();
watch(
value,
val => {
if (val !== undefined) {
tokenValue.value = val;
}
},
{ immediate: true },
);
const debouncedOnChange = debounce((newValue: number | string) => {
props.onChange?.(newValue);
}, 500);
const handleTokenChange = (newValue: number | string) => {
if (!readonly.value) {
tokenValue.value = newValue;
debouncedOnChange(newValue);
}
};
const handleReset = () => {
if (props.onReset) {
props.onReset();
} else {
handleTokenChange(valueRef.value);
}
};
return () => {
const addonAfter = !readonly.value && (
<span
style={{
display: 'flex',
alignItems: 'center',
minWidth: hideTheme.value ? '' : '80px',
}}
>
{canReset.value || hideTheme.value ? (
<Button
style={{
fontSize: '12px',
}}
onClick={handleReset}
type="link"
size="small"
disabled={!canReset.value}
>
{locale.value.reset}
</Button>
) : (
<span style={{ padding: '0 8px' }}>{theme.value?.name}</span>
)}
</span>
);
let inputNode;
if (typeof valueRef.value === 'string' && isColor(valueRef.value)) {
inputNode = (
<Input
bordered={false}
addonAfter={addonAfter}
value={String(tokenValue.value)}
disabled={readonly.value}
addonBefore={
<Popover
trigger="click"
placement="bottomRight"
arrow-point-at-center
overlayInnerStyle={{ padding: 0 }}
v-slots={{
content: () => (
<ColorPanel
alpha
color={String(tokenValue.value)}
style={{ border: 'none' }}
onChange={(v: string) => {
handleTokenChange(v);
}}
/>
),
}}
>
<ColorPreview
color={String(tokenValue.value)}
dark={theme.value?.key === 'dark'}
style={{
cursor: 'pointer',
marginInlineEnd: '8px',
verticalAlign: 'top',
}}
/>
</Popover>
}
onChange={e => {
handleTokenChange(e.target.value);
}}
/>
);
} else if (typeof valueRef.value === 'number') {
inputNode = (
<InputNumber
addonAfter={addonAfter}
bordered={false}
value={tokenValue.value}
disabled={readonly.value}
onChange={newValue => {
handleTokenChange(Number(newValue));
}}
/>
);
} else {
inputNode = (
<Input
addonAfter={addonAfter}
bordered={false}
value={String(tokenValue.value)}
disabled={readonly.value}
onChange={e => {
handleTokenChange(
typeof value.value === 'number' ? Number(e.target.value) : e.target.value,
);
}}
/>
);
}
return wrapSSR(
<div
{...attrs}
class={classNames('previewer-token-input', hashId.value, attrs.class, {
'previewer-token-input-light': light.value,
'previewer-token-input-readonly': readonly.value,
})}
>
{inputNode}
</div>,
);
};
},
});
export default TokenInput;