@tendaui/components 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +176 -0
  3. package/alert/Alert.tsx +147 -0
  4. package/alert/defaultProps.ts +3 -0
  5. package/alert/index.ts +9 -0
  6. package/alert/style/css.js +1 -0
  7. package/alert/style/index.js +1 -0
  8. package/alert/type.ts +44 -0
  9. package/badge/Badge.tsx +85 -0
  10. package/badge/defaultProps.ts +10 -0
  11. package/badge/index.ts +9 -0
  12. package/badge/style/css.js +1 -0
  13. package/badge/style/index.js +1 -0
  14. package/badge/type.ts +51 -0
  15. package/button/Button.tsx +95 -0
  16. package/button/defaultProps.ts +13 -0
  17. package/button/index.ts +7 -0
  18. package/button/style/css.js +1 -0
  19. package/button/style/index.js +1 -0
  20. package/button/type.ts +82 -0
  21. package/checkbox/Checkbox.tsx +19 -0
  22. package/checkbox/CheckboxGroup.tsx +207 -0
  23. package/checkbox/defaultProps.ts +14 -0
  24. package/checkbox/index.ts +10 -0
  25. package/checkbox/style/css.js +1 -0
  26. package/checkbox/style/index.js +1 -0
  27. package/checkbox/type.ts +117 -0
  28. package/common/Check.tsx +131 -0
  29. package/common/FakeArrow.tsx +36 -0
  30. package/common/PluginContainer.tsx +21 -0
  31. package/common/Portal.tsx +67 -0
  32. package/common.ts +76 -0
  33. package/config-provider/ConfigContext.tsx +21 -0
  34. package/config-provider/ConfigProvider.tsx +53 -0
  35. package/config-provider/index.ts +9 -0
  36. package/config-provider/type.ts +1062 -0
  37. package/dialog/Dialog.tsx +254 -0
  38. package/dialog/DialogCard.tsx +152 -0
  39. package/dialog/defaultProps.ts +25 -0
  40. package/dialog/hooks/useDialogDrag.ts +50 -0
  41. package/dialog/hooks/useDialogEsc.ts +31 -0
  42. package/dialog/hooks/useDialogPosition.ts +36 -0
  43. package/dialog/hooks/useLockStyle.ts +54 -0
  44. package/dialog/index.ts +13 -0
  45. package/dialog/plugin.tsx +78 -0
  46. package/dialog/style/css.js +1 -0
  47. package/dialog/style/index.js +1 -0
  48. package/dialog/type.ts +241 -0
  49. package/dialog/utils.ts +4 -0
  50. package/form/Form.tsx +136 -0
  51. package/form/FormContext.tsx +64 -0
  52. package/form/FormItem.tsx +554 -0
  53. package/form/FormList.tsx +303 -0
  54. package/form/const.ts +6 -0
  55. package/form/defaultProps.ts +26 -0
  56. package/form/formModel.ts +117 -0
  57. package/form/hooks/interface.ts +20 -0
  58. package/form/hooks/useForm.ts +122 -0
  59. package/form/hooks/useFormItemInitialData.ts +95 -0
  60. package/form/hooks/useFormItemStyle.tsx +122 -0
  61. package/form/hooks/useInstance.tsx +275 -0
  62. package/form/hooks/useWatch.ts +42 -0
  63. package/form/index.ts +11 -0
  64. package/form/style/css.js +1 -0
  65. package/form/style/index.js +1 -0
  66. package/form/type.ts +519 -0
  67. package/form/utils/index.ts +69 -0
  68. package/hooks/useAttach.ts +24 -0
  69. package/hooks/useCommonClassName.ts +45 -0
  70. package/hooks/useConfig.ts +3 -0
  71. package/hooks/useControlled.ts +39 -0
  72. package/hooks/useDefaultProps.ts +16 -0
  73. package/hooks/useDomCallback.ts +13 -0
  74. package/hooks/useDomRefCallback.ts +12 -0
  75. package/hooks/useDragSorter.tsx +151 -0
  76. package/hooks/useEventCallback.ts +47 -0
  77. package/hooks/useGlobalConfig.ts +14 -0
  78. package/hooks/useGlobalIcon.ts +14 -0
  79. package/hooks/useLastest.ts +13 -0
  80. package/hooks/useLayoutEffect.ts +7 -0
  81. package/hooks/useMouseEvent.ts +142 -0
  82. package/hooks/useMutationObserver.ts +56 -0
  83. package/hooks/usePopper.ts +189 -0
  84. package/hooks/useRipple.ts +0 -0
  85. package/hooks/useSetState.ts +25 -0
  86. package/hooks/useVirtualScroll.ts +246 -0
  87. package/hooks/useWindowSize.ts +31 -0
  88. package/index.ts +70 -0
  89. package/input/Input.tsx +383 -0
  90. package/input/InputGroup.tsx +29 -0
  91. package/input/defaultProps.ts +22 -0
  92. package/input/index.ts +11 -0
  93. package/input/style/css.js +1 -0
  94. package/input/style/index.js +1 -0
  95. package/input/type.ts +219 -0
  96. package/loading/Gradient.tsx +36 -0
  97. package/loading/Loading.tsx +169 -0
  98. package/loading/circleAdapter.ts +44 -0
  99. package/loading/defaultProps.ts +12 -0
  100. package/loading/index.ts +13 -0
  101. package/loading/style/css.js +1 -0
  102. package/loading/style/index.js +1 -0
  103. package/loading/type.ts +71 -0
  104. package/loading/utils/setStyle.ts +13 -0
  105. package/myform/index.ts +0 -0
  106. package/notification/Notify.ts +24 -0
  107. package/notification/NotifyContainer.tsx +90 -0
  108. package/notification/NotifyContext.tsx +173 -0
  109. package/notification/NotifyItem.tsx +121 -0
  110. package/notification/index.ts +3 -0
  111. package/notification/style/css.js +1 -0
  112. package/notification/style/index.js +1 -0
  113. package/notification/type.ts +23 -0
  114. package/package.json +52 -0
  115. package/popup/Popup.tsx +264 -0
  116. package/popup/defaultProps.ts +13 -0
  117. package/popup/hooks/useTrigger.ts +276 -0
  118. package/popup/index.ts +6 -0
  119. package/popup/style/css.js +1 -0
  120. package/popup/style/index.js +1 -0
  121. package/popup/type.ts +130 -0
  122. package/portal/Portal.tsx +63 -0
  123. package/portal/index.ts +1 -0
  124. package/select/Option.tsx +162 -0
  125. package/select/OptionGroup.tsx +30 -0
  126. package/select/PopupContent.tsx +271 -0
  127. package/select/Select.tsx +586 -0
  128. package/select/defaultProps.ts +27 -0
  129. package/select/hooks/useOptions.ts +120 -0
  130. package/select/hooks/usePanelVirtualScroll.ts +111 -0
  131. package/select/index.ts +9 -0
  132. package/select/style/css.js +1 -0
  133. package/select/style/index.js +2 -0
  134. package/select/type.ts +382 -0
  135. package/select/utils/helper.ts +256 -0
  136. package/select-input/SelectInput.tsx +98 -0
  137. package/select-input/defaultProps.ts +15 -0
  138. package/select-input/hook/useMultiple.tsx +100 -0
  139. package/select-input/hook/useOverlayInnerStyle.ts +84 -0
  140. package/select-input/hook/useSingle.tsx +112 -0
  141. package/select-input/index.ts +6 -0
  142. package/select-input/interface.ts +18 -0
  143. package/select-input/style/css.js +1 -0
  144. package/select-input/style/index.js +1 -0
  145. package/select-input/type.ts +280 -0
  146. package/space/defaultProps.ts +0 -0
  147. package/space/index.ts +0 -0
  148. package/space/type.ts +0 -0
  149. package/style/index.js +2 -0
  150. package/styles/_global.scss +39 -0
  151. package/styles/_vars.scss +386 -0
  152. package/styles/components/alert/_index.scss +175 -0
  153. package/styles/components/alert/_vars.scss +39 -0
  154. package/styles/components/badge/_index.scss +70 -0
  155. package/styles/components/badge/_vars.scss +25 -0
  156. package/styles/components/button/_index.scss +511 -0
  157. package/styles/components/button/_mixins.scss +39 -0
  158. package/styles/components/button/_vars.scss +122 -0
  159. package/styles/components/checkbox/_index.scss +158 -0
  160. package/styles/components/checkbox/_mixin.scss +0 -0
  161. package/styles/components/checkbox/_var.scss +60 -0
  162. package/styles/components/dialog/_animate.scss +135 -0
  163. package/styles/components/dialog/_index.scss +311 -0
  164. package/styles/components/dialog/_mixins.scss +0 -0
  165. package/styles/components/dialog/_vars.scss +59 -0
  166. package/styles/components/form/_index.scss +174 -0
  167. package/styles/components/form/_mixins.scss +76 -0
  168. package/styles/components/form/_vars.scss +100 -0
  169. package/styles/components/input/_index.scss +349 -0
  170. package/styles/components/input/_map.scss +0 -0
  171. package/styles/components/input/_mixins.scss +116 -0
  172. package/styles/components/input/_vars.scss +134 -0
  173. package/styles/components/loading/_index.scss +112 -0
  174. package/styles/components/loading/_vars.scss +39 -0
  175. package/styles/components/notification/_index.scss +160 -0
  176. package/styles/components/notification/_mixins.scss +12 -0
  177. package/styles/components/notification/_vars.scss +59 -0
  178. package/styles/components/popup/_index.scss +82 -0
  179. package/styles/components/popup/_mixin.scss +149 -0
  180. package/styles/components/popup/_var.scss +31 -0
  181. package/styles/components/select/_index.scss +290 -0
  182. package/styles/components/select/_var.scss +65 -0
  183. package/styles/components/select-input/_index.scss +5 -0
  184. package/styles/components/select-input/_var.scss +3 -0
  185. package/styles/components/switch/_index.scss +279 -0
  186. package/styles/components/switch/_mixins.scss +0 -0
  187. package/styles/components/switch/_vars.scss +61 -0
  188. package/styles/components/tag/_index.scss +316 -0
  189. package/styles/components/tag/_var.scss +85 -0
  190. package/styles/components/tag-input/_index.scss +163 -0
  191. package/styles/components/tag-input/_vars.scss +16 -0
  192. package/styles/globals.css +250 -0
  193. package/styles/mixins/_focus.scss +7 -0
  194. package/styles/mixins/_layout.scss +32 -0
  195. package/styles/mixins/_reset.scss +10 -0
  196. package/styles/mixins/_scrollbar.scss +31 -0
  197. package/styles/mixins/_text.scss +48 -0
  198. package/styles/rillple.css +16 -0
  199. package/styles/scrollbar.css +42 -0
  200. package/styles/themes/_dark.scss +191 -0
  201. package/styles/themes/_font.scss +79 -0
  202. package/styles/themes/_index.scss +5 -0
  203. package/styles/themes/_light.scss +190 -0
  204. package/styles/themes/_radius.scss +9 -0
  205. package/styles/themes/_size.scss +68 -0
  206. package/styles/themes.css +66 -0
  207. package/styles/utilities/_animation.scss +57 -0
  208. package/styles/utilities/_tips.scss +9 -0
  209. package/switch/Switch.tsx +120 -0
  210. package/switch/defaultProps.ts +3 -0
  211. package/switch/index.ts +7 -0
  212. package/switch/style/css.js +1 -0
  213. package/switch/style/index.js +1 -0
  214. package/switch/type.ts +46 -0
  215. package/tag/Tag.tsx +149 -0
  216. package/tag/defaultProps.ts +19 -0
  217. package/tag/index.ts +8 -0
  218. package/tag/style/css.js +1 -0
  219. package/tag/style/index.js +1 -0
  220. package/tag/type.ts +170 -0
  221. package/tag-input/TagInput.tsx +215 -0
  222. package/tag-input/defaultProps.ts +15 -0
  223. package/tag-input/hooks/useHover.ts +28 -0
  224. package/tag-input/hooks/useTagList.tsx +131 -0
  225. package/tag-input/hooks/useTagScroll.ts +105 -0
  226. package/tag-input/index.ts +9 -0
  227. package/tag-input/style/css.js +1 -0
  228. package/tag-input/style/index.js +1 -0
  229. package/tag-input/type.ts +224 -0
  230. package/tag-input/useTagList.tsx +131 -0
  231. package/utils/composeRefs.ts +14 -0
  232. package/utils/dom.ts +29 -0
  233. package/utils/forwardRefWithStatics.ts +12 -0
  234. package/utils/getScrollbarWidth.ts +11 -0
  235. package/utils/helper.ts +161 -0
  236. package/utils/isFragment.ts +22 -0
  237. package/utils/listener.ts +37 -0
  238. package/utils/noop.ts +3 -0
  239. package/utils/parentTNode.ts +38 -0
  240. package/utils/parseTNode.ts +38 -0
  241. package/utils/react-render.ts +108 -0
  242. package/utils/ref.ts +6 -0
  243. package/utils/refs.ts +81 -0
  244. package/utils/style.ts +60 -0
  245. package/utils/transition.ts +28 -0
