rsuite 5.59.3-alpha.1 → 5.60.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 (73) hide show
  1. package/Animation/styles/fade.less +1 -1
  2. package/Animation/styles/index.css +1 -1
  3. package/CHANGELOG.md +16 -0
  4. package/TagInput/styles/index.css +24 -24
  5. package/TagPicker/styles/index.css +24 -24
  6. package/TagPicker/styles/index.less +11 -10
  7. package/cjs/Form/Form.d.ts +13 -9
  8. package/cjs/Form/Form.js +55 -11
  9. package/cjs/Form/FormContext.d.ts +2 -2
  10. package/cjs/Form/hooks/useFormRef.d.ts +9 -7
  11. package/cjs/Form/hooks/useFormRef.js +5 -32
  12. package/cjs/Form/hooks/useFormValidate.d.ts +6 -4
  13. package/cjs/Form/hooks/useFormValidate.js +18 -12
  14. package/cjs/Form/hooks/useFormValue.d.ts +8 -1
  15. package/cjs/Form/hooks/useFormValue.js +7 -1
  16. package/cjs/InputPicker/InputAutosize.js +2 -2
  17. package/cjs/InputPicker/InputPicker.d.ts +5 -7
  18. package/cjs/InputPicker/InputPicker.js +119 -151
  19. package/cjs/InputPicker/InputPickerContext.d.ts +11 -3
  20. package/cjs/InputPicker/InputPickerContext.js +7 -1
  21. package/cjs/InputPicker/hooks/useData.d.ts +17 -0
  22. package/cjs/InputPicker/hooks/useData.js +44 -0
  23. package/cjs/InputPicker/hooks/useInput.d.ts +21 -0
  24. package/cjs/InputPicker/hooks/useInput.js +46 -0
  25. package/cjs/InputPicker/hooks/useMaxWidth.d.ts +4 -0
  26. package/cjs/InputPicker/hooks/useMaxWidth.js +25 -0
  27. package/cjs/InputPicker/index.d.ts +2 -1
  28. package/cjs/InputPicker/utils.d.ts +1 -0
  29. package/cjs/InputPicker/utils.js +17 -0
  30. package/cjs/TagInput/index.js +2 -2
  31. package/cjs/TagPicker/index.js +2 -2
  32. package/dist/rsuite-no-reset-rtl.css +25 -25
  33. package/dist/rsuite-no-reset-rtl.min.css +1 -1
  34. package/dist/rsuite-no-reset-rtl.min.css.map +1 -1
  35. package/dist/rsuite-no-reset.css +25 -25
  36. package/dist/rsuite-no-reset.min.css +1 -1
  37. package/dist/rsuite-no-reset.min.css.map +1 -1
  38. package/dist/rsuite-rtl.css +25 -25
  39. package/dist/rsuite-rtl.min.css +1 -1
  40. package/dist/rsuite-rtl.min.css.map +1 -1
  41. package/dist/rsuite.css +25 -25
  42. package/dist/rsuite.js +53 -9
  43. package/dist/rsuite.js.map +1 -1
  44. package/dist/rsuite.min.css +1 -1
  45. package/dist/rsuite.min.css.map +1 -1
  46. package/dist/rsuite.min.js +1 -1
  47. package/dist/rsuite.min.js.map +1 -1
  48. package/esm/Form/Form.d.ts +13 -9
  49. package/esm/Form/Form.js +55 -11
  50. package/esm/Form/FormContext.d.ts +2 -2
  51. package/esm/Form/hooks/useFormRef.d.ts +9 -7
  52. package/esm/Form/hooks/useFormRef.js +5 -32
  53. package/esm/Form/hooks/useFormValidate.d.ts +6 -4
  54. package/esm/Form/hooks/useFormValidate.js +18 -12
  55. package/esm/Form/hooks/useFormValue.d.ts +8 -1
  56. package/esm/Form/hooks/useFormValue.js +7 -1
  57. package/esm/InputPicker/InputAutosize.js +2 -2
  58. package/esm/InputPicker/InputPicker.d.ts +5 -7
  59. package/esm/InputPicker/InputPicker.js +111 -143
  60. package/esm/InputPicker/InputPickerContext.d.ts +11 -3
  61. package/esm/InputPicker/InputPickerContext.js +4 -0
  62. package/esm/InputPicker/hooks/useData.d.ts +17 -0
  63. package/esm/InputPicker/hooks/useData.js +38 -0
  64. package/esm/InputPicker/hooks/useInput.d.ts +21 -0
  65. package/esm/InputPicker/hooks/useInput.js +40 -0
  66. package/esm/InputPicker/hooks/useMaxWidth.d.ts +4 -0
  67. package/esm/InputPicker/hooks/useMaxWidth.js +19 -0
  68. package/esm/InputPicker/index.d.ts +2 -1
  69. package/esm/InputPicker/utils.d.ts +1 -0
  70. package/esm/InputPicker/utils.js +12 -0
  71. package/esm/TagInput/index.js +2 -2
  72. package/esm/TagPicker/index.js +2 -2
  73. package/package.json +1 -1
