@react-fabric/form 1.0.0-beta.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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/builder/FileField.js +131 -0
- package/dist/cjs/builder/FormField.js +119 -0
- package/dist/cjs/form/Controller.js +64 -0
- package/dist/cjs/form/Form.js +128 -0
- package/dist/cjs/hooks/useFileUploader.js +204 -0
- package/dist/cjs/hooks/useFormBuilder.js +87 -0
- package/dist/cjs/i18n/ar.json.js +37 -0
- package/dist/cjs/i18n/en.json.js +38 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/input/ArrayInput.js +189 -0
- package/dist/cjs/input/ArrayInput.module.css +1 -0
- package/dist/cjs/input/AvatarInput.js +141 -0
- package/dist/cjs/input/Checkbox.js +83 -0
- package/dist/cjs/input/ColorInput.js +100 -0
- package/dist/cjs/input/ColorPicker.js +122 -0
- package/dist/cjs/input/ColorPicker.module.css +1 -0
- package/dist/cjs/input/Field.js +31 -0
- package/dist/cjs/input/File.js +165 -0
- package/dist/cjs/input/File.module.css +1 -0
- package/dist/cjs/input/Hidden.js +54 -0
- package/dist/cjs/input/Input.js +79 -0
- package/dist/cjs/input/Number.js +80 -0
- package/dist/cjs/input/Password.js +104 -0
- package/dist/cjs/input/Radio.js +70 -0
- package/dist/cjs/input/RadioGroup.js +31 -0
- package/dist/cjs/input/Range.js +169 -0
- package/dist/cjs/input/Search.js +109 -0
- package/dist/cjs/input/Slider.js +172 -0
- package/dist/cjs/input/Switch.js +79 -0
- package/dist/cjs/input/Textarea.js +87 -0
- package/dist/cjs/internal/ErrorIcon.js +32 -0
- package/dist/cjs/internal/Field.module.css +1 -0
- package/dist/cjs/internal/FieldWrapper.js +78 -0
- package/dist/cjs/internal/InputWrapper.js +113 -0
- package/dist/cjs/ranger/index.js +352 -0
- package/dist/cjs/ranger/utils.js +49 -0
- package/dist/cjs/select/AutoComplete.js +268 -0
- package/dist/cjs/select/DualList.js +238 -0
- package/dist/cjs/select/List.js +241 -0
- package/dist/cjs/select/Options.js +76 -0
- package/dist/cjs/select/Select.js +283 -0
- package/dist/cjs/select/helpers.js +41 -0
- package/dist/cjs/select/useSelect.js +337 -0
- package/dist/cjs/utils/eventHandlers.js +50 -0
- package/dist/cjs/utils/getColor.js +40 -0
- package/dist/esm/builder/FileField.js +131 -0
- package/dist/esm/builder/FormField.js +119 -0
- package/dist/esm/form/Controller.js +64 -0
- package/dist/esm/form/Form.js +128 -0
- package/dist/esm/hooks/useFileUploader.js +204 -0
- package/dist/esm/hooks/useFormBuilder.js +87 -0
- package/dist/esm/i18n/ar.json.js +31 -0
- package/dist/esm/i18n/en.json.js +32 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/input/ArrayInput.js +189 -0
- package/dist/esm/input/ArrayInput.module.css +1 -0
- package/dist/esm/input/AvatarInput.js +141 -0
- package/dist/esm/input/Checkbox.js +83 -0
- package/dist/esm/input/ColorInput.js +100 -0
- package/dist/esm/input/ColorPicker.js +122 -0
- package/dist/esm/input/ColorPicker.module.css +1 -0
- package/dist/esm/input/Field.js +31 -0
- package/dist/esm/input/File.js +165 -0
- package/dist/esm/input/File.module.css +1 -0
- package/dist/esm/input/Hidden.js +54 -0
- package/dist/esm/input/Input.js +79 -0
- package/dist/esm/input/Number.js +80 -0
- package/dist/esm/input/Password.js +104 -0
- package/dist/esm/input/Radio.js +70 -0
- package/dist/esm/input/RadioGroup.js +31 -0
- package/dist/esm/input/Range.js +169 -0
- package/dist/esm/input/Search.js +109 -0
- package/dist/esm/input/Slider.js +172 -0
- package/dist/esm/input/Switch.js +79 -0
- package/dist/esm/input/Textarea.js +87 -0
- package/dist/esm/internal/ErrorIcon.js +32 -0
- package/dist/esm/internal/Field.module.css +1 -0
- package/dist/esm/internal/FieldWrapper.js +78 -0
- package/dist/esm/internal/InputWrapper.js +113 -0
- package/dist/esm/ranger/index.js +352 -0
- package/dist/esm/ranger/utils.js +47 -0
- package/dist/esm/select/AutoComplete.js +268 -0
- package/dist/esm/select/DualList.js +238 -0
- package/dist/esm/select/List.js +241 -0
- package/dist/esm/select/Options.js +76 -0
- package/dist/esm/select/Select.js +283 -0
- package/dist/esm/select/helpers.js +40 -0
- package/dist/esm/select/useSelect.js +337 -0
- package/dist/esm/utils/eventHandlers.js +50 -0
- package/dist/esm/utils/getColor.js +39 -0
- package/dist/types/builder/FileField.d.ts +8 -0
- package/dist/types/builder/FormField.d.ts +9 -0
- package/dist/types/form/Controller.d.ts +20 -0
- package/dist/types/form/Form.d.ts +33 -0
- package/dist/types/hooks/useFileUploader.d.ts +23 -0
- package/dist/types/hooks/useFormBuilder.d.ts +16 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/input/ArrayInput.d.ts +64 -0
- package/dist/types/input/AvatarInput.d.ts +31 -0
- package/dist/types/input/Checkbox.d.ts +46 -0
- package/dist/types/input/ColorInput.d.ts +10 -0
- package/dist/types/input/ColorPicker.d.ts +24 -0
- package/dist/types/input/Field.d.ts +41 -0
- package/dist/types/input/File.d.ts +17 -0
- package/dist/types/input/Hidden.d.ts +16 -0
- package/dist/types/input/Input.d.ts +9 -0
- package/dist/types/input/Number.d.ts +17 -0
- package/dist/types/input/Password.d.ts +13 -0
- package/dist/types/input/Radio.d.ts +42 -0
- package/dist/types/input/RadioGroup.d.ts +30 -0
- package/dist/types/input/Range.d.ts +59 -0
- package/dist/types/input/Search.d.ts +17 -0
- package/dist/types/input/Slider.d.ts +63 -0
- package/dist/types/input/Switch.d.ts +42 -0
- package/dist/types/input/Textarea.d.ts +17 -0
- package/dist/types/internal/ErrorIcon.d.ts +1 -0
- package/dist/types/internal/FieldWrapper.d.ts +1 -0
- package/dist/types/internal/InputWrapper.d.ts +1 -0
- package/dist/types/ranger/index.d.ts +70 -0
- package/dist/types/ranger/utils.d.ts +25 -0
- package/dist/types/select/AutoComplete.d.ts +17 -0
- package/dist/types/select/DualList.d.ts +38 -0
- package/dist/types/select/List.d.ts +4 -0
- package/dist/types/select/Options.d.ts +14 -0
- package/dist/types/select/Select.d.ts +2 -0
- package/dist/types/select/helpers.d.ts +2 -0
- package/dist/types/select/useSelect.d.ts +50 -0
- package/dist/types/types/index.d.ts +169 -0
- package/dist/types/types/schema.d.ts +59 -0
- package/dist/types/utils/eventHandlers.d.ts +11 -0
- package/dist/types/utils/getColor.d.ts +4 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/package.json +64 -0
- package/src/builder/FileField.tsx +122 -0
- package/src/builder/FormField.tsx +161 -0
- package/src/form/Controller.tsx +103 -0
- package/src/form/Form.tsx +160 -0
- package/src/hooks/useFileUploader.ts +234 -0
- package/src/hooks/useFormBuilder.tsx +128 -0
- package/src/i18n/ar.json +23 -0
- package/src/i18n/en.json +24 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +54 -0
- package/src/input/ArrayInput.module.css +41 -0
- package/src/input/ArrayInput.tsx +318 -0
- package/src/input/AvatarInput.tsx +150 -0
- package/src/input/Checkbox.tsx +165 -0
- package/src/input/ColorInput.tsx +157 -0
- package/src/input/ColorPicker.module.css +51 -0
- package/src/input/ColorPicker.tsx +210 -0
- package/src/input/Field.tsx +96 -0
- package/src/input/File.module.css +42 -0
- package/src/input/File.tsx +224 -0
- package/src/input/Hidden.tsx +65 -0
- package/src/input/Input.tsx +114 -0
- package/src/input/Number.tsx +126 -0
- package/src/input/Password.tsx +150 -0
- package/src/input/Radio.tsx +146 -0
- package/src/input/RadioGroup.tsx +75 -0
- package/src/input/Range.tsx +276 -0
- package/src/input/Search.tsx +160 -0
- package/src/input/Slider.tsx +282 -0
- package/src/input/Switch.tsx +168 -0
- package/src/input/Textarea.tsx +127 -0
- package/src/internal/ErrorIcon.tsx +61 -0
- package/src/internal/Field.module.css +139 -0
- package/src/internal/FieldWrapper.tsx +84 -0
- package/src/internal/InputWrapper.tsx +187 -0
- package/src/ranger/index.tsx +514 -0
- package/src/ranger/utils.ts +48 -0
- package/src/select/AutoComplete.tsx +354 -0
- package/src/select/DualList.tsx +286 -0
- package/src/select/List.tsx +288 -0
- package/src/select/Options.tsx +121 -0
- package/src/select/Select.tsx +350 -0
- package/src/select/helpers.ts +36 -0
- package/src/select/useSelect.tsx +413 -0
- package/src/types/index.ts +203 -0
- package/src/types/schema.ts +100 -0
- package/src/utils/eventHandlers.ts +72 -0
- package/src/utils/getColor.ts +47 -0
- package/src/utils/index.ts +25 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
4
|
+
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
|
5
|
+
var react$1 = require('@floating-ui/react');
|
|
6
|
+
var core = require('@react-fabric/core');
|
|
7
|
+
var utilities = require('@react-fabric/utilities');
|
|
8
|
+
var classNames = require('classnames');
|
|
9
|
+
var react = require('react');
|
|
10
|
+
var InputWrapper = require('../internal/InputWrapper.js');
|
|
11
|
+
var Options = require('./Options.js');
|
|
12
|
+
var useSelect = require('./useSelect.js');
|
|
13
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
14
|
+
|
|
15
|
+
var _excluded = ["ref", "name", "value", "invalid", "readOnly", "disabled", "required", "placeholder", "autoFocus", "error", "options", "groupProperty", "labelProperty", "valueProperty", "multiple", "searchable", "allowCreate", "createOption", "matcher", "renderer", "onQuery", "onChange", "onSelect", "onEnterPressed"];
|
|
16
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
17
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
18
|
+
var Select = function Select(_ref) {
|
|
19
|
+
var _state$query, _refs$reference$curre;
|
|
20
|
+
var ref = _ref.ref,
|
|
21
|
+
name = _ref.name,
|
|
22
|
+
value = _ref.value,
|
|
23
|
+
invalid = _ref.invalid,
|
|
24
|
+
readOnly = _ref.readOnly,
|
|
25
|
+
disabled = _ref.disabled,
|
|
26
|
+
required = _ref.required,
|
|
27
|
+
placeholder = _ref.placeholder,
|
|
28
|
+
autoFocus = _ref.autoFocus,
|
|
29
|
+
error = _ref.error,
|
|
30
|
+
options = _ref.options,
|
|
31
|
+
groupProperty = _ref.groupProperty,
|
|
32
|
+
_ref$labelProperty = _ref.labelProperty,
|
|
33
|
+
labelProperty = _ref$labelProperty === void 0 ? "label" : _ref$labelProperty,
|
|
34
|
+
_ref$valueProperty = _ref.valueProperty,
|
|
35
|
+
valueProperty = _ref$valueProperty === void 0 ? "id" : _ref$valueProperty,
|
|
36
|
+
multiple = _ref.multiple,
|
|
37
|
+
searchable = _ref.searchable,
|
|
38
|
+
allowCreate = _ref.allowCreate,
|
|
39
|
+
createOption = _ref.createOption,
|
|
40
|
+
matcher = _ref.matcher,
|
|
41
|
+
renderer = _ref.renderer,
|
|
42
|
+
onQuery = _ref.onQuery,
|
|
43
|
+
onChange = _ref.onChange,
|
|
44
|
+
onSelect = _ref.onSelect,
|
|
45
|
+
onEnterPressed = _ref.onEnterPressed,
|
|
46
|
+
rest = _objectWithoutProperties(_ref, _excluded);
|
|
47
|
+
var _useSelect = useSelect.useSelect({
|
|
48
|
+
value: value,
|
|
49
|
+
options: options,
|
|
50
|
+
multiple: multiple,
|
|
51
|
+
groupProperty: groupProperty,
|
|
52
|
+
labelProperty: labelProperty,
|
|
53
|
+
valueProperty: valueProperty,
|
|
54
|
+
allowCreate: allowCreate,
|
|
55
|
+
createOption: createOption,
|
|
56
|
+
matcher: matcher,
|
|
57
|
+
onChange: onChange,
|
|
58
|
+
onSelect: onSelect,
|
|
59
|
+
onQuery: onQuery
|
|
60
|
+
}),
|
|
61
|
+
state = _useSelect.state,
|
|
62
|
+
listRef = _useSelect.listRef,
|
|
63
|
+
listContentRef = _useSelect.listContentRef,
|
|
64
|
+
setOpen = _useSelect.setOpen,
|
|
65
|
+
macthOption = _useSelect.macthOption,
|
|
66
|
+
handleChange = _useSelect.handleChange,
|
|
67
|
+
handleRemove = _useSelect.handleRemove,
|
|
68
|
+
handleQuery = _useSelect.handleQuery,
|
|
69
|
+
setItemRef = _useSelect.setItemRef,
|
|
70
|
+
setActiveIndex = _useSelect.setActiveIndex;
|
|
71
|
+
var dropdownKey = react.useId();
|
|
72
|
+
var _useFloating = react$1.useFloating({
|
|
73
|
+
open: state.open,
|
|
74
|
+
onOpenChange: function onOpenChange(open, evt, reason) {
|
|
75
|
+
!open && (listRef.current = []);
|
|
76
|
+
setOpen(open);
|
|
77
|
+
},
|
|
78
|
+
strategy: "fixed",
|
|
79
|
+
placement: "bottom",
|
|
80
|
+
whileElementsMounted: react$1.autoUpdate,
|
|
81
|
+
middleware: [react$1.shift({
|
|
82
|
+
padding: 8
|
|
83
|
+
}), react$1.flip(), react$1.offset(2)]
|
|
84
|
+
}),
|
|
85
|
+
context = _useFloating.context,
|
|
86
|
+
floatingStyles = _useFloating.floatingStyles,
|
|
87
|
+
refs = _useFloating.refs;
|
|
88
|
+
|
|
89
|
+
// floating interactions
|
|
90
|
+
var click = react$1.useClick(context, {});
|
|
91
|
+
var dismiss = react$1.useDismiss(context, {
|
|
92
|
+
// referencePress: true,
|
|
93
|
+
});
|
|
94
|
+
var navigation = react$1.useListNavigation(context, {
|
|
95
|
+
listRef: listRef,
|
|
96
|
+
activeIndex: state.activeIndex,
|
|
97
|
+
selectedIndex: state.selectedIndex,
|
|
98
|
+
loop: true,
|
|
99
|
+
virtual: true,
|
|
100
|
+
focusItemOnOpen: false,
|
|
101
|
+
onNavigate: setActiveIndex
|
|
102
|
+
});
|
|
103
|
+
var typeahead = react$1.useTypeahead(context, {
|
|
104
|
+
listRef: listContentRef,
|
|
105
|
+
activeIndex: state.activeIndex,
|
|
106
|
+
selectedIndex: state.selectedIndex,
|
|
107
|
+
findMatch: function findMatch(list, typedString) {
|
|
108
|
+
return list.find(function (item) {
|
|
109
|
+
return macthOption(item, typedString);
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
onMatch: function onMatch(index) {
|
|
113
|
+
state.open && setActiveIndex(index);
|
|
114
|
+
!searchable && !multiple && !state.open && handleChange(listContentRef.current[index]);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
var _useInteractions = react$1.useInteractions([dismiss, searchable ? undefined : click, navigation, typeahead]),
|
|
118
|
+
getReferenceProps = _useInteractions.getReferenceProps,
|
|
119
|
+
getFloatingProps = _useInteractions.getFloatingProps,
|
|
120
|
+
getItemProps = _useInteractions.getItemProps;
|
|
121
|
+
var referenceProps = react.useMemo(function () {
|
|
122
|
+
var props = getReferenceProps();
|
|
123
|
+
return _objectSpread(_objectSpread({}, props), {}, {
|
|
124
|
+
onBlur: function onBlur(evt) {
|
|
125
|
+
var _evt$relatedTarget;
|
|
126
|
+
if ((_evt$relatedTarget = evt.relatedTarget) !== null && _evt$relatedTarget !== void 0 && _evt$relatedTarget.closest("[data-select-dropdown=\"".concat(dropdownKey, "\"]"))) {
|
|
127
|
+
var _refs$domReference$cu;
|
|
128
|
+
(_refs$domReference$cu = refs.domReference.current) === null || _refs$domReference$cu === void 0 || (_refs$domReference$cu = _refs$domReference$cu.querySelector("input")) === null || _refs$domReference$cu === void 0 || _refs$domReference$cu.focus();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
handleQuery("");
|
|
132
|
+
setOpen(false);
|
|
133
|
+
},
|
|
134
|
+
onKeyDown: function onKeyDown(evt) {
|
|
135
|
+
if (evt.key === "Enter") {
|
|
136
|
+
if (state.activeIndex !== null) {
|
|
137
|
+
handleChange(listContentRef.current[state.activeIndex]);
|
|
138
|
+
evt.preventDefault();
|
|
139
|
+
evt.stopPropagation();
|
|
140
|
+
return false;
|
|
141
|
+
} else if (state.query) {
|
|
142
|
+
handleQuery("");
|
|
143
|
+
} else {
|
|
144
|
+
onEnterPressed === null || onEnterPressed === void 0 || onEnterPressed(evt);
|
|
145
|
+
}
|
|
146
|
+
} else if (evt.key === "Tab" && state.activeIndex !== null) {
|
|
147
|
+
handleChange(listContentRef.current[state.activeIndex]);
|
|
148
|
+
} else if (evt.key === "Backspace") {
|
|
149
|
+
!state.query && handleRemove();
|
|
150
|
+
} else if (!searchable || ["ArrowUp", "ArrowDown"].includes(evt.key)) {
|
|
151
|
+
if (["ArrowUp", "ArrowDown"].includes(evt.key) && state.activeIndex === null) {
|
|
152
|
+
setActiveIndex(state.selectedIndex);
|
|
153
|
+
}
|
|
154
|
+
setTimeout(function () {
|
|
155
|
+
var _props$onKeyDown;
|
|
156
|
+
(_props$onKeyDown = props.onKeyDown) === null || _props$onKeyDown === void 0 || _props$onKeyDown.call(props, evt);
|
|
157
|
+
}, 50);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}, [getReferenceProps, onEnterPressed, handleQuery, refs, state.query, state.activeIndex]);
|
|
162
|
+
var floatingProps = react.useMemo(function () {
|
|
163
|
+
return getFloatingProps({
|
|
164
|
+
"data-select-dropdown": dropdownKey
|
|
165
|
+
});
|
|
166
|
+
}, [getFloatingProps]);
|
|
167
|
+
var makeItemProps = react.useCallback(function (item) {
|
|
168
|
+
var _state$value, _state$value$includes;
|
|
169
|
+
var selected = utilities.isArray(state.value) ? (_state$value = state.value) === null || _state$value === void 0 || (_state$value$includes = _state$value.includes) === null || _state$value$includes === void 0 ? void 0 : _state$value$includes.call(_state$value, item) : state.value === item;
|
|
170
|
+
return getItemProps({
|
|
171
|
+
"data-selected": selected ? true : undefined,
|
|
172
|
+
onClick: function onClick() {
|
|
173
|
+
handleChange(item);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}, [getItemProps, handleChange, state.value]);
|
|
177
|
+
var displayValue = react.useMemo(function () {
|
|
178
|
+
if (utilities.isArray(state.value)) {
|
|
179
|
+
return state.value.map(function (item, index) {
|
|
180
|
+
var _ref2, _renderer;
|
|
181
|
+
return /*#__PURE__*/jsxRuntime.jsx(core.Chip, {
|
|
182
|
+
onRemove: function onRemove() {
|
|
183
|
+
return handleRemove(index);
|
|
184
|
+
},
|
|
185
|
+
children: (_ref2 = (_renderer = renderer === null || renderer === void 0 ? void 0 : renderer(item)) !== null && _renderer !== void 0 ? _renderer : item[labelProperty]) !== null && _ref2 !== void 0 ? _ref2 : item
|
|
186
|
+
}, index);
|
|
187
|
+
});
|
|
188
|
+
} else if (!utilities.isEmpty(state.value) && !state.query) {
|
|
189
|
+
var _ref3, _renderer2;
|
|
190
|
+
return /*#__PURE__*/jsxRuntime.jsx("span", {
|
|
191
|
+
className: "self-center pointer-events-none select-none",
|
|
192
|
+
children: (_ref3 = (_renderer2 = renderer === null || renderer === void 0 ? void 0 : renderer(state.value)) !== null && _renderer2 !== void 0 ? _renderer2 : state.value[labelProperty]) !== null && _ref3 !== void 0 ? _ref3 : state.value
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}, [state.value, state.query, placeholder, renderer]);
|
|
196
|
+
var openDropdown = react.useCallback(function () {
|
|
197
|
+
var _refs$domReference$cu2;
|
|
198
|
+
setOpen(true);
|
|
199
|
+
(_refs$domReference$cu2 = refs.domReference.current) === null || _refs$domReference$cu2 === void 0 || (_refs$domReference$cu2 = _refs$domReference$cu2.querySelector("input")) === null || _refs$domReference$cu2 === void 0 || _refs$domReference$cu2.focus();
|
|
200
|
+
}, []);
|
|
201
|
+
react.useLayoutEffect(function () {
|
|
202
|
+
state.open && setTimeout(function () {
|
|
203
|
+
var _listRef$current$stat;
|
|
204
|
+
state.selectedIndex && ((_listRef$current$stat = listRef.current[state.selectedIndex]) === null || _listRef$current$stat === void 0 ? void 0 : _listRef$current$stat.scrollIntoView({
|
|
205
|
+
block: "center"
|
|
206
|
+
}));
|
|
207
|
+
}, 100);
|
|
208
|
+
}, [state.open]);
|
|
209
|
+
return /*#__PURE__*/jsxRuntime.jsxs(InputWrapper.InputWrapper, _objectSpread(_objectSpread({
|
|
210
|
+
showClear: !utilities.isEmpty(state.value),
|
|
211
|
+
onClear: handleChange,
|
|
212
|
+
invalid: invalid,
|
|
213
|
+
readOnly: readOnly,
|
|
214
|
+
disabled: disabled,
|
|
215
|
+
required: required,
|
|
216
|
+
error: error,
|
|
217
|
+
wrapperRef: refs.setReference,
|
|
218
|
+
className: state.open ? "!z-[99]" : ""
|
|
219
|
+
}, rest), {}, {
|
|
220
|
+
children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
221
|
+
role: "none",
|
|
222
|
+
"data-select-display": "true",
|
|
223
|
+
className: classNames("group flex-1 py-1 px-2 truncate text-start flex gap-1 relative min-h-5 justify-start", multiple ? "flex-wrap" : "flex-nowrap overflow-hidden"),
|
|
224
|
+
onMouseUp: function onMouseUp(e) {
|
|
225
|
+
var _e$currentTarget$quer;
|
|
226
|
+
return (_e$currentTarget$quer = e.currentTarget.querySelector("input")) === null || _e$currentTarget$quer === void 0 ? void 0 : _e$currentTarget$quer.focus();
|
|
227
|
+
},
|
|
228
|
+
children: [displayValue, /*#__PURE__*/jsxRuntime.jsx("input", _objectSpread(_objectSpread({
|
|
229
|
+
readOnly: !searchable || readOnly,
|
|
230
|
+
disabled: disabled,
|
|
231
|
+
value: (_state$query = state.query) !== null && _state$query !== void 0 ? _state$query : "",
|
|
232
|
+
"aria-invalid": invalid,
|
|
233
|
+
"data-testid": name,
|
|
234
|
+
name: name,
|
|
235
|
+
size: 1,
|
|
236
|
+
ref: ref,
|
|
237
|
+
autoFocus: autoFocus,
|
|
238
|
+
className: classNames("appearance-none bg-transparent outline-none border-none ring-0 flex-1", disabled && "cursor-not-allowed pointer-events-none", multiple && "min-w-24"),
|
|
239
|
+
placeholder: utilities.isEmpty(state.value) ? placeholder : ""
|
|
240
|
+
}, referenceProps), {}, {
|
|
241
|
+
onChange: function onChange(evt) {
|
|
242
|
+
return handleQuery(evt.target.value);
|
|
243
|
+
}
|
|
244
|
+
}))]
|
|
245
|
+
}), !state.loading && /*#__PURE__*/jsxRuntime.jsx(core.Icon, {
|
|
246
|
+
className: classNames("flex-content p-2 z-0 order-9 text-muted cursor-pointer self-stretch flex items-center h-auto", state.open && "-scale-y-100 pointer-events-none", disabled && "pointer-events-none"),
|
|
247
|
+
onClick: function onClick() {
|
|
248
|
+
return !disabled && openDropdown();
|
|
249
|
+
},
|
|
250
|
+
icon: core.CoreIcons.chevronDown
|
|
251
|
+
}), state.loading && /*#__PURE__*/jsxRuntime.jsx(core.Icon, {
|
|
252
|
+
animate: "spin",
|
|
253
|
+
className: classNames("flex-content p-2 z-0 order-9 text-muted pointer-events-none"),
|
|
254
|
+
icon: core.CoreIcons.spinner
|
|
255
|
+
}), state.open && !state.loading && /*#__PURE__*/jsxRuntime.jsx(react$1.FloatingPortal, {
|
|
256
|
+
children: /*#__PURE__*/jsxRuntime.jsx(react$1.FloatingFocusManager, {
|
|
257
|
+
context: context,
|
|
258
|
+
initialFocus: -1,
|
|
259
|
+
visuallyHiddenDismiss: true,
|
|
260
|
+
children: /*#__PURE__*/jsxRuntime.jsx(Options.Options, _objectSpread(_objectSpread({
|
|
261
|
+
ref: refs.setFloating,
|
|
262
|
+
className: "outline shadow-lg max-h-[40vh] z-[var(--z-popover)]",
|
|
263
|
+
style: _objectSpread({
|
|
264
|
+
width: (_refs$reference$curre = refs.reference.current) === null || _refs$reference$curre === void 0 ? void 0 : _refs$reference$curre.getBoundingClientRect().width
|
|
265
|
+
}, floatingStyles)
|
|
266
|
+
}, floatingProps), {}, {
|
|
267
|
+
items: state.items,
|
|
268
|
+
active: state.activeIndex,
|
|
269
|
+
valueProperty: valueProperty,
|
|
270
|
+
labelProperty: labelProperty,
|
|
271
|
+
itemRef: setItemRef,
|
|
272
|
+
itemProps: makeItemProps,
|
|
273
|
+
children: function children(item, label) {
|
|
274
|
+
var _renderer3;
|
|
275
|
+
return (_renderer3 = renderer === null || renderer === void 0 ? void 0 : renderer(item)) !== null && _renderer3 !== void 0 ? _renderer3 : label;
|
|
276
|
+
}
|
|
277
|
+
}))
|
|
278
|
+
})
|
|
279
|
+
})]
|
|
280
|
+
}));
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
exports.Select = Select;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utilities = require('@react-fabric/utilities');
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* React Fabric
|
|
7
|
+
* @version: 1.0.0
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* The MIT License (MIT)
|
|
11
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
12
|
+
*
|
|
13
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
14
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
15
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
16
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
17
|
+
* subject to the following conditions:
|
|
18
|
+
*
|
|
19
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
20
|
+
* portions of the Software.
|
|
21
|
+
*
|
|
22
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
23
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
24
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
25
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
var makeOption = function makeOption(query) {
|
|
29
|
+
return query;
|
|
30
|
+
};
|
|
31
|
+
var getSelectedValue = function getSelectedValue(value, emptyValue) {
|
|
32
|
+
var _ref, _value$valueProperty;
|
|
33
|
+
var valueProperty = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
|
|
34
|
+
return utilities.isArray(value) ? value.map(function (val) {
|
|
35
|
+
var _val$valueProperty;
|
|
36
|
+
return (_val$valueProperty = val[valueProperty]) !== null && _val$valueProperty !== void 0 ? _val$valueProperty : val;
|
|
37
|
+
}) : (_ref = (_value$valueProperty = value === null || value === void 0 ? void 0 : value[valueProperty]) !== null && _value$valueProperty !== void 0 ? _value$valueProperty : value) !== null && _ref !== void 0 ? _ref : emptyValue;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.getSelectedValue = getSelectedValue;
|
|
41
|
+
exports.makeOption = makeOption;
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
|
4
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
5
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
6
|
+
var core = require('@react-fabric/core');
|
|
7
|
+
var utilities = require('@react-fabric/utilities');
|
|
8
|
+
var react = require('react');
|
|
9
|
+
var helpers = require('./helpers.js');
|
|
10
|
+
|
|
11
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
12
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
|
+
var useSelect = function useSelect(_ref) {
|
|
14
|
+
var _ref$options = _ref.options,
|
|
15
|
+
options = _ref$options === void 0 ? utilities.EMPTY_ARRAY : _ref$options,
|
|
16
|
+
value = _ref.value,
|
|
17
|
+
autoComplete = _ref.autoComplete,
|
|
18
|
+
_ref$multiple = _ref.multiple,
|
|
19
|
+
multiple = _ref$multiple === void 0 ? false : _ref$multiple,
|
|
20
|
+
_ref$allowCreate = _ref.allowCreate,
|
|
21
|
+
allowCreate = _ref$allowCreate === void 0 ? false : _ref$allowCreate,
|
|
22
|
+
_ref$groupProperty = _ref.groupProperty,
|
|
23
|
+
groupProperty = _ref$groupProperty === void 0 ? "" : _ref$groupProperty,
|
|
24
|
+
_ref$valueProperty = _ref.valueProperty,
|
|
25
|
+
valueProperty = _ref$valueProperty === void 0 ? "id" : _ref$valueProperty,
|
|
26
|
+
_ref$labelProperty = _ref.labelProperty,
|
|
27
|
+
labelProperty = _ref$labelProperty === void 0 ? "label" : _ref$labelProperty,
|
|
28
|
+
_ref$createOption = _ref.createOption,
|
|
29
|
+
createOption = _ref$createOption === void 0 ? helpers.makeOption : _ref$createOption,
|
|
30
|
+
matcher = _ref.matcher,
|
|
31
|
+
onQuery = _ref.onQuery,
|
|
32
|
+
onChange = _ref.onChange,
|
|
33
|
+
onSelect = _ref.onSelect;
|
|
34
|
+
// value state
|
|
35
|
+
var emptyValue = react.useMemo(function () {
|
|
36
|
+
return multiple ? [] : "";
|
|
37
|
+
}, [multiple]);
|
|
38
|
+
var deferred = react.useDeferredValue(value !== null && value !== void 0 ? value : emptyValue);
|
|
39
|
+
|
|
40
|
+
// list items
|
|
41
|
+
var listRef = react.useRef([]);
|
|
42
|
+
var focusableRef = react.useRef(null);
|
|
43
|
+
var listContentRef = react.useRef([]);
|
|
44
|
+
var optionsRef = react.useRef([]);
|
|
45
|
+
var macthOption = react.useCallback(function (item, query) {
|
|
46
|
+
var _matcher, _item$labelProperty;
|
|
47
|
+
return (_matcher = matcher === null || matcher === void 0 ? void 0 : matcher(item, query)) !== null && _matcher !== void 0 ? _matcher : utilities.matchString((_item$labelProperty = item[labelProperty]) !== null && _item$labelProperty !== void 0 ? _item$labelProperty : item, query);
|
|
48
|
+
}, [matcher]);
|
|
49
|
+
var fireChange = react.useCallback(function (value) {
|
|
50
|
+
setTimeout(function () {
|
|
51
|
+
onSelect === null || onSelect === void 0 || onSelect(value !== null && value !== void 0 ? value : emptyValue);
|
|
52
|
+
onChange === null || onChange === void 0 || onChange(helpers.getSelectedValue(value, emptyValue, valueProperty));
|
|
53
|
+
}, 50);
|
|
54
|
+
}, [emptyValue, onSelect, onChange]);
|
|
55
|
+
var _useReducer = react.useReducer(function (state, action) {
|
|
56
|
+
if (action.type === "open") {
|
|
57
|
+
var index = state.items.indexOf(state.multiple ? state.value[0] : state.value);
|
|
58
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
59
|
+
open: true,
|
|
60
|
+
activeIndex: index > -1 ? index : null
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (action.type === "close") {
|
|
64
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
65
|
+
activeIndex: null,
|
|
66
|
+
open: false
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (action.type === "active" && state.open) {
|
|
70
|
+
if (autoComplete && action.index !== null) state.query = state.items[action.index];
|
|
71
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
72
|
+
activeIndex: action.index
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (action.type === "setValue") {
|
|
76
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
77
|
+
value: action.value,
|
|
78
|
+
selectedIndex: state.items.indexOf(state.multiple ? action.value[0] : action.value)
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (action.type === "changeValue") {
|
|
82
|
+
var _action$value;
|
|
83
|
+
var newState = _objectSpread(_objectSpread({}, state), {}, {
|
|
84
|
+
value: state.emptyValue,
|
|
85
|
+
activeIndex: null,
|
|
86
|
+
selectedIndex: autoComplete ? state.multiple ? (_action$value = action.value) === null || _action$value === void 0 ? void 0 : _action$value.length : 0 : state.items.indexOf(action.value)
|
|
87
|
+
});
|
|
88
|
+
if (action.value !== undefined) {
|
|
89
|
+
if (multiple) {
|
|
90
|
+
var values = [];
|
|
91
|
+
if (action.value && autoComplete) values.push.apply(values, _toConsumableArray(action.value.split(/,\s?/)));else if (action.value) values.push(action.value);
|
|
92
|
+
newState.value = _toConsumableArray(state.value);
|
|
93
|
+
values.forEach(function (value) {
|
|
94
|
+
var _newState$value$inclu, _newState$value;
|
|
95
|
+
newState.value = (_newState$value$inclu = (_newState$value = newState.value).includes) !== null && _newState$value$inclu !== void 0 && _newState$value$inclu.call(_newState$value, value) ? newState.value.filter(function (item) {
|
|
96
|
+
return item !== value;
|
|
97
|
+
}) : [].concat(_toConsumableArray(newState.value), [value]);
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
newState.value = action.value;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!state.multiple) newState.open = false;
|
|
104
|
+
fireChange(newState.value);
|
|
105
|
+
return newState;
|
|
106
|
+
}
|
|
107
|
+
if (action.type === "removeItem") {
|
|
108
|
+
var newValue = state.value;
|
|
109
|
+
if (state.multiple) {
|
|
110
|
+
var _action$index;
|
|
111
|
+
newValue = _toConsumableArray(newValue);
|
|
112
|
+
newValue.splice((_action$index = action.index) !== null && _action$index !== void 0 ? _action$index : -1, 1);
|
|
113
|
+
} else {
|
|
114
|
+
newValue = "";
|
|
115
|
+
}
|
|
116
|
+
fireChange(newValue);
|
|
117
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
118
|
+
value: newValue
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (action.type === "startQuery") {
|
|
122
|
+
return _objectSpread(_objectSpread({}, state), {}, {
|
|
123
|
+
loading: true,
|
|
124
|
+
query: action.query
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (action.type === "options") {
|
|
128
|
+
var _newState = _objectSpread(_objectSpread({}, state), {}, {
|
|
129
|
+
loading: false,
|
|
130
|
+
query: action.query
|
|
131
|
+
});
|
|
132
|
+
var optionList = _toConsumableArray(action.options);
|
|
133
|
+
if (autoComplete && action.query !== undefined) {
|
|
134
|
+
optionList = utilities.dedupe([action.query].concat(_toConsumableArray(optionList)));
|
|
135
|
+
}
|
|
136
|
+
var grouped = utilities.groupBy(optionList, groupProperty, "");
|
|
137
|
+
if (action.open) _newState.open = true;
|
|
138
|
+
_newState.items = [];
|
|
139
|
+
// add new item if allowCreate
|
|
140
|
+
if (allowCreate && action.query) _newState.items.push({
|
|
141
|
+
___create___: true,
|
|
142
|
+
value: createOption(action.query)
|
|
143
|
+
});
|
|
144
|
+
Object.entries(grouped).forEach(function (_ref2) {
|
|
145
|
+
var _newState$items;
|
|
146
|
+
var _ref3 = _slicedToArray(_ref2, 2),
|
|
147
|
+
key = _ref3[0],
|
|
148
|
+
list = _ref3[1];
|
|
149
|
+
if (key) {
|
|
150
|
+
_newState.items.push({
|
|
151
|
+
___group___: true,
|
|
152
|
+
label: key
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
(_newState$items = _newState.items).push.apply(_newState$items, _toConsumableArray(list));
|
|
156
|
+
});
|
|
157
|
+
if (action.setActive) {
|
|
158
|
+
var _index = _newState.items.findIndex(function (opt) {
|
|
159
|
+
return !opt.___group___;
|
|
160
|
+
});
|
|
161
|
+
_newState.activeIndex = _index >= 0 ? _index : null;
|
|
162
|
+
} else {
|
|
163
|
+
_newState.activeIndex = null;
|
|
164
|
+
}
|
|
165
|
+
listContentRef.current = _newState.items;
|
|
166
|
+
return _newState;
|
|
167
|
+
}
|
|
168
|
+
return state;
|
|
169
|
+
}, {
|
|
170
|
+
multiple: multiple,
|
|
171
|
+
emptyValue: emptyValue,
|
|
172
|
+
open: false,
|
|
173
|
+
loading: false,
|
|
174
|
+
activeIndex: null,
|
|
175
|
+
selectedIndex: null,
|
|
176
|
+
value: emptyValue,
|
|
177
|
+
// filter
|
|
178
|
+
query: undefined,
|
|
179
|
+
// dropdown items
|
|
180
|
+
items: []
|
|
181
|
+
}),
|
|
182
|
+
_useReducer2 = _slicedToArray(_useReducer, 2),
|
|
183
|
+
state = _useReducer2[0],
|
|
184
|
+
dispatch = _useReducer2[1];
|
|
185
|
+
react.useEffect(function () {
|
|
186
|
+
if (!multiple) {
|
|
187
|
+
var _ref4;
|
|
188
|
+
var newOption = (allowCreate || autoComplete) && deferred ? createOption(deferred) : undefined;
|
|
189
|
+
var hasValue = options.find(function (opt) {
|
|
190
|
+
var _opt$valueProperty;
|
|
191
|
+
return ((_opt$valueProperty = opt[valueProperty]) !== null && _opt$valueProperty !== void 0 ? _opt$valueProperty : opt) === deferred;
|
|
192
|
+
});
|
|
193
|
+
optionsRef.current = options;
|
|
194
|
+
// if current value missing in list inject it
|
|
195
|
+
if (!hasValue && newOption) {
|
|
196
|
+
optionsRef.current = [newOption].concat(_toConsumableArray(options !== null && options !== void 0 ? options : []));
|
|
197
|
+
}
|
|
198
|
+
dispatch({
|
|
199
|
+
type: "options",
|
|
200
|
+
options: optionsRef.current
|
|
201
|
+
});
|
|
202
|
+
dispatch({
|
|
203
|
+
type: "setValue",
|
|
204
|
+
value: (_ref4 = hasValue !== null && hasValue !== void 0 ? hasValue : newOption) !== null && _ref4 !== void 0 ? _ref4 : ""
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (multiple) {
|
|
208
|
+
optionsRef.current = [];
|
|
209
|
+
var _value = (deferred !== null && deferred !== void 0 ? deferred : []).map(function (item) {
|
|
210
|
+
var _ref5;
|
|
211
|
+
var newOption = (allowCreate || autoComplete) && item ? createOption(item) : undefined;
|
|
212
|
+
var hasValue = options.find(function (opt) {
|
|
213
|
+
var _opt$valueProperty2;
|
|
214
|
+
return ((_opt$valueProperty2 = opt[valueProperty]) !== null && _opt$valueProperty2 !== void 0 ? _opt$valueProperty2 : opt) === deferred;
|
|
215
|
+
});
|
|
216
|
+
if (!hasValue && newOption) {
|
|
217
|
+
optionsRef.current.push(newOption);
|
|
218
|
+
}
|
|
219
|
+
return (_ref5 = hasValue !== null && hasValue !== void 0 ? hasValue : newOption) !== null && _ref5 !== void 0 ? _ref5 : "";
|
|
220
|
+
});
|
|
221
|
+
optionsRef.current = [].concat(_toConsumableArray(optionsRef.current), _toConsumableArray(options !== null && options !== void 0 ? options : []));
|
|
222
|
+
dispatch({
|
|
223
|
+
type: "options",
|
|
224
|
+
options: optionsRef.current
|
|
225
|
+
});
|
|
226
|
+
dispatch({
|
|
227
|
+
type: "setValue",
|
|
228
|
+
value: _value
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}, [deferred, options, multiple, allowCreate, autoComplete, createOption]);
|
|
232
|
+
var fireQuery = core.useDebounce(function (query) {
|
|
233
|
+
void Promise.resolve(onQuery === null || onQuery === void 0 ? void 0 : onQuery(query)).then(function (options) {
|
|
234
|
+
var newOptions = _toConsumableArray(options);
|
|
235
|
+
// if autocomplete inject other filtered options from history
|
|
236
|
+
if (autoComplete) {
|
|
237
|
+
newOptions.push.apply(newOptions, _toConsumableArray(optionsRef.current.filter(function (opt) {
|
|
238
|
+
return macthOption === null || macthOption === void 0 ? void 0 : macthOption(opt, query);
|
|
239
|
+
})));
|
|
240
|
+
}
|
|
241
|
+
dispatch({
|
|
242
|
+
type: "options",
|
|
243
|
+
query: query,
|
|
244
|
+
options: newOptions,
|
|
245
|
+
open: true,
|
|
246
|
+
setActive: true
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
}, [onQuery]);
|
|
250
|
+
|
|
251
|
+
// handlers
|
|
252
|
+
var handleQuery = react.useCallback(function (query) {
|
|
253
|
+
if (!query) {
|
|
254
|
+
dispatch({
|
|
255
|
+
type: "options",
|
|
256
|
+
query: "",
|
|
257
|
+
options: optionsRef.current
|
|
258
|
+
});
|
|
259
|
+
} else {
|
|
260
|
+
if (onQuery) {
|
|
261
|
+
dispatch({
|
|
262
|
+
type: "startQuery",
|
|
263
|
+
query: query
|
|
264
|
+
});
|
|
265
|
+
return fireQuery(query);
|
|
266
|
+
}
|
|
267
|
+
dispatch({
|
|
268
|
+
type: "options",
|
|
269
|
+
query: query,
|
|
270
|
+
open: true,
|
|
271
|
+
setActive: true,
|
|
272
|
+
options: optionsRef.current.filter(function (opt) {
|
|
273
|
+
return macthOption === null || macthOption === void 0 ? void 0 : macthOption(opt, query);
|
|
274
|
+
})
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}, [macthOption, onQuery]);
|
|
278
|
+
var handleChange = react.useCallback(function (value) {
|
|
279
|
+
var values = value;
|
|
280
|
+
if (autoComplete && multiple && value) {
|
|
281
|
+
values = value.split(/,\s?/);
|
|
282
|
+
}
|
|
283
|
+
dispatch({
|
|
284
|
+
type: "changeValue",
|
|
285
|
+
value: value !== null && value !== void 0 && value.___create___ ? value.value : value
|
|
286
|
+
});
|
|
287
|
+
if (value !== null && value !== void 0 && value.___create___) {
|
|
288
|
+
optionsRef.current = [].concat(_toConsumableArray(optionsRef.current), [createOption(value.value)]);
|
|
289
|
+
}
|
|
290
|
+
if (autoComplete) {
|
|
291
|
+
optionsRef.current = utilities.dedupe([].concat(_toConsumableArray(utilities.isArray(values) ? values : [values]), _toConsumableArray(optionsRef.current)));
|
|
292
|
+
}
|
|
293
|
+
dispatch({
|
|
294
|
+
type: "options",
|
|
295
|
+
options: optionsRef.current
|
|
296
|
+
});
|
|
297
|
+
}, []);
|
|
298
|
+
var handleRemove = react.useCallback(function (index) {
|
|
299
|
+
dispatch({
|
|
300
|
+
type: "removeItem",
|
|
301
|
+
index: index
|
|
302
|
+
});
|
|
303
|
+
}, []);
|
|
304
|
+
var setOpen = react.useCallback(function (open) {
|
|
305
|
+
dispatch({
|
|
306
|
+
type: open ? "open" : "close"
|
|
307
|
+
});
|
|
308
|
+
dispatch({
|
|
309
|
+
type: "options",
|
|
310
|
+
query: undefined,
|
|
311
|
+
options: optionsRef.current
|
|
312
|
+
});
|
|
313
|
+
}, []);
|
|
314
|
+
var setActiveIndex = react.useCallback(function (index) {
|
|
315
|
+
dispatch({
|
|
316
|
+
type: "active",
|
|
317
|
+
index: index
|
|
318
|
+
});
|
|
319
|
+
}, []);
|
|
320
|
+
return {
|
|
321
|
+
state: state,
|
|
322
|
+
listRef: listRef,
|
|
323
|
+
focusableRef: focusableRef,
|
|
324
|
+
listContentRef: listContentRef,
|
|
325
|
+
handleChange: handleChange,
|
|
326
|
+
handleRemove: handleRemove,
|
|
327
|
+
handleQuery: handleQuery,
|
|
328
|
+
macthOption: macthOption,
|
|
329
|
+
setActiveIndex: setActiveIndex,
|
|
330
|
+
setOpen: setOpen,
|
|
331
|
+
setItemRef: function setItemRef(index, node) {
|
|
332
|
+
listRef.current[index] = node;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
exports.useSelect = useSelect;
|