@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,288 @@
|
|
|
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
|
+
FloatingFocusManager,
|
|
27
|
+
useFloating,
|
|
28
|
+
useInteractions,
|
|
29
|
+
useListNavigation,
|
|
30
|
+
useTypeahead,
|
|
31
|
+
} from "@floating-ui/react";
|
|
32
|
+
import { Chip } from "@react-fabric/core";
|
|
33
|
+
import { isArray, isEmpty } from "@react-fabric/utilities";
|
|
34
|
+
import classNames from "classnames";
|
|
35
|
+
import { Fragment, useCallback, useId, useMemo } from "react";
|
|
36
|
+
import { FieldWrapper } from "../internal/FieldWrapper";
|
|
37
|
+
import { InputWrapper } from "../internal/InputWrapper";
|
|
38
|
+
import { type SelectProps } from "../types";
|
|
39
|
+
import { Options } from "./Options";
|
|
40
|
+
import { useSelect } from "./useSelect";
|
|
41
|
+
|
|
42
|
+
export const List = <T extends AnyObject = string>({
|
|
43
|
+
ref,
|
|
44
|
+
name,
|
|
45
|
+
value,
|
|
46
|
+
readOnly,
|
|
47
|
+
disabled,
|
|
48
|
+
required,
|
|
49
|
+
placeholder,
|
|
50
|
+
autoFocus,
|
|
51
|
+
options,
|
|
52
|
+
height,
|
|
53
|
+
groupProperty,
|
|
54
|
+
labelProperty,
|
|
55
|
+
valueProperty,
|
|
56
|
+
multiple,
|
|
57
|
+
allowCreate,
|
|
58
|
+
createOption,
|
|
59
|
+
matcher,
|
|
60
|
+
renderer,
|
|
61
|
+
onQuery,
|
|
62
|
+
onChange,
|
|
63
|
+
onSelect,
|
|
64
|
+
onEnterPressed,
|
|
65
|
+
invalid,
|
|
66
|
+
error,
|
|
67
|
+
allowClear,
|
|
68
|
+
decorateStart,
|
|
69
|
+
decorateEnd,
|
|
70
|
+
// @ts-expect-error ignore
|
|
71
|
+
"data-inner": isInner,
|
|
72
|
+
// @ts-expect-error ignore
|
|
73
|
+
"data-hide-selected": hideSelected,
|
|
74
|
+
...rest
|
|
75
|
+
}: SelectProps<T> & { height?: string | number }) => {
|
|
76
|
+
const Wrapper = useMemo(() => (isInner ? Fragment : FieldWrapper), [isInner]);
|
|
77
|
+
const wrapperProps = useMemo(
|
|
78
|
+
() => (isInner ? {} : { ...rest }),
|
|
79
|
+
[isInner, rest],
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const {
|
|
83
|
+
state,
|
|
84
|
+
listRef,
|
|
85
|
+
listContentRef,
|
|
86
|
+
macthOption,
|
|
87
|
+
handleChange,
|
|
88
|
+
handleRemove,
|
|
89
|
+
handleQuery,
|
|
90
|
+
setItemRef,
|
|
91
|
+
setActiveIndex,
|
|
92
|
+
} = useSelect({
|
|
93
|
+
value,
|
|
94
|
+
options,
|
|
95
|
+
multiple,
|
|
96
|
+
groupProperty,
|
|
97
|
+
labelProperty,
|
|
98
|
+
valueProperty,
|
|
99
|
+
allowCreate,
|
|
100
|
+
createOption,
|
|
101
|
+
matcher,
|
|
102
|
+
onChange,
|
|
103
|
+
onSelect,
|
|
104
|
+
onQuery,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const dropdownKey = useId();
|
|
108
|
+
|
|
109
|
+
const { context, refs } = useFloating({
|
|
110
|
+
open: true,
|
|
111
|
+
placement: "bottom",
|
|
112
|
+
whileElementsMounted: autoUpdate,
|
|
113
|
+
middleware: [],
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// floating interactions
|
|
117
|
+
const navigation = useListNavigation(context, {
|
|
118
|
+
listRef,
|
|
119
|
+
activeIndex: state.activeIndex,
|
|
120
|
+
selectedIndex: state.selectedIndex,
|
|
121
|
+
loop: true,
|
|
122
|
+
virtual: true,
|
|
123
|
+
focusItemOnOpen: false,
|
|
124
|
+
onNavigate: setActiveIndex,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const typeahead = useTypeahead(context, {
|
|
128
|
+
listRef: listContentRef,
|
|
129
|
+
activeIndex: state.activeIndex,
|
|
130
|
+
selectedIndex: state.selectedIndex,
|
|
131
|
+
findMatch(list, typedString) {
|
|
132
|
+
return list.find((item) => macthOption(item, typedString));
|
|
133
|
+
},
|
|
134
|
+
onMatch(index) {
|
|
135
|
+
state.open && setActiveIndex(index);
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions(
|
|
140
|
+
[navigation, typeahead],
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const referenceProps = useMemo(() => {
|
|
144
|
+
const props: AnyObject = getReferenceProps();
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
...props,
|
|
148
|
+
onBlur(evt: React.FocusEvent) {
|
|
149
|
+
if (
|
|
150
|
+
evt.relatedTarget?.closest(`[data-select-dropdown="${dropdownKey}"]`)
|
|
151
|
+
) {
|
|
152
|
+
refs.domReference.current?.querySelector("input")?.focus();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
handleQuery("");
|
|
156
|
+
},
|
|
157
|
+
onKeyDown(evt: React.KeyboardEvent) {
|
|
158
|
+
if (evt.key === "Enter") {
|
|
159
|
+
if (state.activeIndex !== null) {
|
|
160
|
+
handleChange(listContentRef.current[state.activeIndex]);
|
|
161
|
+
evt.preventDefault();
|
|
162
|
+
evt.stopPropagation();
|
|
163
|
+
return false;
|
|
164
|
+
} else if (!state.query) {
|
|
165
|
+
onEnterPressed?.(evt);
|
|
166
|
+
}
|
|
167
|
+
} else if (evt.key === "Backspace") {
|
|
168
|
+
!state.query && handleRemove();
|
|
169
|
+
} else if (["ArrowUp", "ArrowDown"].includes(evt.key)) {
|
|
170
|
+
if (state.activeIndex === null) {
|
|
171
|
+
setActiveIndex(state.selectedIndex);
|
|
172
|
+
}
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
props.onKeyDown?.(evt);
|
|
175
|
+
}, 50);
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}, [
|
|
180
|
+
getReferenceProps,
|
|
181
|
+
onEnterPressed,
|
|
182
|
+
handleQuery,
|
|
183
|
+
refs,
|
|
184
|
+
state.query,
|
|
185
|
+
state.activeIndex,
|
|
186
|
+
]);
|
|
187
|
+
|
|
188
|
+
const floatingProps = useMemo(() => {
|
|
189
|
+
return getFloatingProps({
|
|
190
|
+
"data-select-dropdown": dropdownKey,
|
|
191
|
+
} as AnyObject);
|
|
192
|
+
}, [getFloatingProps]);
|
|
193
|
+
|
|
194
|
+
const makeItemProps = useCallback(
|
|
195
|
+
(item: AnyObject) => {
|
|
196
|
+
const selected = isArray(state.value)
|
|
197
|
+
? state.value?.includes?.(item)
|
|
198
|
+
: state.value === item;
|
|
199
|
+
return {
|
|
200
|
+
...getItemProps({
|
|
201
|
+
"data-selected": selected ? true : undefined,
|
|
202
|
+
onClick() {
|
|
203
|
+
handleChange(item);
|
|
204
|
+
},
|
|
205
|
+
} as AnyObject),
|
|
206
|
+
"aria-hidden": undefined,
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
[getItemProps, handleChange, state.value],
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const displayValue = useMemo(() => {
|
|
213
|
+
if (isArray(state.value)) {
|
|
214
|
+
return state.value.map((item: AnyObject, index) => (
|
|
215
|
+
<Chip key={index} onRemove={() => handleRemove(index)}>
|
|
216
|
+
{renderer?.(item) ?? item[labelProperty] ?? item}
|
|
217
|
+
</Chip>
|
|
218
|
+
));
|
|
219
|
+
} else if (!isEmpty(state.value) && !state.query) {
|
|
220
|
+
return (
|
|
221
|
+
<span>
|
|
222
|
+
{renderer?.(state.value) ?? state.value[labelProperty] ?? state.value}
|
|
223
|
+
</span>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}, [state.value, state.query, placeholder, renderer]);
|
|
227
|
+
|
|
228
|
+
return (
|
|
229
|
+
<Wrapper {...wrapperProps}>
|
|
230
|
+
<InputWrapper
|
|
231
|
+
showClear={!isEmpty(state.value)}
|
|
232
|
+
onClear={handleChange}
|
|
233
|
+
invalid={invalid}
|
|
234
|
+
readOnly={readOnly}
|
|
235
|
+
disabled={disabled}
|
|
236
|
+
required={required}
|
|
237
|
+
error={error}
|
|
238
|
+
className="z-1 rounded-b-none"
|
|
239
|
+
data-inner={true}
|
|
240
|
+
wrapperRef={refs.setReference}
|
|
241
|
+
>
|
|
242
|
+
<div
|
|
243
|
+
data-select-display="true"
|
|
244
|
+
className="group flex-1 py-1 px-2 truncate text-start flex gap-1 flex-wrap"
|
|
245
|
+
>
|
|
246
|
+
{!hideSelected && displayValue}
|
|
247
|
+
<input
|
|
248
|
+
disabled={disabled}
|
|
249
|
+
value={state.query ?? ""}
|
|
250
|
+
aria-invalid={invalid}
|
|
251
|
+
data-testid={name}
|
|
252
|
+
name={name}
|
|
253
|
+
className={classNames(
|
|
254
|
+
"appearance-none bg-transparent outline-none border-none ring-0 flex-1 min-w-24",
|
|
255
|
+
disabled && "cursor-not-allowed pointer-events-none",
|
|
256
|
+
)}
|
|
257
|
+
placeholder={isEmpty(state.value) ? placeholder : ""}
|
|
258
|
+
{...referenceProps}
|
|
259
|
+
onChange={(evt) => handleQuery(evt.target.value)}
|
|
260
|
+
/>
|
|
261
|
+
</div>
|
|
262
|
+
</InputWrapper>
|
|
263
|
+
<FloatingFocusManager
|
|
264
|
+
context={context}
|
|
265
|
+
initialFocus={-1}
|
|
266
|
+
visuallyHiddenDismiss
|
|
267
|
+
>
|
|
268
|
+
<Options
|
|
269
|
+
ref={refs.setFloating}
|
|
270
|
+
className={classNames(
|
|
271
|
+
"max-h-96 z-0 rounded-capped rounded-t-none outline",
|
|
272
|
+
disabled && "pointer-events-none opacity-65",
|
|
273
|
+
)}
|
|
274
|
+
style={{
|
|
275
|
+
height,
|
|
276
|
+
}}
|
|
277
|
+
{...floatingProps}
|
|
278
|
+
items={state.items}
|
|
279
|
+
active={state.activeIndex}
|
|
280
|
+
itemRef={setItemRef}
|
|
281
|
+
itemProps={makeItemProps}
|
|
282
|
+
>
|
|
283
|
+
{(item) => renderer?.(item) ?? item[labelProperty] ?? item}
|
|
284
|
+
</Options>
|
|
285
|
+
</FloatingFocusManager>
|
|
286
|
+
</Wrapper>
|
|
287
|
+
);
|
|
288
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
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 { EmptyContent } from "@react-fabric/core";
|
|
25
|
+
import classNames from "classnames";
|
|
26
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
27
|
+
import { Trans } from "react-i18next";
|
|
28
|
+
|
|
29
|
+
export const Options = ({
|
|
30
|
+
ref,
|
|
31
|
+
style,
|
|
32
|
+
items,
|
|
33
|
+
active,
|
|
34
|
+
className,
|
|
35
|
+
itemRef,
|
|
36
|
+
empty,
|
|
37
|
+
valueProperty = "id",
|
|
38
|
+
labelProperty = "label",
|
|
39
|
+
itemProps,
|
|
40
|
+
children,
|
|
41
|
+
...rest
|
|
42
|
+
}: {
|
|
43
|
+
ref: AnyObject;
|
|
44
|
+
itemRef: AnyObject;
|
|
45
|
+
style?: AnyObject;
|
|
46
|
+
items: AnyObject[];
|
|
47
|
+
active: number | null;
|
|
48
|
+
className?: string;
|
|
49
|
+
empty?: ReactElement;
|
|
50
|
+
valueProperty?: AnyObject;
|
|
51
|
+
labelProperty?: AnyObject;
|
|
52
|
+
itemProps: (item: AnyObject) => AnyObject;
|
|
53
|
+
children: (item: AnyObject, label: string) => ReactNode;
|
|
54
|
+
} & KeyValue) => {
|
|
55
|
+
return (
|
|
56
|
+
<div
|
|
57
|
+
ref={ref}
|
|
58
|
+
className={classNames(
|
|
59
|
+
"bg-base select-none overflow-x-hidden overflow-y-auto",
|
|
60
|
+
className,
|
|
61
|
+
)}
|
|
62
|
+
style={style}
|
|
63
|
+
{...rest}
|
|
64
|
+
>
|
|
65
|
+
{items.length === 0 &&
|
|
66
|
+
(empty ?? (
|
|
67
|
+
<EmptyContent message={<Trans i18nKey="form:select.emptyList" />} />
|
|
68
|
+
))}
|
|
69
|
+
{items.map((item, index) => {
|
|
70
|
+
if (item.___group___)
|
|
71
|
+
return (
|
|
72
|
+
<p
|
|
73
|
+
key={index}
|
|
74
|
+
className="px-2 py-1 text-muted text-xs sticky -top-px z-2 bg-base border-t"
|
|
75
|
+
>
|
|
76
|
+
{item.label}
|
|
77
|
+
</p>
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (item.___create___)
|
|
81
|
+
return (
|
|
82
|
+
<div
|
|
83
|
+
role="none"
|
|
84
|
+
key={index}
|
|
85
|
+
tabIndex={active === index ? 0 : -1}
|
|
86
|
+
ref={(el: AnyObject) => itemRef(index, el)}
|
|
87
|
+
data-active={active === index ? true : undefined}
|
|
88
|
+
className={classNames(
|
|
89
|
+
"block text-start truncate px-2 py-1 leading-normal cursor-pointer select-none",
|
|
90
|
+
"data-[active]:bg-primary-200 data-[selected]:bg-primary-600 data-[selected]:text-white data-[active]:data-[selected]:underline",
|
|
91
|
+
)}
|
|
92
|
+
{...itemProps(item)}
|
|
93
|
+
>
|
|
94
|
+
<Trans
|
|
95
|
+
i18nKey="form:select.createOption"
|
|
96
|
+
values={{ query: item.value }}
|
|
97
|
+
components={[<strong key="t0">query</strong>]}
|
|
98
|
+
/>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<div
|
|
104
|
+
role="none"
|
|
105
|
+
key={index}
|
|
106
|
+
tabIndex={active === index ? 0 : -1}
|
|
107
|
+
ref={(el: AnyObject) => itemRef(index, el)}
|
|
108
|
+
data-active={active === index ? true : undefined}
|
|
109
|
+
className={classNames(
|
|
110
|
+
"block text-start truncate px-2 py-1 leading-normal cursor-pointer",
|
|
111
|
+
"data-[active]:bg-primary-200 data-[selected]:bg-primary-600 data-[selected]:text-white data-[active]:data-[selected]:underline",
|
|
112
|
+
)}
|
|
113
|
+
{...itemProps(item)}
|
|
114
|
+
>
|
|
115
|
+
{children(item, item[labelProperty] ?? item)}
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
})}
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
};
|