@@ -3,7 +3,7 @@ import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/esm/taggedTempla
3
3
  import _extends from "@babel/runtime/helpers/esm/extends";
4
4
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5
5
  var _templateObject, _templateObject2, _templateObject3, _templateObject4;
6
- import React, { useState, useRef, useEffect, useCallback, useContext } from 'react';
6
+ import React, { useEffect } from 'react';
7
7
  import PropTypes from 'prop-types';
8
8
  import isUndefined from 'lodash/isUndefined';
9
9
  import isNil from 'lodash/isNil';
@@ -13,7 +13,6 @@ import clone from 'lodash/clone';
13
13
  import isArray from 'lodash/isArray';
14
14
  import omit from 'lodash/omit';
15
15
  import pick from 'lodash/pick';
16
- import getWidth from 'dom-lib/getWidth';
17
16
  import shallowEqual from '../utils/shallowEqual';
18
17
  import { filterNodesOfTree } from '../utils/treeUtils';
19
18
  import Plaintext from '../internals/Plaintext';
@@ -22,21 +21,12 @@ import { oneOf } from '../internals/propTypes';
22
21
  import { getDataGroupBy } from '../utils/getDataGroupBy';
23
22
  import { Listbox, ListItem, ListCheckItem, PickerToggle, PickerPopup, PickerToggleTrigger, useFocusItemValue, usePickerClassName, useSearch, usePickerRef, useToggleKeyDownEvent, pickTriggerPropKeys, omitTriggerPropKeys, listPickerPropTypes } from '../internals/Picker';
24
23
  import Tag from '../Tag';
25
- import InputAutosize from './InputAutosize';
26
24
  import TextBox from './TextBox';
27
- import InputPickerContext from './InputPickerContext';
25
+ import { useTagContext } from './InputPickerContext';
28
26
  import Stack from '../Stack';
