/
index.tsx
250 lines (227 loc) · 7.32 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
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 React, { useState, useEffect, useMemo } from 'react';
import Input from '@uiw/react-input';
import { IProps } from '@uiw/utils';
import Dropdown, { DropdownProps } from '@uiw/react-dropdown';
import Menu from '@uiw/react-menu';
import Icon from '@uiw/react-icon';
import './style/index.less';
type ValueType = Array<string | number>;
type OptionType = { value: string | number; label: React.ReactNode; children?: Array<OptionType> };
type SearchOptionType = { label: string; options: Array<OptionType> };
export interface CascaderProps extends IProps, DropdownProps {
option?: Array<OptionType>;
value?: ValueType;
onChange?: (isSeleted: boolean, value: ValueType, selectedOptions: Array<OptionType>) => void;
onSearch?: boolean | ((searchText: string) => void);
allowClear?: boolean;
placeholder?: string;
disabled?: boolean;
}
function Cascader(props: CascaderProps) {
const {
value,
onChange,
onSearch,
disabled,
allowClear,
placeholder,
prefixCls = 'w-cascader',
className,
style = { width: 200 },
option = [],
others,
} = props;
const cls = [prefixCls, className].filter(Boolean).join(' ').trim();
const [innerIsOpen, setInnerIsOpen] = useState(false);
const [selectedValue, setSelectedValue] = useState<Array<OptionType>>([]);
const [selectIconType, setSelectIconType] = useState('');
const [searchText, setSearchText] = useState<string>('');
const [searchOn, setSearchOn] = useState(false);
const [inputValue, setInputValue] = useState('');
const [searchOption, setSearchOption] = useState<Array<SearchOptionType>>();
useEffect(() => {
if (onSearch) {
const tempOptions: Array<SearchOptionType> = [];
iteratorOption(option, (opt) => {
const label = opt.map((m) => m.label).join(' / ');
tempOptions.push({ label, options: opt });
});
setSearchOption(tempOptions);
}
}, [onSearch]);
const iteratorOption = (
options: Array<OptionType>,
cb: (opt: Array<OptionType>) => void,
opts: Array<OptionType> = [],
) => {
options.map((opt) => {
const optsTemp = [...opts, opt];
if (opt.children) {
iteratorOption(opt.children, cb, optsTemp);
} else {
cb?.(optsTemp);
}
});
};
useEffect(() => {
if (value) {
const valueTemp: Array<OptionType> = [];
let optChildren = option;
value?.map((item) => {
const findOpt = optChildren.find((opt) => opt.value === item);
optChildren = findOpt?.children || [];
valueTemp.push({ label: item, value: item, ...findOpt });
});
setSelectedValue(valueTemp);
}
}, [value]);
function onVisibleChange(isOpen: boolean) {
setInnerIsOpen(isOpen);
}
function renderSelectIcon(type: string) {
let selectIconType;
if (type === 'enter' && allowClear && selectedValue.length > 0) {
selectIconType = 'close';
} else {
selectIconType = '';
}
setSelectIconType(selectIconType);
}
const searchItemClick = (options: Array<OptionType>) => {
setSearchText('');
setInnerIsOpen(false);
handelChange(false, options);
};
const handleItemClick = (optionsItem: OptionType, level: number) => {
selectedValue.splice(level, selectedValue.length - level, optionsItem);
handelChange(true, selectedValue);
};
const handelChange = (isSeleted: boolean, selectedValue: Array<OptionType>) => {
setSelectedValue([...selectedValue]);
const value = selectedValue.map((item) => item.value);
onChange?.(isSeleted, value, selectedValue);
};
const onClear = (e: React.MouseEvent<any, MouseEvent>) => {
e.stopPropagation();
handelChange(false, []);
};
const handelSearch = (searchText: string) => {
setSearchText(searchText);
};
const inputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!innerIsOpen) {
setInnerIsOpen(!innerIsOpen);
}
const value = e.target.value;
onSearch && handelSearch(value);
};
const widths = (style?.width as number) * 0.7 || undefined;
const OptionIter = (option: Array<OptionType>, level: number = 0) => {
if (!option) return;
return (
<Menu
key={level}
bordered
style={{
minHeight: 25,
minWidth: widths,
overflowY: 'scroll',
width: widths,
}}
>
{!option || option.length === 0 ? (
<div style={{ color: '#c7c7c7', fontSize: 12 }}>{'没有数据'}</div>
) : (
option.map((opt, index) => {
const active = selectedValue[level]?.value === opt.value;
return (
<Menu.Item
active={active}
key={index}
text={opt.label}
addonAfter={opt.children ? <Icon type="right" /> : undefined}
onClick={() => handleItemClick(opt, level)}
/>
);
})
)}
</Menu>
) as JSX.Element;
};
useEffect(() => {
const inputValue = selectedValue.map((opt) => opt.label).join(' / ');
setInputValue(inputValue);
}, [selectedValue]);
return (
<Dropdown
className={cls}
trigger="click"
style={{ marginTop: 5, ...style }}
overlayStyl={{ width: 100 }}
disabled={disabled}
{...others}
onVisibleChange={onVisibleChange}
isOpen={innerIsOpen}
menu={
!searchText ? (
<div style={{ display: 'flex' }}>
{new Array(selectedValue.length + 1)
.fill(0)
.map((_, index) => {
const options = index ? selectedValue[index - 1]?.children! : option;
return OptionIter(options, index);
})
.filter((m) => !!m)}
</div>
) : (
<Menu
bordered
style={{
minHeight: 25,
minWidth: style?.width,
overflowY: 'scroll',
width: style?.width,
}}
>
{!searchOption || searchOption.length === 0 ? (
<div style={{ color: '#c7c7c7', fontSize: 12 }}>{'没有数据'}</div>
) : (
searchOption
.filter((opt) => opt.label.includes(searchText.trim()))
.map((opt, index) => {
return (
<Menu.Item
key={index}
text={opt.label}
onClick={() => searchItemClick(opt.options)} //
/>
);
})
)}
</Menu>
)
}
>
<span onMouseLeave={() => renderSelectIcon('leave')} onMouseOver={() => renderSelectIcon('enter')}>
<Input
value={searchOn ? searchText : inputValue}
onChange={inputChange}
disabled={disabled}
placeholder={searchOn ? inputValue : placeholder}
style={{ width: style?.width }}
onFocus={() => onSearch && setSearchOn(true)}
onBlur={() => onSearch && setSearchOn(false)}
readOnly={!onSearch}
addonAfter={
<span style={{ width: 'auto' }}>
{!disabled && selectIconType === 'close' && (
<Icon type={selectIconType} onClick={onClear} className={`${prefixCls}-close`} />
)}
</span>
}
/>
</span>
</Dropdown>
);
}
export default Cascader;