@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,350 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
autoUpdate,
|
|
26
|
+
flip,
|
|
27
|
+
FloatingFocusManager,
|
|
28
|
+
FloatingPortal,
|
|
29
|
+
offset,
|
|
30
|
+
shift,
|
|
31
|
+
useClick,
|
|
32
|
+
useDismiss,
|
|
33
|
+
useFloating,
|
|
34
|
+
useInteractions,
|
|
35
|
+
useListNavigation,
|
|
36
|
+
useTypeahead,
|
|
37
|
+
} from "@floating-ui/react";
|
|
38
|
+
import { Chip, CoreIcons, Icon } from "@react-fabric/core";
|
|
39
|
+
import { isArray, isEmpty } from "@react-fabric/utilities";
|
|
40
|
+
import classNames from "classnames";
|
|
41
|
+
import { useCallback, useId, useLayoutEffect, useMemo } from "react";
|
|
42
|
+
import { InputWrapper } from "../internal/InputWrapper";
|
|
43
|
+
import { type SelectProps } from "../types";
|
|
44
|
+
import { Options } from "./Options";
|
|
45
|
+
import { useSelect } from "./useSelect";
|
|
46
|
+
|
|
47
|
+
export const Select = <T extends AnyObject = string>({
|
|
48
|
+
ref,
|
|
49
|
+
name,
|
|
50
|
+
value,
|
|
51
|
+
invalid,
|
|
52
|
+
readOnly,
|
|
53
|
+
disabled,
|
|
54
|
+
required,
|
|
55
|
+
placeholder,
|
|
56
|
+
autoFocus,
|
|
57
|
+
error,
|
|
58
|
+
options,
|
|
59
|
+
groupProperty,
|
|
60
|
+
labelProperty = "label" as AnyObject,
|
|
61
|
+
valueProperty = "id" as AnyObject,
|
|
62
|
+
multiple,
|
|
63
|
+
searchable,
|
|
64
|
+
allowCreate,
|
|
65
|
+
createOption,
|
|
66
|
+
matcher,
|
|
67
|
+
renderer,
|
|
68
|
+
onQuery,
|
|
69
|
+
onChange,
|
|
70
|
+
onSelect,
|
|
71
|
+
onEnterPressed,
|
|
72
|
+
...rest
|
|
73
|
+
}: SelectProps<T>) => {
|
|
74
|
+
const {
|
|
75
|
+
state,
|
|
76
|
+
listRef,
|
|
77
|
+
listContentRef,
|
|
78
|
+
setOpen,
|
|
79
|
+
macthOption,
|
|
80
|
+
handleChange,
|
|
81
|
+
handleRemove,
|
|
82
|
+
handleQuery,
|
|
83
|
+
setItemRef,
|
|
84
|
+
setActiveIndex,
|
|
85
|
+
} = useSelect({
|
|
86
|
+
value,
|
|
87
|
+
options,
|
|
88
|
+
multiple,
|
|
89
|
+
groupProperty,
|
|
90
|
+
labelProperty,
|
|
91
|
+
valueProperty,
|
|
92
|
+
allowCreate,
|
|
93
|
+
createOption,
|
|
94
|
+
matcher,
|
|
95
|
+
onChange,
|
|
96
|
+
onSelect,
|
|
97
|
+
onQuery,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const dropdownKey = useId();
|
|
101
|
+
|
|
102
|
+
const { context, floatingStyles, refs } = useFloating({
|
|
103
|
+
open: state.open,
|
|
104
|
+
onOpenChange: (open, evt, reason) => {
|
|
105
|
+
!open && (listRef.current = []);
|
|
106
|
+
setOpen(open);
|
|
107
|
+
},
|
|
108
|
+
strategy: "fixed",
|
|
109
|
+
placement: "bottom",
|
|
110
|
+
whileElementsMounted: autoUpdate,
|
|
111
|
+
middleware: [shift({ padding: 8 }), flip(), offset(2)],
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// floating interactions
|
|
115
|
+
const click = useClick(context, {});
|
|
116
|
+
const dismiss = useDismiss(context, {
|
|
117
|
+
// referencePress: true,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const navigation = useListNavigation(context, {
|
|
121
|
+
listRef,
|
|
122
|
+
activeIndex: state.activeIndex,
|
|
123
|
+
selectedIndex: state.selectedIndex,
|
|
124
|
+
loop: true,
|
|
125
|
+
virtual: true,
|
|
126
|
+
focusItemOnOpen: false,
|
|
127
|
+
onNavigate: setActiveIndex,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const typeahead = useTypeahead(context, {
|
|
131
|
+
listRef: listContentRef,
|
|
132
|
+
activeIndex: state.activeIndex,
|
|
133
|
+
selectedIndex: state.selectedIndex,
|
|
134
|
+
findMatch(list, typedString) {
|
|
135
|
+
return list.find((item) => macthOption(item, typedString));
|
|
136
|
+
},
|
|
137
|
+
onMatch(index) {
|
|
138
|
+
state.open && setActiveIndex(index);
|
|
139
|
+
|
|
140
|
+
!searchable &&
|
|
141
|
+
!multiple &&
|
|
142
|
+
!state.open &&
|
|
143
|
+
handleChange(listContentRef.current[index]);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions(
|
|
148
|
+
[dismiss, searchable ? undefined : click, navigation, typeahead],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const referenceProps = useMemo(() => {
|
|
152
|
+
const props: AnyObject = getReferenceProps();
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
...props,
|
|
156
|
+
onBlur(evt: React.FocusEvent) {
|
|
157
|
+
if (
|
|
158
|
+
evt.relatedTarget?.closest(`[data-select-dropdown="${dropdownKey}"]`)
|
|
159
|
+
) {
|
|
160
|
+
refs.domReference.current?.querySelector("input")?.focus();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
handleQuery("");
|
|
164
|
+
setOpen(false);
|
|
165
|
+
},
|
|
166
|
+
onKeyDown(evt: React.KeyboardEvent) {
|
|
167
|
+
if (evt.key === "Enter") {
|
|
168
|
+
if (state.activeIndex !== null) {
|
|
169
|
+
handleChange(listContentRef.current[state.activeIndex]);
|
|
170
|
+
evt.preventDefault();
|
|
171
|
+
evt.stopPropagation();
|
|
172
|
+
return false;
|
|
173
|
+
} else if (state.query) {
|
|
174
|
+
handleQuery("");
|
|
175
|
+
} else {
|
|
176
|
+
onEnterPressed?.(evt);
|
|
177
|
+
}
|
|
178
|
+
} else if (evt.key === "Tab" && state.activeIndex !== null) {
|
|
179
|
+
handleChange(listContentRef.current[state.activeIndex]);
|
|
180
|
+
} else if (evt.key === "Backspace") {
|
|
181
|
+
!state.query && handleRemove();
|
|
182
|
+
} else if (!searchable || ["ArrowUp", "ArrowDown"].includes(evt.key)) {
|
|
183
|
+
if (
|
|
184
|
+
["ArrowUp", "ArrowDown"].includes(evt.key) &&
|
|
185
|
+
state.activeIndex === null
|
|
186
|
+
) {
|
|
187
|
+
setActiveIndex(state.selectedIndex);
|
|
188
|
+
}
|
|
189
|
+
setTimeout(() => {
|
|
190
|
+
props.onKeyDown?.(evt);
|
|
191
|
+
}, 50);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}, [
|
|
196
|
+
getReferenceProps,
|
|
197
|
+
onEnterPressed,
|
|
198
|
+
handleQuery,
|
|
199
|
+
refs,
|
|
200
|
+
state.query,
|
|
201
|
+
state.activeIndex,
|
|
202
|
+
]);
|
|
203
|
+
|
|
204
|
+
const floatingProps = useMemo(() => {
|
|
205
|
+
return getFloatingProps({
|
|
206
|
+
"data-select-dropdown": dropdownKey,
|
|
207
|
+
} as AnyObject);
|
|
208
|
+
}, [getFloatingProps]);
|
|
209
|
+
|
|
210
|
+
const makeItemProps = useCallback(
|
|
211
|
+
(item: AnyObject) => {
|
|
212
|
+
const selected = isArray(state.value)
|
|
213
|
+
? state.value?.includes?.(item)
|
|
214
|
+
: state.value === item;
|
|
215
|
+
return getItemProps({
|
|
216
|
+
"data-selected": selected ? true : undefined,
|
|
217
|
+
onClick() {
|
|
218
|
+
handleChange(item);
|
|
219
|
+
},
|
|
220
|
+
} as AnyObject);
|
|
221
|
+
},
|
|
222
|
+
[getItemProps, handleChange, state.value],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const displayValue = useMemo(() => {
|
|
226
|
+
if (isArray(state.value)) {
|
|
227
|
+
return state.value.map((item: AnyObject, index) => (
|
|
228
|
+
<Chip key={index} onRemove={() => handleRemove(index)}>
|
|
229
|
+
{renderer?.(item) ?? item[labelProperty] ?? item}
|
|
230
|
+
</Chip>
|
|
231
|
+
));
|
|
232
|
+
} else if (!isEmpty(state.value) && !state.query) {
|
|
233
|
+
return (
|
|
234
|
+
<span className="self-center pointer-events-none select-none">
|
|
235
|
+
{renderer?.(state.value) ?? state.value[labelProperty] ?? state.value}
|
|
236
|
+
</span>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
}, [state.value, state.query, placeholder, renderer]);
|
|
240
|
+
|
|
241
|
+
const openDropdown = useCallback(() => {
|
|
242
|
+
setOpen(true);
|
|
243
|
+
refs.domReference.current?.querySelector("input")?.focus();
|
|
244
|
+
}, []);
|
|
245
|
+
|
|
246
|
+
useLayoutEffect(() => {
|
|
247
|
+
state.open &&
|
|
248
|
+
setTimeout(() => {
|
|
249
|
+
state.selectedIndex &&
|
|
250
|
+
listRef.current[state.selectedIndex]?.scrollIntoView({
|
|
251
|
+
block: "center",
|
|
252
|
+
});
|
|
253
|
+
}, 100);
|
|
254
|
+
}, [state.open]);
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<InputWrapper
|
|
258
|
+
showClear={!isEmpty(state.value)}
|
|
259
|
+
onClear={handleChange}
|
|
260
|
+
invalid={invalid}
|
|
261
|
+
readOnly={readOnly}
|
|
262
|
+
disabled={disabled}
|
|
263
|
+
required={required}
|
|
264
|
+
error={error}
|
|
265
|
+
wrapperRef={refs.setReference}
|
|
266
|
+
className={state.open ? "!z-[99]" : ""}
|
|
267
|
+
{...rest}
|
|
268
|
+
>
|
|
269
|
+
<div
|
|
270
|
+
role="none"
|
|
271
|
+
data-select-display="true"
|
|
272
|
+
className={classNames(
|
|
273
|
+
"group flex-1 py-1 px-2 truncate text-start flex gap-1 relative min-h-5 justify-start",
|
|
274
|
+
multiple ? "flex-wrap" : "flex-nowrap overflow-hidden",
|
|
275
|
+
)}
|
|
276
|
+
onMouseUp={(e) =>
|
|
277
|
+
e.currentTarget.querySelector<HTMLElement>("input")?.focus()
|
|
278
|
+
}
|
|
279
|
+
>
|
|
280
|
+
{displayValue}
|
|
281
|
+
<input
|
|
282
|
+
readOnly={!searchable || readOnly}
|
|
283
|
+
disabled={disabled}
|
|
284
|
+
value={state.query ?? ""}
|
|
285
|
+
aria-invalid={invalid}
|
|
286
|
+
data-testid={name}
|
|
287
|
+
name={name}
|
|
288
|
+
size={1}
|
|
289
|
+
ref={ref}
|
|
290
|
+
autoFocus={autoFocus}
|
|
291
|
+
className={classNames(
|
|
292
|
+
"appearance-none bg-transparent outline-none border-none ring-0 flex-1",
|
|
293
|
+
disabled && "cursor-not-allowed pointer-events-none",
|
|
294
|
+
multiple && "min-w-24",
|
|
295
|
+
)}
|
|
296
|
+
placeholder={isEmpty(state.value) ? placeholder : ""}
|
|
297
|
+
{...referenceProps}
|
|
298
|
+
onChange={(evt) => handleQuery(evt.target.value)}
|
|
299
|
+
/>
|
|
300
|
+
</div>
|
|
301
|
+
{!state.loading && (
|
|
302
|
+
<Icon
|
|
303
|
+
className={classNames(
|
|
304
|
+
"flex-content p-2 z-0 order-9 text-muted cursor-pointer self-stretch flex items-center h-auto",
|
|
305
|
+
state.open && "-scale-y-100 pointer-events-none",
|
|
306
|
+
disabled && "pointer-events-none",
|
|
307
|
+
)}
|
|
308
|
+
onClick={() => !disabled && openDropdown()}
|
|
309
|
+
icon={CoreIcons.chevronDown}
|
|
310
|
+
/>
|
|
311
|
+
)}
|
|
312
|
+
{state.loading && (
|
|
313
|
+
<Icon
|
|
314
|
+
animate="spin"
|
|
315
|
+
className={classNames(
|
|
316
|
+
"flex-content p-2 z-0 order-9 text-muted pointer-events-none",
|
|
317
|
+
)}
|
|
318
|
+
icon={CoreIcons.spinner}
|
|
319
|
+
/>
|
|
320
|
+
)}
|
|
321
|
+
{state.open && !state.loading && (
|
|
322
|
+
<FloatingPortal>
|
|
323
|
+
<FloatingFocusManager
|
|
324
|
+
context={context}
|
|
325
|
+
initialFocus={-1}
|
|
326
|
+
visuallyHiddenDismiss
|
|
327
|
+
>
|
|
328
|
+
<Options
|
|
329
|
+
ref={refs.setFloating}
|
|
330
|
+
className="outline shadow-lg max-h-[40vh] z-[var(--z-popover)]"
|
|
331
|
+
style={{
|
|
332
|
+
width: refs.reference.current?.getBoundingClientRect().width,
|
|
333
|
+
...floatingStyles,
|
|
334
|
+
}}
|
|
335
|
+
{...floatingProps}
|
|
336
|
+
items={state.items}
|
|
337
|
+
active={state.activeIndex}
|
|
338
|
+
valueProperty={valueProperty}
|
|
339
|
+
labelProperty={labelProperty}
|
|
340
|
+
itemRef={setItemRef}
|
|
341
|
+
itemProps={makeItemProps}
|
|
342
|
+
>
|
|
343
|
+
{(item, label) => renderer?.(item) ?? label}
|
|
344
|
+
</Options>
|
|
345
|
+
</FloatingFocusManager>
|
|
346
|
+
</FloatingPortal>
|
|
347
|
+
)}
|
|
348
|
+
</InputWrapper>
|
|
349
|
+
);
|
|
350
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { isArray } from "@react-fabric/utilities";
|
|
25
|
+
|
|
26
|
+
export const makeOption = (query: string) => query;
|
|
27
|
+
|
|
28
|
+
export const getSelectedValue = (
|
|
29
|
+
value: AnyObject,
|
|
30
|
+
emptyValue: AnyObject,
|
|
31
|
+
valueProperty: AnyObject = "",
|
|
32
|
+
) => {
|
|
33
|
+
return isArray(value)
|
|
34
|
+
? value.map((val: AnyObject) => val[valueProperty] ?? val)
|
|
35
|
+
: value?.[valueProperty] ?? value ?? emptyValue;
|
|
36
|
+
};
|