@@ -0,0 +1,586 @@
1
+ import React, {
2
+ useEffect,
3
+ useMemo,
4
+ KeyboardEvent,
5
+ WheelEvent,
6
+ useRef,
7
+ useCallback,
8
+ Children,
9
+ cloneElement,
10
+ isValidElement,
11
+ useState
12
+ } from "react";
13
+
14
+ import classNames from "classnames";
15
+ import { isFunction, get, debounce } from "lodash-es";
16
+ import { getOffsetTopToContainer } from "../utils/helper";
17
+ import useControlled from "../hooks/useControlled";
18
+ import useConfig from "../hooks/useConfig";
19
+ import forwardRefWithStatics from "../utils/forwardRefWithStatics";
20
+ import { getSelectValueArr, getSelectedOptions } from "./utils/helper";
21
+
22
+ import noop from "../utils/noop";
23
+ import FakeArrow from "../common/FakeArrow";
24
+ import Loading from "../loading";
25
+ import SelectInput, { SelectInputValue, SelectInputValueChangeContext } from "../select-input";
26
+ import Option from "./Option";
27
+ import OptionGroup from "./OptionGroup";
28
+ import PopupContent from "./PopupContent";
29
+ import { Tag } from "../tag";
30
+ import { TdSelectProps, TdOptionProps, SelectOption, SelectValueChangeTrigger, SelectValue } from "./type";
31
+
32
+ import { StyledProps } from "../common";
33
+ import { selectDefaultProps } from "./defaultProps";
34
+ import { PopupVisibleChangeContext, type TdPopupProps } from "../popup";
35
+
36
+ import useOptions, { isSelectOptionGroup } from "./hooks/useOptions";
37
+ import composeRefs from "../utils/composeRefs";
38
+ import { parseContentTNode } from "../utils/parentTNode";
39
+ import useDefaultProps from "../hooks/useDefaultProps";
40
+
41
+ export interface SelectProps<T = SelectOption> extends TdSelectProps<T>, StyledProps {
42
+ // 子节点
43
+ children?: React.ReactNode;
44
+ onMouseEnter?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
45
+ onMouseLeave?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
46
+ }
47
+
48
+ type OptionsType = TdOptionProps[];
49
+
50
+ const Select = forwardRefWithStatics(
51
+ (originalProps: SelectProps, ref: React.Ref<HTMLDivElement>) => {
52
+ const props = useDefaultProps<SelectProps>(originalProps, selectDefaultProps);
53
+ const {
54
+ readonly,
55
+ borderless,
56
+ autoWidth,
57
+ creatable,
58
+ loadingText = "加载中...",
59
+ max,
60
+ popupProps,
61
+ reserveKeyword,
62
+ className,
63
+ style,
64
+ disabled,
65
+ size,
66
+ multiple,
67
+ placeholder,
68
+ clearable,
69
+ prefixIcon,
70
+ options,
71
+ filterable,
72
+ loading,
73
+ empty,
74
+ valueType,
75
+ keys,
76
+ children,
77
+ collapsedItems,
78
+ minCollapsedNum,
79
+ valueDisplay,
80
+ showArrow,
81
+ inputProps,
82
+ panelBottomContent,
83
+ panelTopContent,
84
+ selectInputProps,
85
+ tagInputProps,
86
+ tagProps,
87
+ scroll,
88
+ suffixIcon,
89
+ label,
90
+ filter,
91
+ onFocus,
92
+ onBlur,
93
+ onClear = noop,
94
+ onCreate,
95
+ onRemove,
96
+ onSearch,
97
+ onEnter,
98
+ onPopupVisibleChange
99
+ } = props;
100
+
101
+ const [value, onChange] = useControlled(props, "value", props.onChange);
102
+ const selectInputRef = useRef(null);
103
+ const { classPrefix } = useConfig();
104
+ const { overlayClassName, onScroll, onScrollToBottom, ...restPopupProps } = popupProps || {};
105
+ const [isScrolling, toggleIsScrolling] = useState(false);
106
+
107
+ const name = `${classPrefix}-select`; // t-select
108
+
109
+ const [showPopup, setShowPopup] = useControlled(props, "popupVisible", onPopupVisibleChange);
110
+ const [inputValue, onInputChange] = useControlled(props, "inputValue", props.onInputChange);
111
+
112
+ const { currentOptions, setCurrentOptions, tmpPropOptions, valueToOption, selectedOptions } = useOptions(
113
+ keys,
114
+ options,
115
+ children,
116
+ valueType,
117
+ value,
118
+ reserveKeyword
119
+ );
120
+
121
+ const selectedLabel = useMemo(() => {
122
+ if (multiple) {
123
+ return selectedOptions.map((selectedOption) => get(selectedOption || {}, keys?.label || "label") || "");
124
+ }
125
+ return get(selectedOptions[0] || {}, keys?.label || "label") || undefined;
126
+ }, [selectedOptions, keys, multiple]);
127
+
128
+ const handleShowPopup = (visible: boolean, ctx: PopupVisibleChangeContext) => {
129
+ if (disabled) return;
130
+ if (visible) {
131
+ toggleIsScrolling(false);
132
+ }
133
+ if (!visible) {
134
+ onInputChange("", { trigger: "blur" });
135
+ }
136
+ setShowPopup(visible, ctx);
137
+ };
138
+
139
+ // 可以根据触发来源,自由定制标签变化时的筛选器行为
140
+ const onTagChange = (_currentTags: SelectInputValue, context) => {
141
+ const { trigger, index, item, e } = context;
142
+ // backspace
143
+ if (trigger === "backspace") {
144
+ e.stopPropagation();
145
+
146
+ let closest = -1;
147
+ let len = index;
148
+ while (len >= 0) {
149
+ const option = selectedOptions[len];
150
+ if (!isSelectOptionGroup(option) && !option.disabled) {
151
+ closest = len;
152
+ break;
153
+ }
154
+ len -= 1;
155
+ }
156
+ if (closest < 0) {
157
+ return;
158
+ }
159
+ const values = getSelectValueArr(value, value[closest], true, valueType, keys);
160
+
161
+ // 处理onChange回调中的selectedOptions参数
162
+ const { currentSelectedOptions } = getSelectedOptions(values, multiple, valueType, keys, valueToOption);
163
+ onChange(values, {
164
+ e,
165
+ trigger,
166
+ selectedOptions: currentSelectedOptions
167
+ });
168
+ return;
169
+ }
170
+
171
+ if (trigger === "tag-remove") {
172
+ e?.stopPropagation?.();
173
+ const values = getSelectValueArr(value, value[index], true, valueType, keys);
174
+ // 处理onChange回调中的selectedOptions参数
175
+ const { currentSelectedOptions } = getSelectedOptions(values, multiple, valueType, keys, valueToOption);
176
+
177
+ onChange(values, {
178
+ e,
179
+ trigger,
180
+ selectedOptions: currentSelectedOptions
181
+ });
182
+ if (isFunction(onRemove)) {
183
+ onRemove({
184
+ value: value[index],
185
+ data: {
186
+ label: item,
187
+ value: value[index]
188
+ },
189
+ e
190
+ });
191
+ }
192
+ }
193
+ };
194
+
195
+ const onCheckAllChange = (checkAll: boolean, e: React.MouseEvent<HTMLLIElement>) => {
196
+ const isDisabledCheckAll = (opt: TdOptionProps) => opt.checkAll && opt.disabled;
197
+ if (!multiple || currentOptions.some((opt) => !isSelectOptionGroup(opt) && isDisabledCheckAll(opt))) {
198
+ return;
199
+ }
200
+
201
+ const isSelectableOption = (opt: TdOptionProps) => !opt.checkAll && !opt.disabled;
202
+ const getOptionValue = (option: SelectOption) =>
203
+ valueType === "object" ? option : option[keys?.value || "value"];
204
+
205
+ const values = [];
206
+ currentOptions.forEach((option) => {
207
+ if (isSelectOptionGroup(option)) {
208
+ option.children.forEach((item) => {
209
+ if (isSelectableOption(item)) {
210
+ values.push(getOptionValue(item));
211
+ }
212
+ });
213
+ } else if (isSelectableOption(option)) {
214
+ values.push(getOptionValue(option));
215
+ }
216
+ });
217
+
218
+ const { currentSelectedOptions, allSelectedValue } = getSelectedOptions(
219
+ values,
220
+ multiple,
221
+ valueType,
222
+ keys,
223
+ valueToOption
224
+ );
225
+
226
+ const checkAllValue =
227
+ !checkAll && allSelectedValue.length !== (props.value as Array<SelectOption>)?.length ? allSelectedValue : [];
228
+
229
+ onChange?.(checkAllValue, {
230
+ e,
231
+ trigger: !checkAll ? "check" : "uncheck",
232
+ selectedOptions: currentSelectedOptions
233
+ });
234
+ };
235
+
236
+ // 选中 Popup 某项
237
+ const handleChange = (
238
+ value: string | number | Array<string | number | Record<string, string | number>>,
239
+ context: {
240
+ e: React.MouseEvent<HTMLLIElement>;
241
+ trigger: SelectValueChangeTrigger;
242
+ value?: SelectValue;
243
+ label?: string;
244
+ }
245
+ ) => {
246
+ const selectedValue = multiple ? context.value : value;
247
+
248
+ if (multiple) {
249
+ if (!reserveKeyword && inputValue) {
250
+ onInputChange("", { e: context.e, trigger: "change" });
251
+ }
252
+ }
253
+ if (creatable && isFunction(onCreate)) {
254
+ if ((options as OptionsType).filter((option) => option.value === selectedValue).length === 0) {
255
+ onCreate(selectedValue as string); // 手动输入 此时为string
256
+ }
257
+ }
258
+ // 处理onChange回调中的selectedOptions参数
259
+ const { currentSelectedOptions, currentOption } = getSelectedOptions(
260
+ value,
261
+ multiple,
262
+ valueType,
263
+ keys,
264
+ valueToOption,
265
+ selectedValue
266
+ );
267
+ onChange?.(value, {
268
+ e: context.e,
269
+ trigger: context.trigger,
270
+ selectedOptions: currentSelectedOptions,
271
+ option: currentOption
272
+ });
273
+
274
+ if (multiple && context?.trigger === "uncheck" && isFunction(onRemove)) {
275
+ const value = context?.value;
276
+ const option = (options as OptionsType).find((option) => option.value === value);
277
+ onRemove({
278
+ value,
279
+ data: option,
280
+ e: context.e
281
+ });
282
+ }
283
+ };
284
+
285
+ // 处理filter逻辑
286
+ const handleFilter = (value: string) => {
287
+ let filteredOptions: SelectOption[] = [];
288
+ if (filterable && isFunction(onSearch)) {
289
+ return;
290
+ }
291
+ if (!value) {
292
+ setCurrentOptions(tmpPropOptions);
293
+ return;
294
+ }
295
+
296
+ const filterLabels = [];
297
+ const filterMethods = (option: SelectOption) => {
298
+ if (filter && isFunction(filter)) {
299
+ return filter(value, option);
300
+ }
301
+ const upperValue = value.toUpperCase();
302
+ return (String(option?.label) || "").toUpperCase().includes(upperValue);
303
+ };
304
+
305
+ tmpPropOptions?.forEach((option) => {
306
+ if (isSelectOptionGroup(option)) {
307
+ filteredOptions.push({
308
+ ...option,
309
+ children: option.children?.filter((child) => {
310
+ if (filterMethods(child)) {
311
+ filterLabels.push(child.label);
312
+ return true;
313
+ }
314
+ return false;
315
+ })
316
+ });
317
+ } else if (filterMethods(option)) {
318
+ filterLabels.push(option.label);
319
+ filteredOptions.push(option);
320
+ }
321
+ });
322
+ const isSameLabelOptionExist = filterLabels.includes(value);
323
+ if (creatable && !isSameLabelOptionExist) {
324
+ filteredOptions = filteredOptions.concat([{ label: value, value }]);
325
+ }
326
+ setCurrentOptions(filteredOptions);
327
+ };
328
+
329
+ // 处理输入框逻辑
330
+ const handleInputChange = (value: string, context: SelectInputValueChangeContext) => {
331
+ if (context.trigger !== "clear") {
332
+ onInputChange(value, { e: context.e, trigger: "input" });
333
+ }
334
+ if (value === undefined) {
335
+ return;
336
+ }
337
+ if (isFunction(onSearch)) {
338
+ onSearch(value, { e: context.e as KeyboardEvent<HTMLDivElement> });
339
+ return;
340
+ }
341
+ };
342
+
343
+ const handleClear = (context) => {
344
+ context.e.stopPropagation();
345
+ if (Array.isArray(value)) {
346
+ onChange([], { ...context, trigger: "clear", selectedOptions: [] });
347
+ } else {
348
+ onChange(null, { ...context, trigger: "clear", selectedOptions: [] });
349
+ }
350
+ onClear(context);
351
+ };
352
+
353
+ useEffect(() => {
354
+ if (typeof inputValue !== "undefined") {
355
+ handleFilter(String(inputValue));
356
+ }
357
+ // eslint-disable-next-line react-hooks/exhaustive-deps
358
+ }, [inputValue, tmpPropOptions]);
359
+
360
+ // 渲染后置图标
361
+ const renderSuffixIcon = () => {
362
+ if (suffixIcon) {
363
+ return suffixIcon;
364
+ }
365
+ if (loading) {
366
+ return (
367
+ <Loading className={classNames(`${name}__right-icon`, `${name}__active-icon`)} loading={true} size="small" />
368
+ );
369
+ }
370
+
371
+ return showArrow && <FakeArrow className={`${name}__right-icon`} isActive={showPopup} disabled={disabled} />;
372
+ };
373
+
374
+ const getPopupInstance = useCallback(() => selectInputRef.current?.getPopupContentElement(), []);
375
+
376
+ const childrenWithProps = Children.map(children, (child) => {
377
+ if (isValidElement(child)) {
378
+ const addedProps = { multiple };
379
+ return cloneElement(child, { ...addedProps });
380
+ }
381
+ return child;
382
+ });
383
+
384
+ // 渲染主体内容
385
+ const renderContent = () => {
386
+ const popupContentProps = {
387
+ onChange: handleChange,
388
+ value,
389
+ className,
390
+ size,
391
+ multiple,
392
+ showPopup,
393
+ // popup弹出层内容只会在点击事件之后触发 并且无任何透传参数
394
+ setShowPopup: (show: boolean) => handleShowPopup(show, {}),
395
+ options: currentOptions,
396
+ empty,
397
+ max,
398
+ loadingText,
399
+ loading,
400
+ valueType,
401
+ keys,
402
+ panelBottomContent,
403
+ panelTopContent,
404
+ onCheckAllChange,
405
+ getPopupInstance,
406
+ scroll
407
+ };
408
+ return <PopupContent {...popupContentProps}>{childrenWithProps}</PopupContent>;
409
+ };
410
+
411
+ const renderValueDisplay = () => {
412
+ if (!valueDisplay) {
413
+ if (!multiple) {
414
+ if (typeof selectedLabel !== "string") {
415
+ return selectedLabel;
416
+ }
417
+ return "";
418
+ }
419
+ return ({ value: val }) =>
420
+ val.slice(0, minCollapsedNum ? minCollapsedNum : val.length).map((v: string, key: number) => {
421
+ const filterOption: SelectOption & { disabled?: boolean } = options?.find((option) => option.label === v);
422
+ return (
423
+ <Tag
424
+ key={key}
425
+ closable={!filterOption?.disabled && !disabled && !readonly}
426
+ size={size}
427
+ {...tagProps}
428
+ onClose={({ e }) => {
429
+ e.stopPropagation();
430
+ e?.nativeEvent?.stopImmediatePropagation?.();
431
+ const values = getSelectValueArr(value, value[key], true, valueType, keys);
432
+
433
+ const { currentSelectedOptions } = getSelectedOptions(
434
+ values,
435
+ multiple,
436
+ valueType,
437
+ keys,
438
+ valueToOption,
439
+ value
440
+ );
441
+ onChange(values, {
442
+ e,
443
+ selectedOptions: currentSelectedOptions,
444
+ trigger: "tag-remove"
445
+ });
446
+ tagProps?.onClose?.({ e });
447
+
448
+ onRemove?.({
449
+ value: value[key],
450
+ data: { label: v, value: value[key] },
451
+ e: e as unknown as React.MouseEvent<HTMLDivElement, MouseEvent>
452
+ });
453
+ }}
454
+ >
455
+ {v}
456
+ </Tag>
457
+ );
458
+ });
459
+ }
460
+ if (typeof valueDisplay === "string") {
461
+ return valueDisplay;
462
+ }
463
+ if (multiple) {
464
+ return ({ onClose }) => parseContentTNode(valueDisplay, { value: selectedOptions, onClose });
465
+ }
466
+ return parseContentTNode(valueDisplay, {
467
+ value: selectedLabel,
468
+ onClose: noop
469
+ });
470
+ };
471
+
472
+ // 将第一个选中的 option 置于列表可见范围的最后一位
473
+ const updateScrollTop = (content: HTMLDivElement) => {
474
+ if (!content || isScrolling) {
475
+ return;
476
+ }
477
+ const firstSelectedNode: HTMLDivElement = content.querySelector(`.${classPrefix}-is-selected`);
478
+ if (!multiple && firstSelectedNode) {
479
+ const { paddingBottom } = getComputedStyle(firstSelectedNode);
480
+ const { marginBottom } = getComputedStyle(content);
481
+ const elementBottomHeight = parseInt(paddingBottom, 10) + parseInt(marginBottom, 10);
482
+ // 小于0时不需要特殊处理,会被设为0
483
+ const updateValue =
484
+ getOffsetTopToContainer(firstSelectedNode, content) -
485
+ content.offsetTop -
486
+ (content.clientHeight - firstSelectedNode.clientHeight) +
487
+ elementBottomHeight;
488
+
489
+ // 通过 setTimeout 确保组件渲染完成后再设置 scrollTop
490
+ setTimeout(() => {
491
+ content.scrollTop = updateValue;
492
+ });
493
+ }
494
+ };
495
+
496
+ const { onMouseEnter, onMouseLeave } = props;
497
+
498
+ const handleEnter = (_, context: { inputValue: string; e: KeyboardEvent<HTMLDivElement> }) => {
499
+ onEnter?.({ ...context, value });
500
+ };
501
+
502
+ const handleScroll = ({ e }: { e: WheelEvent<HTMLDivElement> }) => {
503
+ toggleIsScrolling(true);
504
+
505
+ onScroll?.({ e });
506
+ if (onScrollToBottom) {
507
+ const debounceOnScrollBottom = debounce((e) => onScrollToBottom({ e }), 100);
508
+
509
+ const { scrollTop, clientHeight, scrollHeight } = e.target as HTMLDivElement;
510
+ if (clientHeight + Math.floor(scrollTop) === scrollHeight) {
511
+ debounceOnScrollBottom(e);
512
+ }
513
+ }
514
+ };
515
+
516
+ return (
517
+ <div
518
+ className={classNames(`${name}__wrap`, className)}
519
+ style={style}
520
+ onMouseEnter={onMouseEnter}
521
+ onMouseLeave={onMouseLeave}
522
+ >
523
+ <SelectInput
524
+ autoWidth={!style?.width && autoWidth}
525
+ ref={composeRefs(ref, selectInputRef)}
526
+ className={name}
527
+ readonly={readonly}
528
+ autofocus={props.autofocus}
529
+ allowInput={filterable || isFunction(filter)}
530
+ multiple={multiple}
531
+ value={selectedLabel}
532
+ options={selectedOptions}
533
+ valueDisplay={renderValueDisplay()}
534
+ clearable={clearable}
535
+ disabled={disabled}
536
+ status={props.status}
537
+ tips={props.tips}
538
+ borderless={borderless}
539
+ label={label}
540
+ suffix={props.suffix}
541
+ prefixIcon={prefixIcon}
542
+ suffixIcon={renderSuffixIcon()}
543
+ panel={renderContent()}
544
+ placeholder={!multiple && showPopup && selectedLabel ? selectedLabel : placeholder || "请选择"}
545
+ inputValue={inputValue}
546
+ tagInputProps={{
547
+ size,
548
+ ...tagInputProps
549
+ }}
550
+ tagProps={{ size, ...tagProps }}
551
+ inputProps={{
552
+ size,
553
+ ...inputProps
554
+ }}
555
+ minCollapsedNum={minCollapsedNum}
556
+ collapsedItems={collapsedItems}
557
+ updateScrollTop={updateScrollTop}
558
+ popupProps={{
559
+ overlayClassName: [`${name}__dropdown`, overlayClassName] as TdPopupProps["overlayClassName"],
560
+ onScroll: handleScroll,
561
+ ...restPopupProps
562
+ }}
563
+ popupVisible={showPopup}
564
+ onPopupVisibleChange={handleShowPopup}
565
+ onTagChange={onTagChange}
566
+ onInputChange={handleInputChange}
567
+ onFocus={onFocus}
568
+ onEnter={handleEnter}
569
+ onBlur={(_, context) => {
570
+ onBlur?.({
571
+ value,
572
+ e: context.e as React.FocusEvent<HTMLDivElement>
573
+ });
574
+ }}
575
+ onClear={handleClear}
576
+ {...selectInputProps}
577
+ />
578
+ </div>
579
+ );
580
+ },
581
+ { Option, OptionGroup }
582
+ );
583
+
584
+ Select.displayName = "Select";
585
+
586
+ export default Select;
@@ -0,0 +1,27 @@
1
+ import { TdSelectProps, TdOptionProps, TdOptionGroupProps } from "./type";
2
+
3
+ export const selectDefaultProps: TdSelectProps = {
4
+ autoWidth: false,
5
+ autofocus: false,
6
+ borderless: false,
7
+ clearable: false,
8
+ creatable: false,
9
+ loading: false,
10
+ max: 0,
11
+ minCollapsedNum: 0,
12
+ multiple: false,
13
+ placeholder: undefined,
14
+ readonly: false,
15
+ reserveKeyword: false,
16
+ showArrow: true,
17
+ size: "medium",
18
+ status: "default",
19
+ valueType: "value"
20
+ };
21
+
22
+ export const optionDefaultProps: TdOptionProps = {
23
+ checkAll: false,
24
+ disabled: false
25
+ };
26
+
27
+ export const optionGroupDefaultProps: TdOptionGroupProps = { divider: true };