-
-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathMultipleContent.tsx
More file actions
223 lines (205 loc) · 6.36 KB
/
MultipleContent.tsx
File metadata and controls
223 lines (205 loc) · 6.36 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
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
import * as React from 'react';
import { clsx } from 'clsx';
import Overflow from '@rc-component/overflow';
import Input from '../Input';
import { useSelectInputContext } from '../context';
import type { SharedContentProps } from '.';
import type { DisplayValueType, RawValueType } from '../../interface';
import type { RenderNode, CustomTagProps } from '../../BaseSelect';
import TransBtn from '../../TransBtn';
import { getTitle } from '../../utils/commonUtil';
import useBaseProps from '../../hooks/useBaseProps';
import Placeholder from './Placeholder';
function itemKey(value: DisplayValueType) {
return value.key ?? value.value;
}
const onPreventMouseDown = (event: React.MouseEvent) => {
event.preventDefault();
event.stopPropagation();
};
export default React.forwardRef<HTMLInputElement, SharedContentProps>(function MultipleContent(
{ inputProps },
ref,
) {
const {
prefixCls,
displayValues,
searchValue,
mode,
onSelectorRemove,
removeIcon: removeIconFromContext,
} = useSelectInputContext();
const {
disabled,
showSearch,
triggerOpen,
rawOpen,
toggleOpen,
autoClearSearchValue,
tagRender: tagRenderFromContext,
maxTagPlaceholder: maxTagPlaceholderFromContext,
maxTagTextLength,
maxTagCount,
classNames,
styles,
} = useBaseProps();
const selectionItemPrefixCls = `${prefixCls}-selection-item`;
// ===================== Search ======================
// Apply autoClearSearchValue logic: when dropdown is closed and autoClearSearchValue is not false (default true), clear search value
// Use rawOpen to avoid clearing search when emptyListContent blocks open
let computedSearchValue = searchValue;
if (!rawOpen && mode === 'multiple' && autoClearSearchValue !== false) {
computedSearchValue = '';
}
const inputValue = showSearch ? computedSearchValue || '' : '';
const inputEditable: boolean = showSearch && !disabled;
// Props from context with safe defaults
const removeIcon: RenderNode = removeIconFromContext ?? '×';
const maxTagPlaceholder:
| React.ReactNode
| ((omittedValues: DisplayValueType[]) => React.ReactNode) =
maxTagPlaceholderFromContext ??
((omittedValues: DisplayValueType[]) => `+ ${omittedValues.length} ...`);
const tagRender: ((props: CustomTagProps) => React.ReactElement) | undefined =
tagRenderFromContext;
const onToggleOpen = (newOpen?: boolean) => {
toggleOpen(newOpen);
};
const onRemove = (value: DisplayValueType) => {
onSelectorRemove?.(value);
};
// ======================== Item ========================
// >>> Render Selector Node. Includes Item & Rest
const defaultRenderSelector = (
item: DisplayValueType,
content: React.ReactNode,
itemDisabled: boolean,
closable?: boolean,
onClose?: React.MouseEventHandler,
) => (
<span
title={getTitle(item)}
className={clsx(
selectionItemPrefixCls,
{
[`${selectionItemPrefixCls}-disabled`]: itemDisabled,
},
classNames?.item,
)}
style={styles?.item}
>
<span
className={clsx(`${selectionItemPrefixCls}-content`, classNames?.itemContent)}
style={styles?.itemContent}
>
{content}
</span>
{closable && (
<TransBtn
className={clsx(`${selectionItemPrefixCls}-remove`, classNames?.itemRemove)}
style={styles?.itemRemove}
onMouseDown={onPreventMouseDown}
onClick={onClose}
customizeIcon={removeIcon}
>
×
</TransBtn>
)}
</span>
);
const customizeRenderSelector = (
value: RawValueType,
content: React.ReactNode,
itemDisabled: boolean,
closable?: boolean,
onClose?: React.MouseEventHandler,
isMaxTag?: boolean,
info?: { index: number },
) => {
const onMouseDown = (e: React.MouseEvent) => {
onPreventMouseDown(e);
onToggleOpen(!triggerOpen);
};
return (
<span onMouseDown={onMouseDown}>
{tagRender({
label: content,
value,
index: info?.index,
disabled: itemDisabled,
closable,
onClose,
isMaxTag: !!isMaxTag,
})}
</span>
);
};
// ====================== Overflow ======================
const renderItem = (valueItem: DisplayValueType, info: { index: number }) => {
const { disabled: itemDisabled, label, value } = valueItem;
const closable = !disabled && !itemDisabled;
let displayLabel: React.ReactNode = label;
if (typeof maxTagTextLength === 'number') {
if (typeof label === 'string' || typeof label === 'number') {
const strLabel = String(displayLabel);
if (strLabel.length > maxTagTextLength) {
displayLabel = `${strLabel.slice(0, maxTagTextLength)}...`;
}
}
}
const onClose = (event?: React.MouseEvent) => {
if (event) {
event.stopPropagation();
}
onRemove(valueItem);
};
return typeof tagRender === 'function'
? customizeRenderSelector(
value,
displayLabel,
itemDisabled,
closable,
onClose,
undefined,
info,
)
: defaultRenderSelector(valueItem, displayLabel, itemDisabled, closable, onClose);
};
const renderRest = (omittedValues: DisplayValueType[]) => {
// https://github.com/ant-design/ant-design/issues/48930
if (!displayValues.length) {
return null;
}
const content =
typeof maxTagPlaceholder === 'function'
? maxTagPlaceholder(omittedValues)
: maxTagPlaceholder;
return typeof tagRender === 'function'
? customizeRenderSelector(undefined, content, false, false, undefined, true)
: defaultRenderSelector({ title: content }, content, false);
};
// ======================= Render =======================
return (
<Overflow
prefixCls={`${prefixCls}-content`}
className={classNames?.content}
style={styles?.content}
prefix={!displayValues.length && !inputValue && <Placeholder />}
data={displayValues}
renderItem={renderItem}
renderRest={renderRest}
suffix={
<Input
ref={ref}
disabled={disabled}
readOnly={!inputEditable}
{...inputProps}
value={inputValue || ''}
syncWidth
/>
}
itemKey={itemKey}
maxCount={maxTagCount}
/>
);
});