Select.tsx
3.34 KB
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
import { useState, type ReactNode } from 'react';
import { Select as TDesignSelect } from 'tdesign-react';
import type {
SelectOption as TDesignSelectOption,
SelectValue,
} from 'tdesign-react/es/select/type';
export interface SelectOption {
label: ReactNode;
value: string;
disabled?: boolean;
}
interface SelectBaseProps {
options: SelectOption[];
placeholder?: string;
allowClear?: boolean;
disabled?: boolean;
className?: string;
'aria-label'?: string;
}
export interface SelectSingleProps extends SelectBaseProps {
multiple?: false;
value?: string;
onChange?: (value: string | undefined, option: SelectOption | undefined) => void;
}
export interface SelectMultipleProps extends SelectBaseProps {
multiple: true;
value?: string[];
onChange?: (value: string[], option: SelectOption | undefined) => void;
}
export type SelectProps = SelectSingleProps | SelectMultipleProps;
function toTDesignOptions(options: SelectOption[]): TDesignSelectOption[] {
return options.map((option) => ({
label: option.label,
value: option.value,
disabled: option.disabled,
}));
}
function normalizeSingleValue(value: SelectValue): string | undefined {
if (Array.isArray(value)) return typeof value[0] === 'string' ? value[0] : undefined;
return typeof value === 'string' ? value : undefined;
}
function normalizeMultipleValue(value: SelectValue): string[] {
if (!Array.isArray(value)) return typeof value === 'string' ? [value] : [];
return value.filter((item): item is string => typeof item === 'string');
}
export function Select(props: SelectProps) {
const {
allowClear,
className,
disabled,
options,
placeholder = '请选择',
multiple,
value,
onChange,
'aria-label': ariaLabel,
} = props;
const isControlled = Object.prototype.hasOwnProperty.call(props, 'value');
const [innerValue, setInnerValue] = useState<string | string[] | undefined>(multiple ? [] : undefined);
const currentValue = isControlled ? value : innerValue;
const selectedOption = !multiple && typeof currentValue === 'string'
? options.find((item) => item.value === currentValue)
: undefined;
return (
<TDesignSelect
aria-label={ariaLabel}
className={className}
clearable={allowClear}
disabled={disabled}
inputProps={{
'aria-disabled': disabled,
'aria-label': ariaLabel,
role: 'combobox',
} as never}
multiple={multiple}
options={toTDesignOptions(options)}
placeholder={placeholder}
value={currentValue}
valueDisplay={!multiple ? selectedOption?.label : undefined}
onChange={(nextValue, context) => {
const changedOption = context.option as { value?: unknown } | undefined;
const option = context.option
? options.find((item) => item.value === String(changedOption?.value))
: undefined;
if (multiple) {
const normalizedValue = normalizeMultipleValue(nextValue);
if (!isControlled) setInnerValue(normalizedValue);
(onChange as SelectMultipleProps['onChange'] | undefined)?.(normalizedValue, option);
return;
}
const normalizedValue = normalizeSingleValue(nextValue);
if (!isControlled) setInnerValue(normalizedValue);
(onChange as SelectSingleProps['onChange'] | undefined)?.(normalizedValue, option);
}}
/>
);
}