29
- var convertSize = function convertSize(size) {
30
- switch (size) {
31
- case 'lg':
32
- return 'lg';
33
- case 'sm':
34
- case 'xs':
35
- return 'sm';
36
- default:
37
- return 'md';
38
- }
39
- };
27
+ import useInput from './hooks/useInput';
28
+ import useData from './hooks/useData';
29
+ import { convertSize } from './utils';
40
30
  /**
41
31
  * Single item selector with text box input.
42
32
  *
@@ -67,10 +57,13 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
67
57
  overrideLocale = props.locale,
68
58
  toggleAs = props.toggleAs,
69
59
  style = props.style,
60
+ size = props.size,
70
61
  _props$searchable = props.searchable,
71
62
  searchable = _props$searchable === void 0 ? true : _props$searchable,
72
63
  controlledOpen = props.open,
73
64
  placeholder = props.placeholder,
65
+ _props$placement = props.placement,
66
+ placement = _props$placement === void 0 ? 'bottomStart' : _props$placement,
74
67
  groupBy = props.groupBy,
75
68
  menuClassName = props.menuClassName,
76
69
  menuStyle = props.menuStyle,
@@ -107,21 +100,17 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
107
100
  onBlur = props.onBlur,
108
101
  onFocus = props.onFocus,
109
102
  searchBy = props.searchBy,
110
- size = props.size,
111
- _props$placement = props.placement,
112
- placement = _props$placement === void 0 ? 'bottomStart' : _props$placement,
113
- rest = _objectWithoutPropertiesLoose(props, ["as", "appearance", "cleanable", "cacheData", "classPrefix", "data", "disabled", "readOnly", "plaintext", "defaultValue", "defaultOpen", "disabledItemValues", "locale", "toggleAs", "style", "searchable", "open", "placeholder", "groupBy", "menuClassName", "menuStyle", "menuAutoWidth", "menuMaxHeight", "creatable", "shouldDisplayCreateOption", "value", "valueKey", "virtualized", "labelKey", "listProps", "id", "tabIndex", "sort", "renderMenu", "renderExtraFooter", "renderValue", "renderMenuItem", "renderMenuGroup", "onEnter", "onEntered", "onExit", "onExited", "onChange", "onClean", "onCreate", "onSearch", "onSelect", "onBlur", "onFocus", "searchBy", "size", "placement"]);
114
- var _useContext = useContext(InputPickerContext),
115
- multi = _useContext.multi,
116
- tagProps = _useContext.tagProps,
117
- trigger = _useContext.trigger,
118
- disabledOptions = _useContext.disabledOptions,
119
- onTagRemove = _useContext.onTagRemove,
120
- renderCheckbox = _useContext.renderCheckbox;
103
+ rest = _objectWithoutPropertiesLoose(props, ["as", "appearance", "cleanable", "cacheData", "classPrefix", "data", "disabled", "readOnly", "plaintext", "defaultValue", "defaultOpen", "disabledItemValues", "locale", "toggleAs", "style", "size", "searchable", "open", "placeholder", "placement", "groupBy", "menuClassName", "menuStyle", "menuAutoWidth", "menuMaxHeight", "creatable", "shouldDisplayCreateOption", "value", "valueKey", "virtualized", "labelKey", "listProps", "id", "tabIndex", "sort", "renderMenu", "renderExtraFooter", "renderValue", "renderMenuItem", "renderMenuGroup", "onEnter", "onEntered", "onExit", "onExited", "onChange", "onClean", "onCreate", "onSearch", "onSelect", "onBlur", "onFocus", "searchBy"]);
104
+ var _useTagContext = useTagContext(),
105
+ multi = _useTagContext.multi,
106
+ tagProps = _useTagContext.tagProps,
107
+ trigger = _useTagContext.trigger,
108
+ disabledOptions = _useTagContext.disabledOptions,
109
+ onTagRemove = _useTagContext.onTagRemove,
110
+ renderCheckbox = _useTagContext.renderCheckbox;
121
111
  if (groupBy === valueKey || groupBy === labelKey) {
122
112
  throw Error('`groupBy` can not be equal to `valueKey` and `labelKey`');
123
113
  }
124
- var inputRef = useRef();
125
114
  var _usePickerRef = usePickerRef(ref),
126
115
  triggerRef = _usePickerRef.trigger,
127
116
  root = _usePickerRef.root,
@@ -133,29 +122,34 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
133
122
  var _useClassNames = useClassNames(classPrefix),
134
123
  prefix = _useClassNames.prefix,
135
124
  merge = _useClassNames.merge;
136
- var _useState = useState(controlledData),
137
- uncontrolledData = _useState[0],
138
- setData = _useState[1];
139
- var _useState2 = useState(100),
140
- maxWidth = _useState2[0],
141
- setMaxWidth = _useState2[1];
142
- var _useState3 = useState([]),
143
- newData = _useState3[0],
144
- setNewData = _useState3[1];
145
- var _useState4 = useState(defaultOpen),
146
- uncontrolledOpen = _useState4[0],
147
- setOpen = _useState4[1];
148
- var open = isUndefined(controlledOpen) ? uncontrolledOpen : controlledOpen;
149
- var getAllData = useCallback(function () {
150
- return [].concat(uncontrolledData, newData);
151
- }, [uncontrolledData, newData]);
152
- var getAllDataAndCache = useCallback(function () {
153
- return [].concat(getAllData(), cacheData);
154
- }, [getAllData, cacheData]);
155
- var _useControlled = useControlled(valueProp, multi ? defaultValue || [] : defaultValue),
156
- value = _useControlled[0],
157
- setValue = _useControlled[1],
158
- isControlled = _useControlled[2];
125
+ var _useControlled = useControlled(controlledOpen, defaultOpen),
126
+ open = _useControlled[0],
127
+ setOpen = _useControlled[1];
128
+ var _useInput = useInput({
129
+ multi: multi,
130
+ triggerRef: triggerRef
131
+ }),
132
+ inputRef = _useInput.inputRef,
133
+ inputProps = _useInput.inputProps,
134
+ focus = _useInput.focus,
135
+ blur = _useInput.blur;
136
+ var handleDataChange = function handleDataChange(data) {
137
+ var _data$;
138
+ setFocusItemValue(data === null || data === void 0 ? void 0 : (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$[valueKey]);
139
+ };
140
+ var _useData = useData({
141
+ controlledData: controlledData,
142
+ cacheData: cacheData,
143
+ onChange: handleDataChange
144
+ }),
145
+ data = _useData.data,
146
+ dataWithCache = _useData.dataWithCache,
147
+ newData = _useData.newData,
148
+ setNewData = _useData.setNewData;
149
+ var _useControlled2 = useControlled(valueProp, multi ? defaultValue || [] : defaultValue),
150
+ value = _useControlled2[0],
151
+ setValue = _useControlled2[1],
152
+ isControlled = _useControlled2[2];
159
153
  var cloneValue = function cloneValue() {
160
154
  return multi ? clone(value) || [] : value;
161
155
  };
@@ -166,62 +160,53 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
166
160
  // The focus is on the trigger button after closing
167
161
  (_target$current = target.current) === null || _target$current === void 0 ? void 0 : (_target$current$focus = _target$current.focus) === null || _target$current$focus === void 0 ? void 0 : _target$current$focus.call(_target$current);
168
162
  });
163
+ var focusItemValueOptions = {
164
+ data: dataWithCache,
165
+ valueKey: valueKey,
166
+ target: function target() {
167
+ return overlay.current;
168
+ }
169
+ };
169
170
 
170
171
  // Used to hover the focuse item when trigger `onKeydown`
171
- var _useFocusItemValue = useFocusItemValue(multi ? value === null || value === void 0 ? void 0 : value[0] : value, {
172
- data: getAllDataAndCache(),
173
- valueKey: valueKey,
174
- target: function target() {
175
- return overlay.current;
176
- }
177
- }),
172
+ var _useFocusItemValue = useFocusItemValue(multi ? value === null || value === void 0 ? void 0 : value[0] : value, focusItemValueOptions),
178
173
  focusItemValue = _useFocusItemValue.focusItemValue,
179
174
  setFocusItemValue = _useFocusItemValue.setFocusItemValue,
180
175
  onKeyDown = _useFocusItemValue.onKeyDown;
181
- var handleSearchCallback = useEventCallback(function (searchKeyword, filteredData, event) {
182
- var _filteredData$;
183
- // The first option after filtering is the focus.
184
- setFocusItemValue(disabledOptions ? searchKeyword : (filteredData === null || filteredData === void 0 ? void 0 : (_filteredData$ = filteredData[0]) === null || _filteredData$ === void 0 ? void 0 : _filteredData$[valueKey]) || searchKeyword);
176
+ var onSearchCallback = useEventCallback(function (searchKeyword, filteredData, event) {
177
+ if (!disabledOptions) {
178
+ var _filteredData$;
179
+ // The first option after filtering is the focus.
180
+ var firstItemValue = filteredData === null || filteredData === void 0 ? void 0 : (_filteredData$ = filteredData[0]) === null || _filteredData$ === void 0 ? void 0 : _filteredData$[valueKey];
181
+
182
+ // If there is no value in the option and new options are supported, the search keyword is the first option
183
+ if (!firstItemValue && creatable) {
184
+ firstItemValue = searchKeyword;
185
+ }
186
+ setFocusItemValue(firstItemValue);
187
+ }
185
188
  onSearch === null || onSearch === void 0 ? void 0 : onSearch(searchKeyword, event);
186
189
  });
190
+ var searchOptions = {
191
+ labelKey: labelKey,
192
+ searchBy: searchBy,
193
+ callback: onSearchCallback
194
+ };
187
195
 
188
196
  // Use search keywords to filter options.
189
- var _useSearch = useSearch(getAllData(), {
190
- labelKey: labelKey,
191
- searchBy: searchBy,
192
- callback: handleSearchCallback
193
- }),
197
+ var _useSearch = useSearch(data, searchOptions),
194
198
  searchKeyword = _useSearch.searchKeyword,
195
199
  resetSearch = _useSearch.resetSearch,
196
200
  checkShouldDisplay = _useSearch.checkShouldDisplay,
197
- handleSearch = _useSearch.handleSearch; // Update the state when the data in props changes
201
+ handleSearch = _useSearch.handleSearch; // Update the position of the menu when the search keyword and value change
198
202
  useEffect(function () {
199
- if (controlledData && !shallowEqual(controlledData, uncontrolledData)) {
200
- var _controlledData$;
201
- setData(controlledData);
202
- setNewData([]);
203
- setFocusItemValue(controlledData === null || controlledData === void 0 ? void 0 : (_controlledData$ = controlledData[0]) === null || _controlledData$ === void 0 ? void 0 : _controlledData$[valueKey]);
204
- }
205
- }, [setFocusItemValue, controlledData, uncontrolledData, valueKey]);
206
- useEffect(function () {
207
- var _triggerRef$current2;
208
- // In multiple selection, you need to set a maximum width for the input.
209
- if ((_triggerRef$current2 = triggerRef.current) !== null && _triggerRef$current2 !== void 0 && _triggerRef$current2.root) {
210
- setMaxWidth(getWidth(triggerRef.current.root));
211
- }
212
- // eslint-disable-next-line react-hooks/exhaustive-deps
213
- }, []);
214
-
215
- // Update the position of the menu when the search keyword and value change
216
- useEffect(function () {
217
- var _triggerRef$current3, _triggerRef$current3$;
218
- (_triggerRef$current3 = triggerRef.current) === null || _triggerRef$current3 === void 0 ? void 0 : (_triggerRef$current3$ = _triggerRef$current3.updatePosition) === null || _triggerRef$current3$ === void 0 ? void 0 : _triggerRef$current3$.call(_triggerRef$current3);
203
+ var _triggerRef$current2, _triggerRef$current2$;
204
+ (_triggerRef$current2 = triggerRef.current) === null || _triggerRef$current2 === void 0 ? void 0 : (_triggerRef$current2$ = _triggerRef$current2.updatePosition) === null || _triggerRef$current2$ === void 0 ? void 0 : _triggerRef$current2$.call(_triggerRef$current2);
219
205
  // eslint-disable-next-line react-hooks/exhaustive-deps
220
206
  }, [searchKeyword, value]);
221
207
  var getDataItem = function getDataItem(value) {
222
208
  // Find active `MenuItem` by `value`
223
- var allData = getAllDataAndCache();
224
- var activeItem = allData.find(function (item) {
209
+ var activeItem = dataWithCache.find(function (item) {
225
210
  return shallowEqual(item[valueKey], value);
226
211
  });
227
212
  var itemNode = placeholder;
@@ -234,33 +219,20 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
234
219
  itemNode: itemNode
235
220
  };
236
221
  };
237
- var getInput = function getInput() {
238
- var _inputRef$current;
239
- return multi ? (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.input : inputRef.current;
240
- };
241
- var focusInput = function focusInput() {
242
- var _getInput;
243
- return (_getInput = getInput()) === null || _getInput === void 0 ? void 0 : _getInput.focus();
244
- };
245
- var blurInput = function blurInput() {
246
- var _getInput2;
247
- return (_getInput2 = getInput()) === null || _getInput2 === void 0 ? void 0 : _getInput2.blur();
248
- };
249
222
 
250
223
  /**
251
224
  * Convert the string of the newly created option into an object.
252
225
  */
