1 | import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
|
2 | import _toConsumableArray from '@babel/runtime/helpers/esm/toConsumableArray';
|
3 | import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
4 | import { useMemo, useCallback } from 'react';
|
5 | import { H as cleanValue, D as valueTernary } from './index-a301f526.esm.js';
|
6 | import { g as getOptionValue, b as getOptionLabel } from './Select-49a62830.esm.js';
|
7 |
|
8 | var _excluded = ["allowCreateWhileLoading", "createOptionPosition", "formatCreateLabel", "isValidNewOption", "getNewOptionData", "onCreateOption", "options", "onChange"];
|
9 | var compareOption = function compareOption() {
|
10 | var inputValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
11 | var option = arguments.length > 1 ? arguments[1] : undefined;
|
12 | var accessors = arguments.length > 2 ? arguments[2] : undefined;
|
13 | var candidate = String(inputValue).toLowerCase();
|
14 | var optionValue = String(accessors.getOptionValue(option)).toLowerCase();
|
15 | var optionLabel = String(accessors.getOptionLabel(option)).toLowerCase();
|
16 | return optionValue === candidate || optionLabel === candidate;
|
17 | };
|
18 | var builtins = {
|
19 | formatCreateLabel: function formatCreateLabel(inputValue) {
|
20 | return "Create \"".concat(inputValue, "\"");
|
21 | },
|
22 | isValidNewOption: function isValidNewOption(inputValue, selectValue, selectOptions, accessors) {
|
23 | return !(!inputValue || selectValue.some(function (option) {
|
24 | return compareOption(inputValue, option, accessors);
|
25 | }) || selectOptions.some(function (option) {
|
26 | return compareOption(inputValue, option, accessors);
|
27 | }));
|
28 | },
|
29 | getNewOptionData: function getNewOptionData(inputValue, optionLabel) {
|
30 | return {
|
31 | label: optionLabel,
|
32 | value: inputValue,
|
33 | __isNew__: true
|
34 | };
|
35 | }
|
36 | };
|
37 | function useCreatable(_ref) {
|
38 | var _ref$allowCreateWhile = _ref.allowCreateWhileLoading,
|
39 | allowCreateWhileLoading = _ref$allowCreateWhile === void 0 ? false : _ref$allowCreateWhile,
|
40 | _ref$createOptionPosi = _ref.createOptionPosition,
|
41 | createOptionPosition = _ref$createOptionPosi === void 0 ? 'last' : _ref$createOptionPosi,
|
42 | _ref$formatCreateLabe = _ref.formatCreateLabel,
|
43 | formatCreateLabel = _ref$formatCreateLabe === void 0 ? builtins.formatCreateLabel : _ref$formatCreateLabe,
|
44 | _ref$isValidNewOption = _ref.isValidNewOption,
|
45 | isValidNewOption = _ref$isValidNewOption === void 0 ? builtins.isValidNewOption : _ref$isValidNewOption,
|
46 | _ref$getNewOptionData = _ref.getNewOptionData,
|
47 | getNewOptionData = _ref$getNewOptionData === void 0 ? builtins.getNewOptionData : _ref$getNewOptionData,
|
48 | onCreateOption = _ref.onCreateOption,
|
49 | _ref$options = _ref.options,
|
50 | propsOptions = _ref$options === void 0 ? [] : _ref$options,
|
51 | propsOnChange = _ref.onChange,
|
52 | restSelectProps = _objectWithoutProperties(_ref, _excluded);
|
53 | var _restSelectProps$getO = restSelectProps.getOptionValue,
|
54 | getOptionValue$1 = _restSelectProps$getO === void 0 ? getOptionValue : _restSelectProps$getO,
|
55 | _restSelectProps$getO2 = restSelectProps.getOptionLabel,
|
56 | getOptionLabel$1 = _restSelectProps$getO2 === void 0 ? getOptionLabel : _restSelectProps$getO2,
|
57 | inputValue = restSelectProps.inputValue,
|
58 | isLoading = restSelectProps.isLoading,
|
59 | isMulti = restSelectProps.isMulti,
|
60 | value = restSelectProps.value,
|
61 | name = restSelectProps.name;
|
62 | var newOption = useMemo(function () {
|
63 | return isValidNewOption(inputValue, cleanValue(value), propsOptions, {
|
64 | getOptionValue: getOptionValue$1,
|
65 | getOptionLabel: getOptionLabel$1
|
66 | }) ? getNewOptionData(inputValue, formatCreateLabel(inputValue)) : undefined;
|
67 | }, [formatCreateLabel, getNewOptionData, getOptionLabel$1, getOptionValue$1, inputValue, isValidNewOption, propsOptions, value]);
|
68 | var options = useMemo(function () {
|
69 | return (allowCreateWhileLoading || !isLoading) && newOption ? createOptionPosition === 'first' ? [newOption].concat(_toConsumableArray(propsOptions)) : [].concat(_toConsumableArray(propsOptions), [newOption]) : propsOptions;
|
70 | }, [allowCreateWhileLoading, createOptionPosition, isLoading, newOption, propsOptions]);
|
71 | var onChange = useCallback(function (newValue, actionMeta) {
|
72 | if (actionMeta.action !== 'select-option') {
|
73 | return propsOnChange(newValue, actionMeta);
|
74 | }
|
75 | var valueArray = Array.isArray(newValue) ? newValue : [newValue];
|
76 | if (valueArray[valueArray.length - 1] === newOption) {
|
77 | if (onCreateOption) onCreateOption(inputValue);else {
|
78 | var newOptionData = getNewOptionData(inputValue, inputValue);
|
79 | var newActionMeta = {
|
80 | action: 'create-option',
|
81 | name: name,
|
82 | option: newOptionData
|
83 | };
|
84 | propsOnChange(valueTernary(isMulti, [].concat(_toConsumableArray(cleanValue(value)), [newOptionData]), newOptionData), newActionMeta);
|
85 | }
|
86 | return;
|
87 | }
|
88 | propsOnChange(newValue, actionMeta);
|
89 | }, [getNewOptionData, inputValue, isMulti, name, newOption, onCreateOption, propsOnChange, value]);
|
90 | return _objectSpread(_objectSpread({}, restSelectProps), {}, {
|
91 | options: options,
|
92 | onChange: onChange
|
93 | });
|
94 | }
|
95 |
|
96 | export { useCreatable as u };
|