/
SearchTagInput.tsx
175 lines (157 loc) · 5.63 KB
/
SearchTagInput.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
import React, { useMemo, useState, useRef, useEffect, ReactElement } from 'react';
import Dropdown, { DropdownProps } from '@uiw/react-dropdown';
import Icon from '@uiw/react-icon';
import Input from '@uiw/react-input';
import Tag from '@uiw/react-tag';
import Card from '@uiw/react-card';
import Empty from '@uiw/react-empty';
import { IProps } from '@uiw/utils';
import './style/index.less';
export interface DropContent<V> {
values: Array<V>;
onSelected?: (selectedAll: Array<V>, selectd: V, isChecked: boolean) => void;
options?: Array<any>;
}
export interface SearchTagInputOption {
label: string;
key: string | number;
}
export interface SearchTagInputProps<V> extends IProps, DropdownProps, DropContent<V> {
allowClear?: boolean;
content: ReactElement<DropContent<V>>;
size?: 'large' | 'default' | 'small';
onChange: (selectedAll: Array<V>, selectd: V, isChecked: boolean) => void;
onSearch?: (seachValue: string) => void;
// mode?: 'single' | 'multiple';
loading?: boolean;
placeholder?: string;
emptyOption?: boolean | React.ReactNode;
}
function SearchTagInput<V extends SearchTagInputOption>(props: SearchTagInputProps<V>) {
const {
prefixCls = 'w-search-tree',
mode = 'single',
size = 'default',
disabled = false,
allowClear = false,
loading = false,
className,
style,
placeholder,
content,
options,
values,
onChange,
onSearch,
emptyOption,
...others
} = props;
const cls = [prefixCls, className].filter(Boolean).join(' ').trim();
// const isMultiple = useMemo(() => mode === 'multiple', [mode]);
const [innerIsOpen, setInnerIsOpen] = useState(false);
const [selectedOption, setSelectedOption] = useState<Array<V>>(values);
const optionRef = useRef<Array<V>>();
const [searchValue, searchValueSet] = useState<string>('');
optionRef.current = useMemo(() => selectedOption, [selectedOption]);
const [selectIconType, setSelectIconType] = useState('');
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
setSelectedOption(values);
}, [JSON.stringify(values)]);
function renderSelectIcon(type: string) {
const selectIconType = type === 'enter' && allowClear && (!!selectedOption.length || searchValue) ? 'close' : '';
setSelectIconType(selectIconType);
}
const handleSelectChange = (selectedAll: Array<V>, selectd?: V, isChecked: boolean = true) => {
setSelectedOption(selectedAll);
onChange && onChange(selectedAll, selectd!, isChecked);
};
const removeSelectItem = (index: number) => {
const selectedOption = optionRef.current as Array<V>;
const curreentItem = selectedOption[index];
selectedOption.splice(index, 1);
handleSelectChange([...selectedOption], curreentItem, false);
};
function handleInputChange(value: string) {
searchValueSet(value);
onSearch?.(value);
setSelectIconType(value ? 'loading' : '');
}
// 清除选中的值
function resetSelectedValue(e: any) {
e.stopPropagation();
setInnerIsOpen(false);
setSelectedOption([]);
handleInputChange('');
handleSelectChange([]);
}
function inputKeyDown(e: any) {
if (selectedOption.length > 0 && !searchValue && e.keyCode === 8) {
removeSelectItem(selectedOption.length - 1);
}
}
const newContent = useMemo(() => {
if (emptyOption) {
return typeof emptyOption === 'boolean' ? <Empty style={{ minWidth: 200, width: style?.width }} /> : emptyOption;
}
const newProps = {
...content.props,
onSelected: handleSelectChange,
values: selectedOption,
options,
};
return React.cloneElement(content as JSX.Element, newProps);
}, [selectedOption, options, emptyOption]);
return (
<Dropdown className={cls} trigger="focus" {...others} isOpen={innerIsOpen} menu={<Card>{newContent}</Card>}>
<div
onMouseOver={() => renderSelectIcon('enter')}
onMouseLeave={() => renderSelectIcon('leave')}
onClick={() => {
if (innerIsOpen) inputRef.current?.focus();
}}
style={{ minWidth: 200, maxWidth: 'none', ...style }}
>
<div className={`${prefixCls}-inner`}>
<div style={{ display: 'flex', flexFlow: 'wrap' }}>
{selectedOption.map((item, index) => {
return (
<Tag
style={{ height: 20, margin: 1, display: 'flex', alignItems: 'center' }}
className={`${prefixCls}-tag`}
key={index}
closable
color="#393E48"
onClose={(e) => {
e.stopPropagation();
removeSelectItem(index);
}}
>
{item.label}
</Tag>
);
})}
<Input
ref={inputRef}
style={{ flex: 1, minWidth: 30 }}
className={`${prefixCls}-input-contents`}
size={size}
disabled={disabled}
onKeyDown={inputKeyDown}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => handleInputChange(e.target.value)}
value={searchValue}
placeholder={selectedOption.length ? '' : placeholder}
// readOnly={false}
/>
</div>
<span style={{ height: 25 }} className={`${prefixCls}-close-tag-contents`}>
{(selectIconType === 'close' || (selectIconType === 'loading' && loading)) && (
<Icon type={selectIconType} spin={loading && selectIconType === 'loading'} onClick={resetSelectedValue} />
)}
</span>
</div>
</div>
</Dropdown>
);
}
export default SearchTagInput;