253
226
  var createOption = function createOption(value) {
254
- var _ref2;
227
+ var _option;
228
+ var option = (_option = {
229
+ create: true
230
+ }, _option[valueKey] = value, _option[labelKey] = value, _option);
255
231
  if (groupBy) {
256
- var _ref;
257
- return _ref = {
258
- create: true
259
- }, _ref[groupBy] = locale === null || locale === void 0 ? void 0 : locale.newItem, _ref[valueKey] = value, _ref[labelKey] = value, _ref;
232
+ var _extends2;
233
+ return _extends((_extends2 = {}, _extends2[groupBy] = locale === null || locale === void 0 ? void 0 : locale.newItem, _extends2), option);
260
234
  }
261
- return _ref2 = {
262
- create: true
263
- }, _ref2[valueKey] = value, _ref2[labelKey] = value, _ref2;
235
+ return option;
264
236
  };
265
237
  var handleChange = useEventCallback(function (value, event) {
266
238
  onChange === null || onChange === void 0 ? void 0 : onChange(value, event);
@@ -320,7 +292,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
320
292
  setFocusItemValue(nextItemValue);
321
293
  handleSelect(val, item, event);
322
294
  handleChange(val, event);
323
- focusInput();
295
+ focus();
324
296
  });
325
297
  var handleTagKeyPress = useEventCallback(function (event) {
326
298
  // When composing, ignore the keypress event.
@@ -328,31 +300,36 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
328
300
  return;
329
301
  }
330
302
  var val = cloneValue();
331
- var data = getAllData();
332
- if (!focusItemValue || !data) {
303
+ var newItemValue = focusItemValue || '';
304
+
305
+ // In TagInput
306
+ if (multi && disabledOptions) {
307
+ newItemValue = searchKeyword;
308
+ }
309
+ if (!newItemValue || !data) {
333
310
  return;
334
311
  }
335
312
 
336
313
  // If the value is disabled in this option, it is returned.
337
314
  if (disabledItemValues !== null && disabledItemValues !== void 0 && disabledItemValues.some(function (item) {
338
- return item === focusItemValue;
315
+ return item === newItemValue;
339
316
  })) {
340
317
  return;
341
318
  }
342
319
  if (!val.some(function (v) {
343
- return shallowEqual(v, focusItemValue);
320
+ return shallowEqual(v, newItemValue);
344
321
  })) {
345
- val.push(focusItemValue);
322
+ val.push(newItemValue);
346
323
  } else if (!disabledOptions) {
347
324
  remove(val, function (itemVal) {
348
- return shallowEqual(itemVal, focusItemValue);
325
+ return shallowEqual(itemVal, newItemValue);
349
326
  });
350
327
  }
351
328
  var focusItem = data.find(function (item) {
352
- return shallowEqual(item === null || item === void 0 ? void 0 : item[valueKey], focusItemValue);
329
+ return shallowEqual(item === null || item === void 0 ? void 0 : item[valueKey], newItemValue);
353
330
  });
354
331
  if (!focusItem) {
355
- focusItem = createOption(focusItemValue);
332
+ focusItem = createOption(newItemValue);
356
333
  }
357
334
  setValue(val);
358
335
  resetSearch();
@@ -372,8 +349,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
372
349
  }
373
350
 
374
351
  // Find active `MenuItem` by `value`
375
- var allData = getAllData();
376
- var focusItem = allData.find(function (item) {
352
+ var focusItem = data.find(function (item) {
377
353
  return shallowEqual(item[valueKey], focusItemValue);
378
354
  });
379
355
 
@@ -397,7 +373,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
397
373
  var removeLastItem = useEventCallback(function (event) {
398
374
  var target = event === null || event === void 0 ? void 0 : event.target;
399
375
  if ((target === null || target === void 0 ? void 0 : target.tagName) !== 'INPUT') {
400
- focusInput();
376
+ focus();
401
377
  return;
402
378
  }
403
379
  if ((target === null || target === void 0 ? void 0 : target.tagName) === 'INPUT' && target !== null && target !== void 0 && target.value) {
@@ -460,19 +436,20 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
460
436
  setFocusItemValue(multi ? value === null || value === void 0 ? void 0 : value[0] : value);
461
437
  resetSearch();
462
438
  });
463
- var handleFocus = useEventCallback(function () {
439
+ var handleFocus = useEventCallback(function (event) {
464
440
  if (!readOnly) {
465
- var _triggerRef$current4;
441
+ var _triggerRef$current3;
466
442
  setOpen(true);
467
- (_triggerRef$current4 = triggerRef.current) === null || _triggerRef$current4 === void 0 ? void 0 : _triggerRef$current4.open();
443
+ (_triggerRef$current3 = triggerRef.current) === null || _triggerRef$current3 === void 0 ? void 0 : _triggerRef$current3.open();
468
444
  }
445
+ onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
469
446
  });
470
447
  var handleEnter = useEventCallback(function () {
471
- focusInput();
448
+ focus();
472
449
  setOpen(true);
473
450
  });
474
451
  var handleExit = useEventCallback(function () {
475
- blurInput();
452
+ blur();
476
453
  setOpen(false);
477
454
  });
478
455
  var renderListItem = function renderListItem(label, item) {
@@ -543,7 +520,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
543
520
  left: left,
544
521
  top: top
545
522
  });
546
- var items = filterNodesOfTree(getAllData(), checkShouldDisplay);
523
+ var items = filterNodesOfTree(data, checkShouldDisplay);
547
524
  if (creatable && (typeof shouldDisplayCreateOption === 'function' ? shouldDisplayCreateOption(searchKeyword, items) : searchKeyword && !items.find(function (item) {
548
525
  return item[valueKey] === searchKeyword;
549
526
  }))) {
@@ -582,8 +559,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
582
559
  ,
583
560
  group: !isUndefined(groupBy),
584
561
  groupBy: groupBy,
585
- onSelect: multi ? handleCheckTag : handleSelectItem // fixme don't use any
586
- ,
562
+ onSelect: multi ? handleCheckTag : handleSelectItem,
587
563
  renderMenuGroup: renderMenuGroup,
588
564
  renderMenuItem: renderListItem,
589
565
  virtualized: virtualized
@@ -624,14 +600,6 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
624
600
  var classes = merge(pickerClasses, (_merge = {}, _merge[prefix(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["tag"])))] = multi, _merge[prefix((multi ? 'tag' : 'input') + "-" + size)] = size, _merge[prefix(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["focused"])))] = open, _merge[prefix(_templateObject4 || (_templateObject4 = _taggedTemplateLiteralLoose(["disabled-options"])))] = disabledOptions, _merge));
625
601
  var searching = !!searchKeyword && open;
626
602
  var editable = searchable && !disabled && !rest.loading;
627
- var inputProps = multi ? {
628
- inputStyle: {
629
- maxWidth: maxWidth - 63
630
- },
631
- as: InputAutosize
632
- } : {
633
- as: 'input'
634
- };
635
603
  if (plaintext) {
636
604
  var plaintextProps = {};
637
605
 
@@ -663,7 +631,7 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
663
631
  }, /*#__PURE__*/React.createElement(Component, {
664
632
  className: classes,
665
633
  style: style,
666
- onClick: focusInput,
634
+ onClick: focus,
667
635
  onKeyDown: onPickerKeyDown,
668
636
  ref: root
669
637
  }, /*#__PURE__*/React.createElement(PickerToggle, _extends({}, omit(rest, [].concat(omitTriggerPropKeys, usedClassNamePropKeys)), {
@@ -685,17 +653,17 @@ var InputPicker = /*#__PURE__*/React.forwardRef(function (props, ref) {
685
653
  size: size
686
654
  }), searching || multi && hasValue ? null : itemNode || placeholderNode), /*#__PURE__*/React.createElement(TextBox, {
687
655
  showTagList: hasValue && multi,
656
+ inputRef: inputRef,
657
+ inputValue: open ? searchKeyword : '',
658
+ inputProps: inputProps,
688
659
  tags: tagElements,
689
660
  editable: editable,
690
661
  readOnly: readOnly,
691
662
  disabled: disabled,
692
663
  multiple: multi,
693
664
  onBlur: onBlur,
694
- onFocus: createChainedFunction(handleFocus, onFocus),
695
- inputRef: inputRef,
696
- onChange: handleSearch,
697
- inputValue: open ? searchKeyword : '',
698
- inputProps: inputProps
665
+ onFocus: handleFocus,
666
+ onChange: handleSearch
699
667
  })));
700
668
  });
701
669
  InputPicker.displayName = 'InputPicker';
@@ -9,18 +9,26 @@ export interface TagOnlyProps {
9
9
  * Set the trigger for creating tags. only valid when creatable
10
10
  */
11
11
  trigger: TriggerType | TriggerType[];
12
- /** Callback fired when a tag is removed. */
12
+ /**
13
+ * Callback fired when a tag is removed.
14
+ */
13
15
  onTagRemove?: (tag: string, event: React.MouseEvent) => void;
14
16
  }
15
17
  export interface InputPickerContextProps extends TagOnlyProps {
16
- /** Multiple selections are allowed */
18
+ /**
19
+ * Multiple selections are allowed
20
+ */
17
21
  multi?: boolean;
18
22
  /**
19
23
  * No overlay provides options
20
24
  */
21
25
  disabledOptions?: boolean;
22
- /** Custom render checkbox on menu item */
26
+ /**
27
+ * Custom render checkbox on menu item
28
+ */
23
29
  renderCheckbox?: (checkboxProps: CheckboxProps) => React.ReactNode;
24
30
  }
25
31
  declare const InputPickerContext: React.Context<InputPickerContextProps>;
32
+ export declare function useTagContext(): InputPickerContextProps;
33
+ export declare const TagProvider: React.Provider<InputPickerContextProps>;
26
34
  export default InputPickerContext;
@@ -4,4 +4,8 @@ var InputPickerContext = /*#__PURE__*/React.createContext({
4
4
  tagProps: {},
5
5
  trigger: 'Enter'
6
6
  });
7
+ export function useTagContext() {
8
+ return React.useContext(InputPickerContext);
9
+ }
10
+ export var TagProvider = InputPickerContext.Provider;
7
11
  export default InputPickerContext;
@@ -0,0 +1,17 @@
1
+ /// <reference types="react" />
2
+ import type { ItemDataType } from '../../@types/common';
3
+ export interface InputItemDataType<T = number | string> extends ItemDataType<T> {
4
+ create?: boolean;
5
+ }
6
+ interface UseDataProps {
7
+ controlledData?: InputItemDataType[];
8
+ cacheData?: InputItemDataType[];
9
+ onChange?: (data: ItemDataType[]) => void;
10
+ }
11
+ declare function useData(props: UseDataProps): {
12
+ data: ItemDataType<string | number>[];
13
+ dataWithCache: ItemDataType<string | number>[];
14
+ newData: InputItemDataType<string | number>[];
15
+ setNewData: import("react").Dispatch<import("react").SetStateAction<InputItemDataType<string | number>[]>>;
16
+ };
17
+ export default useData;
@@ -0,0 +1,38 @@
1
+ 'use client';
2
+ import { useState, useEffect, useMemo } from 'react';
3
+ import shallowEqual from '../../utils/shallowEqual';
4
+ function useData(props) {
5
+ var _props$controlledData = props.controlledData,
6
+ controlledData = _props$controlledData === void 0 ? [] : _props$controlledData,
7
+ _props$cacheData = props.cacheData,
8
+ cacheData = _props$cacheData === void 0 ? [] : _props$cacheData,
9
+ onChange = props.onChange;
10
+ var _useState = useState(controlledData),
11
+ uncontrolledData = _useState[0],
12
+ setData = _useState[1];
13
+ var _useState2 = useState([]),
14
+ newData = _useState2[0],
15
+ setNewData = _useState2[1];
16
+ var data = useMemo(function () {
17
+ return [].concat(uncontrolledData, newData);
18
+ }, [newData, uncontrolledData]);
19
+ var dataWithCache = useMemo(function () {
20
+ return [].concat(data, cacheData);
21
+ }, [data, cacheData]);
22
+
23
+ // Update the state when the data in props changes
24
+ useEffect(function () {
25
+ if (controlledData && !shallowEqual(controlledData, uncontrolledData)) {
26
+ setData(controlledData);
27
+ setNewData([]);
28
+ onChange === null || onChange === void 0 ? void 0 : onChange(controlledData);
29
+ }
30
+ }, [controlledData, uncontrolledData, onChange]);
31
+ return {
32
+ data: data,
33
+ dataWithCache: dataWithCache,
34
+ newData: newData,
35
+ setNewData: setNewData
36
+ };
37
+ }
38
+ export default useData;
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import type { OverlayTriggerHandle } from '../../internals/Picker/PickerToggleTrigger';
3
+ interface InputProps {
4
+ multi?: boolean;
5
+ triggerRef: React.RefObject<OverlayTriggerHandle>;
6
+ }
7
+ declare function useInput(props: InputProps): {
8
+ inputProps: {
9
+ inputStyle: {
10
+ maxWidth: number;
11
+ };
12
+ as: import("react").ForwardRefExoticComponent<import("../InputAutosize").InputAutosizeProps & import("react").RefAttributes<import("../InputAutosize").InputInstance>>;
13
+ } | {
14
+ as: string;
15
+ inputStyle?: undefined;
16
+ };
17
+ inputRef: import("react").MutableRefObject<any>;
18
+ focus: () => void;
19
+ blur: () => void;
20
+ };
21
+ export default useInput;
@@ -0,0 +1,40 @@
1
+ 'use client';
2
+ import { useRef, useCallback, useMemo } from 'react';
3
+ import useMaxWidth from './useMaxWidth';
4
+ import InputAutosize from '../InputAutosize';
5
+ var INPUT_MARGIN_RIGHT = 60;
6
+ function useInput(props) {
7
+ var multi = props.multi,
8
+ triggerRef = props.triggerRef;
9
+ var inputRef = useRef();
10
+ var maxWidth = useMaxWidth(triggerRef);
11
+ var getInput = useCallback(function () {
12
+ var _inputRef$current;
13
+ return multi ? (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.input : inputRef.current;
14
+ }, [multi]);
15
+ var focus = useCallback(function () {
16
+ var _getInput;
17
+ (_getInput = getInput()) === null || _getInput === void 0 ? void 0 : _getInput.focus();
18
+ }, [getInput]);
19
+ var blur = useCallback(function () {
20
+ var _getInput2;
21
+ (_getInput2 = getInput()) === null || _getInput2 === void 0 ? void 0 : _getInput2.blur();
22
+ }, [getInput]);
23
+ var inputProps = useMemo(function () {
24
+ return multi ? {
25
+ inputStyle: {
26
+ maxWidth: maxWidth - INPUT_MARGIN_RIGHT
27
+ },
28
+ as: InputAutosize
29
+ } : {
30
+ as: 'input'
31
+ };
32
+ }, [maxWidth, multi]);
33
+ return {
34
+ inputProps: inputProps,
35
+ inputRef: inputRef,
36
+ focus: focus,
37
+ blur: blur
38
+ };
39
+ }
40
+ export default useInput;
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ import type { OverlayTriggerHandle } from '../../internals/Picker/PickerToggleTrigger';
3
+ declare function useMaxWidth(triggerRef: React.RefObject<OverlayTriggerHandle>): number;
4
+ export default useMaxWidth;
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+ import { useState, useEffect } from 'react';
3
+ import getWidth from 'dom-lib/getWidth';
4
+ function useMaxWidth(triggerRef) {
5
+ var _useState = useState(100),
6
+ maxWidth = _useState[0],
7
+ setMaxWidth = _useState[1];
8
+ useEffect(function () {
9
+ var _triggerRef$current;
10
+ // In multiple selection, you need to set a maximum width for the input.
11
+ if ((_triggerRef$current = triggerRef.current) !== null && _triggerRef$current !== void 0 && _triggerRef$current.root) {
12
+ var _triggerRef$current2;
13
+ setMaxWidth(getWidth((_triggerRef$current2 = triggerRef.current) === null || _triggerRef$current2 === void 0 ? void 0 : _triggerRef$current2.root));
14
+ }
15
+ // eslint-disable-next-line react-hooks/exhaustive-deps
16
+ }, []);
17
+ return maxWidth;
18
+ }
19
+ export default useMaxWidth;
@@ -1,3 +1,4 @@
1
1
  import InputPicker from './InputPicker';
2
- export type { InputPickerProps, InputItemDataType } from './InputPicker';
2
+ export type { InputItemDataType } from './hooks/useData';
3
+ export type { InputPickerProps } from './InputPicker';
3
4
  export default InputPicker;
@@ -0,0 +1 @@
1
+ export declare const convertSize: (size?: string) => "lg" | "md" | "sm";
@@ -0,0 +1,12 @@
1
+ 'use client';
2
+ export var convertSize = function convertSize(size) {
3
+ switch (size) {
4
+ case 'lg':
5
+ return 'lg';
6
+ case 'sm':
7
+ case 'xs':
8
+ return 'sm';
9
+ default:
10
+ return 'md';
11
+ }
12
+ };