@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,413 @@
|
|
|
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 { useDebounce } from "@react-fabric/core";
|
|
25
|
+
import {
|
|
26
|
+
dedupe,
|
|
27
|
+
EMPTY_ARRAY,
|
|
28
|
+
groupBy,
|
|
29
|
+
isArray,
|
|
30
|
+
matchString,
|
|
31
|
+
} from "@react-fabric/utilities";
|
|
32
|
+
import {
|
|
33
|
+
useCallback,
|
|
34
|
+
useDeferredValue,
|
|
35
|
+
useEffect,
|
|
36
|
+
useMemo,
|
|
37
|
+
useReducer,
|
|
38
|
+
useRef,
|
|
39
|
+
} from "react";
|
|
40
|
+
import { getSelectedValue, makeOption } from "./helpers";
|
|
41
|
+
|
|
42
|
+
type StateActions =
|
|
43
|
+
| {
|
|
44
|
+
type: "open";
|
|
45
|
+
}
|
|
46
|
+
| { type: "close" }
|
|
47
|
+
| {
|
|
48
|
+
type: "options";
|
|
49
|
+
options: AnyObject[];
|
|
50
|
+
query?: string;
|
|
51
|
+
open?: boolean;
|
|
52
|
+
setActive?: true;
|
|
53
|
+
}
|
|
54
|
+
| { type: "startQuery"; query: string }
|
|
55
|
+
| { type: "setValue"; value: AnyObject }
|
|
56
|
+
| { type: "changeValue"; value: AnyObject }
|
|
57
|
+
| { type: "removeItem"; index?: number }
|
|
58
|
+
| { type: "active"; index: number | null };
|
|
59
|
+
|
|
60
|
+
interface State {
|
|
61
|
+
multiple: boolean;
|
|
62
|
+
emptyValue: AnyObject;
|
|
63
|
+
open: boolean;
|
|
64
|
+
loading: boolean;
|
|
65
|
+
activeIndex: number | null;
|
|
66
|
+
selectedIndex: number | null;
|
|
67
|
+
value: AnyObject;
|
|
68
|
+
// filter
|
|
69
|
+
query?: string;
|
|
70
|
+
// dropdown items
|
|
71
|
+
items: AnyObject[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const useSelect = ({
|
|
75
|
+
options = EMPTY_ARRAY,
|
|
76
|
+
value,
|
|
77
|
+
autoComplete,
|
|
78
|
+
multiple = false,
|
|
79
|
+
allowCreate = false,
|
|
80
|
+
groupProperty = "",
|
|
81
|
+
valueProperty = "id",
|
|
82
|
+
labelProperty = "label",
|
|
83
|
+
createOption = makeOption,
|
|
84
|
+
matcher,
|
|
85
|
+
onQuery,
|
|
86
|
+
onChange,
|
|
87
|
+
onSelect,
|
|
88
|
+
}: {
|
|
89
|
+
value?: AnyObject;
|
|
90
|
+
options?: AnyObject[];
|
|
91
|
+
multiple?: boolean;
|
|
92
|
+
allowCreate?: boolean;
|
|
93
|
+
groupProperty?: AnyObject;
|
|
94
|
+
valueProperty?: AnyObject;
|
|
95
|
+
labelProperty?: AnyObject;
|
|
96
|
+
autoComplete?: boolean;
|
|
97
|
+
matcher?: (item: AnyObject, query: string) => boolean;
|
|
98
|
+
createOption?: (query: string) => AnyObject;
|
|
99
|
+
onQuery?: (query: string) => AnyObject;
|
|
100
|
+
onSelect?: (item: AnyObject) => void;
|
|
101
|
+
onChange?: (value: AnyObject) => void;
|
|
102
|
+
}) => {
|
|
103
|
+
// value state
|
|
104
|
+
const emptyValue = useMemo(() => (multiple ? [] : ""), [multiple]);
|
|
105
|
+
const deferred = useDeferredValue(value ?? emptyValue);
|
|
106
|
+
|
|
107
|
+
// list items
|
|
108
|
+
const listRef = useRef<HTMLElement[]>([]);
|
|
109
|
+
const focusableRef = useRef<HTMLElement>(null);
|
|
110
|
+
const listContentRef = useRef<AnyObject[]>([]);
|
|
111
|
+
const optionsRef = useRef<AnyObject[]>([]);
|
|
112
|
+
|
|
113
|
+
const macthOption = useCallback(
|
|
114
|
+
(item: AnyObject, query: string) => {
|
|
115
|
+
return (
|
|
116
|
+
matcher?.(item, query) ??
|
|
117
|
+
matchString(item[labelProperty] ?? item, query)
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
[matcher],
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const fireChange = useCallback(
|
|
124
|
+
(value: AnyObject) => {
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
onSelect?.(value ?? emptyValue);
|
|
127
|
+
onChange?.(getSelectedValue(value, emptyValue, valueProperty));
|
|
128
|
+
}, 50);
|
|
129
|
+
},
|
|
130
|
+
[emptyValue, onSelect, onChange],
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const [state, dispatch] = useReducer(
|
|
134
|
+
(state: State, action: StateActions) => {
|
|
135
|
+
if (action.type === "open") {
|
|
136
|
+
const index = state.items.indexOf(
|
|
137
|
+
state.multiple ? state.value[0] : state.value,
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
...state,
|
|
141
|
+
open: true,
|
|
142
|
+
activeIndex: index > -1 ? index : null,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (action.type === "close") {
|
|
146
|
+
return { ...state, activeIndex: null, open: false };
|
|
147
|
+
}
|
|
148
|
+
if (action.type === "active" && state.open) {
|
|
149
|
+
if (autoComplete && action.index !== null)
|
|
150
|
+
state.query = state.items[action.index];
|
|
151
|
+
return { ...state, activeIndex: action.index };
|
|
152
|
+
}
|
|
153
|
+
if (action.type === "setValue") {
|
|
154
|
+
return {
|
|
155
|
+
...state,
|
|
156
|
+
value: action.value,
|
|
157
|
+
selectedIndex: state.items.indexOf(
|
|
158
|
+
state.multiple ? action.value[0] : action.value,
|
|
159
|
+
),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (action.type === "changeValue") {
|
|
163
|
+
const newState = {
|
|
164
|
+
...state,
|
|
165
|
+
value: state.emptyValue,
|
|
166
|
+
activeIndex: null,
|
|
167
|
+
selectedIndex: autoComplete
|
|
168
|
+
? state.multiple
|
|
169
|
+
? action.value?.length
|
|
170
|
+
: 0
|
|
171
|
+
: state.items.indexOf(action.value),
|
|
172
|
+
};
|
|
173
|
+
if (action.value !== undefined) {
|
|
174
|
+
if (multiple) {
|
|
175
|
+
const values = [];
|
|
176
|
+
if (action.value && autoComplete)
|
|
177
|
+
values.push(...action.value.split(/,\s?/));
|
|
178
|
+
else if (action.value) values.push(action.value);
|
|
179
|
+
newState.value = [...state.value];
|
|
180
|
+
values.forEach((value: AnyObject) => {
|
|
181
|
+
newState.value = newState.value.includes?.(value)
|
|
182
|
+
? newState.value.filter((item: AnyObject) => item !== value)
|
|
183
|
+
: [...newState.value, value];
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
newState.value = action.value;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!state.multiple) newState.open = false;
|
|
190
|
+
fireChange(newState.value);
|
|
191
|
+
return newState;
|
|
192
|
+
}
|
|
193
|
+
if (action.type === "removeItem") {
|
|
194
|
+
let newValue = state.value;
|
|
195
|
+
|
|
196
|
+
if (state.multiple) {
|
|
197
|
+
newValue = [...newValue];
|
|
198
|
+
newValue.splice(action.index ?? -1, 1);
|
|
199
|
+
} else {
|
|
200
|
+
newValue = "";
|
|
201
|
+
}
|
|
202
|
+
fireChange(newValue);
|
|
203
|
+
return {
|
|
204
|
+
...state,
|
|
205
|
+
value: newValue,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (action.type === "startQuery") {
|
|
209
|
+
return {
|
|
210
|
+
...state,
|
|
211
|
+
loading: true,
|
|
212
|
+
query: action.query,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if (action.type === "options") {
|
|
216
|
+
const newState = {
|
|
217
|
+
...state,
|
|
218
|
+
loading: false,
|
|
219
|
+
query: action.query,
|
|
220
|
+
};
|
|
221
|
+
let optionList = [...action.options];
|
|
222
|
+
if (autoComplete && action.query !== undefined) {
|
|
223
|
+
optionList = dedupe([action.query, ...optionList]);
|
|
224
|
+
}
|
|
225
|
+
const grouped = groupBy(optionList, groupProperty, "");
|
|
226
|
+
if (action.open) newState.open = true;
|
|
227
|
+
newState.items = [];
|
|
228
|
+
// add new item if allowCreate
|
|
229
|
+
if (allowCreate && action.query)
|
|
230
|
+
newState.items.push({
|
|
231
|
+
___create___: true,
|
|
232
|
+
value: createOption(action.query),
|
|
233
|
+
});
|
|
234
|
+
Object.entries(grouped).forEach(([key, list]) => {
|
|
235
|
+
if (key) {
|
|
236
|
+
newState.items.push({ ___group___: true, label: key });
|
|
237
|
+
}
|
|
238
|
+
newState.items.push(...list);
|
|
239
|
+
});
|
|
240
|
+
if (action.setActive) {
|
|
241
|
+
const index = newState.items.findIndex(
|
|
242
|
+
(opt: AnyObject) => !opt.___group___,
|
|
243
|
+
);
|
|
244
|
+
newState.activeIndex = index >= 0 ? index : null;
|
|
245
|
+
} else {
|
|
246
|
+
newState.activeIndex = null;
|
|
247
|
+
}
|
|
248
|
+
listContentRef.current = newState.items;
|
|
249
|
+
return newState;
|
|
250
|
+
}
|
|
251
|
+
return state;
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
multiple,
|
|
255
|
+
emptyValue,
|
|
256
|
+
open: false,
|
|
257
|
+
loading: false,
|
|
258
|
+
activeIndex: null,
|
|
259
|
+
selectedIndex: null,
|
|
260
|
+
value: emptyValue,
|
|
261
|
+
// filter
|
|
262
|
+
query: undefined,
|
|
263
|
+
// dropdown items
|
|
264
|
+
items: [],
|
|
265
|
+
},
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
useEffect(() => {
|
|
269
|
+
if (!multiple) {
|
|
270
|
+
const newOption =
|
|
271
|
+
(allowCreate || autoComplete) && deferred
|
|
272
|
+
? createOption(deferred)
|
|
273
|
+
: undefined;
|
|
274
|
+
const hasValue = options.find(
|
|
275
|
+
(opt: AnyObject) => (opt[valueProperty] ?? opt) === deferred,
|
|
276
|
+
);
|
|
277
|
+
optionsRef.current = options;
|
|
278
|
+
// if current value missing in list inject it
|
|
279
|
+
if (!hasValue && newOption) {
|
|
280
|
+
optionsRef.current = [newOption, ...(options ?? [])];
|
|
281
|
+
}
|
|
282
|
+
dispatch({ type: "options", options: optionsRef.current });
|
|
283
|
+
dispatch({
|
|
284
|
+
type: "setValue",
|
|
285
|
+
value: hasValue ?? newOption ?? "",
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
if (multiple) {
|
|
289
|
+
optionsRef.current = [];
|
|
290
|
+
const value = (deferred ?? []).map((item: AnyObject) => {
|
|
291
|
+
const newOption =
|
|
292
|
+
(allowCreate || autoComplete) && item
|
|
293
|
+
? createOption(item)
|
|
294
|
+
: undefined;
|
|
295
|
+
const hasValue = options.find(
|
|
296
|
+
(opt: AnyObject) => (opt[valueProperty] ?? opt) === deferred,
|
|
297
|
+
);
|
|
298
|
+
if (!hasValue && newOption) {
|
|
299
|
+
optionsRef.current.push(newOption);
|
|
300
|
+
}
|
|
301
|
+
return hasValue ?? newOption ?? "";
|
|
302
|
+
});
|
|
303
|
+
optionsRef.current = [...optionsRef.current, ...(options ?? [])];
|
|
304
|
+
dispatch({ type: "options", options: optionsRef.current });
|
|
305
|
+
dispatch({
|
|
306
|
+
type: "setValue",
|
|
307
|
+
value,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}, [deferred, options, multiple, allowCreate, autoComplete, createOption]);
|
|
311
|
+
|
|
312
|
+
const fireQuery = useDebounce(
|
|
313
|
+
(query: string) => {
|
|
314
|
+
void Promise.resolve(onQuery?.(query)).then((options) => {
|
|
315
|
+
const newOptions = [...options];
|
|
316
|
+
// if autocomplete inject other filtered options from history
|
|
317
|
+
if (autoComplete) {
|
|
318
|
+
newOptions.push(
|
|
319
|
+
...optionsRef.current.filter((opt: AnyObject) => {
|
|
320
|
+
return macthOption?.(opt, query);
|
|
321
|
+
}),
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
dispatch({
|
|
325
|
+
type: "options",
|
|
326
|
+
query,
|
|
327
|
+
options: newOptions,
|
|
328
|
+
open: true,
|
|
329
|
+
setActive: true,
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
},
|
|
333
|
+
[onQuery],
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
// handlers
|
|
337
|
+
const handleQuery = useCallback(
|
|
338
|
+
(query: string) => {
|
|
339
|
+
if (!query) {
|
|
340
|
+
dispatch({ type: "options", query: "", options: optionsRef.current });
|
|
341
|
+
} else {
|
|
342
|
+
if (onQuery) {
|
|
343
|
+
dispatch({ type: "startQuery", query });
|
|
344
|
+
return fireQuery(query);
|
|
345
|
+
}
|
|
346
|
+
dispatch({
|
|
347
|
+
type: "options",
|
|
348
|
+
query,
|
|
349
|
+
open: true,
|
|
350
|
+
setActive: true,
|
|
351
|
+
options: optionsRef.current.filter((opt: AnyObject) => {
|
|
352
|
+
return macthOption?.(opt, query);
|
|
353
|
+
}),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
[macthOption, onQuery],
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
const handleChange = useCallback((value?: AnyObject) => {
|
|
361
|
+
let values = value;
|
|
362
|
+
if (autoComplete && multiple && value) {
|
|
363
|
+
values = value.split(/,\s?/);
|
|
364
|
+
}
|
|
365
|
+
dispatch({
|
|
366
|
+
type: "changeValue",
|
|
367
|
+
value: value?.___create___ ? value.value : value,
|
|
368
|
+
});
|
|
369
|
+
if (value?.___create___) {
|
|
370
|
+
optionsRef.current = [...optionsRef.current, createOption(value.value)];
|
|
371
|
+
}
|
|
372
|
+
if (autoComplete) {
|
|
373
|
+
optionsRef.current = dedupe([
|
|
374
|
+
...(isArray(values) ? values : [values]),
|
|
375
|
+
...optionsRef.current,
|
|
376
|
+
]);
|
|
377
|
+
}
|
|
378
|
+
dispatch({ type: "options", options: optionsRef.current });
|
|
379
|
+
}, []);
|
|
380
|
+
|
|
381
|
+
const handleRemove = useCallback((index?: number) => {
|
|
382
|
+
dispatch({ type: "removeItem", index });
|
|
383
|
+
}, []);
|
|
384
|
+
|
|
385
|
+
const setOpen = useCallback((open: boolean) => {
|
|
386
|
+
dispatch({ type: open ? "open" : "close" });
|
|
387
|
+
dispatch({
|
|
388
|
+
type: "options",
|
|
389
|
+
query: undefined,
|
|
390
|
+
options: optionsRef.current,
|
|
391
|
+
});
|
|
392
|
+
}, []);
|
|
393
|
+
|
|
394
|
+
const setActiveIndex = useCallback((index: number | null) => {
|
|
395
|
+
dispatch({ type: "active", index });
|
|
396
|
+
}, []);
|
|
397
|
+
|
|
398
|
+
return {
|
|
399
|
+
state,
|
|
400
|
+
listRef,
|
|
401
|
+
focusableRef,
|
|
402
|
+
listContentRef,
|
|
403
|
+
handleChange,
|
|
404
|
+
handleRemove,
|
|
405
|
+
handleQuery,
|
|
406
|
+
macthOption,
|
|
407
|
+
setActiveIndex,
|
|
408
|
+
setOpen,
|
|
409
|
+
setItemRef: (index: number, node: HTMLElement) => {
|
|
410
|
+
listRef.current[index] = node;
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
};
|
|
@@ -0,0 +1,203 @@
|
|
|
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 { type RefProp } from "@react-fabric/core/dist/types/types";
|
|
25
|
+
|
|
26
|
+
export interface InputProps<V = string> {
|
|
27
|
+
/**
|
|
28
|
+
* value
|
|
29
|
+
*/
|
|
30
|
+
value?: V;
|
|
31
|
+
/**
|
|
32
|
+
* change handler
|
|
33
|
+
*/
|
|
34
|
+
onChange?: (value?: V) => void;
|
|
35
|
+
/**
|
|
36
|
+
* on focus event handler
|
|
37
|
+
*/
|
|
38
|
+
onFocus?: React.FocusEventHandler;
|
|
39
|
+
/**
|
|
40
|
+
* on blur event handler
|
|
41
|
+
*/
|
|
42
|
+
onBlur?: React.FocusEventHandler;
|
|
43
|
+
/**
|
|
44
|
+
* on enter press callback
|
|
45
|
+
*/
|
|
46
|
+
onEnterPressed?: React.KeyboardEventHandler;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* input name
|
|
50
|
+
*/
|
|
51
|
+
name?: string;
|
|
52
|
+
/**
|
|
53
|
+
* input label
|
|
54
|
+
*/
|
|
55
|
+
label?: string;
|
|
56
|
+
/**
|
|
57
|
+
* input placeholder
|
|
58
|
+
*/
|
|
59
|
+
placeholder?: string;
|
|
60
|
+
/**
|
|
61
|
+
* append to label end
|
|
62
|
+
*/
|
|
63
|
+
appendLabel?: JSX.Element | string | number | boolean;
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
decorateStart?: JSX.Element | string | number | boolean;
|
|
68
|
+
/**
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
decorateEnd?: JSX.Element | string | number | boolean;
|
|
72
|
+
/**
|
|
73
|
+
* info label at bottom
|
|
74
|
+
*/
|
|
75
|
+
info?: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* required
|
|
79
|
+
*/
|
|
80
|
+
required?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* disabled input
|
|
83
|
+
*/
|
|
84
|
+
disabled?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* read-only input
|
|
87
|
+
*/
|
|
88
|
+
readOnly?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* auto focus when rendered
|
|
91
|
+
*/
|
|
92
|
+
autoFocus?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* show clear button
|
|
95
|
+
*/
|
|
96
|
+
allowClear?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* invalid value input
|
|
99
|
+
*/
|
|
100
|
+
invalid?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* error message
|
|
103
|
+
*/
|
|
104
|
+
error?: string;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* field width
|
|
108
|
+
*/
|
|
109
|
+
width?: number | string;
|
|
110
|
+
/**
|
|
111
|
+
* inline label and input
|
|
112
|
+
*/
|
|
113
|
+
inline?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* label width for inline
|
|
116
|
+
*/
|
|
117
|
+
labelWidth?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface BaseSelectProps<T>
|
|
121
|
+
extends RefProp<HTMLInputElement>,
|
|
122
|
+
Omit<InputProps, "value" | "onChange"> {
|
|
123
|
+
/**
|
|
124
|
+
* list options
|
|
125
|
+
*/
|
|
126
|
+
options?: T[];
|
|
127
|
+
/**
|
|
128
|
+
* group by property name
|
|
129
|
+
*/
|
|
130
|
+
groupProperty?: keyof T;
|
|
131
|
+
/**
|
|
132
|
+
* label property name
|
|
133
|
+
*/
|
|
134
|
+
labelProperty?: keyof T;
|
|
135
|
+
/**
|
|
136
|
+
* value property name
|
|
137
|
+
*/
|
|
138
|
+
valueProperty?: keyof T;
|
|
139
|
+
/**
|
|
140
|
+
* searchable list
|
|
141
|
+
*/
|
|
142
|
+
searchable?: boolean;
|
|
143
|
+
/**
|
|
144
|
+
* allow create item
|
|
145
|
+
*/
|
|
146
|
+
allowCreate?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* create new item object
|
|
149
|
+
*/
|
|
150
|
+
createOption?: (query: string) => T;
|
|
151
|
+
/**
|
|
152
|
+
* query handler
|
|
153
|
+
*/
|
|
154
|
+
onQuery?: (query: string) => T[] | Promise<T[]>;
|
|
155
|
+
/**
|
|
156
|
+
* item renderer
|
|
157
|
+
*/
|
|
158
|
+
renderer?: (option: T) => React.ReactNode;
|
|
159
|
+
/**
|
|
160
|
+
* item matcher
|
|
161
|
+
*/
|
|
162
|
+
matcher?: (option: T, query: string) => boolean;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export type SelectProps<T> = BaseSelectProps<T> &
|
|
166
|
+
(
|
|
167
|
+
| {
|
|
168
|
+
/**
|
|
169
|
+
* is multiple selection
|
|
170
|
+
*/
|
|
171
|
+
multiple?: never | false;
|
|
172
|
+
/**
|
|
173
|
+
* selected value
|
|
174
|
+
*/
|
|
175
|
+
value?: string;
|
|
176
|
+
/**
|
|
177
|
+
* select handler
|
|
178
|
+
*/
|
|
179
|
+
onSelect?: (option: T) => void;
|
|
180
|
+
/**
|
|
181
|
+
* change handler
|
|
182
|
+
*/
|
|
183
|
+
onChange?: (value?: string) => void;
|
|
184
|
+
}
|
|
185
|
+
| {
|
|
186
|
+
/**
|
|
187
|
+
* is multiple selection
|
|
188
|
+
*/
|
|
189
|
+
multiple: true;
|
|
190
|
+
/**
|
|
191
|
+
* selected value
|
|
192
|
+
*/
|
|
193
|
+
value?: string[];
|
|
194
|
+
/**
|
|
195
|
+
* select handler
|
|
196
|
+
*/
|
|
197
|
+
onSelect?: (option: T[]) => void;
|
|
198
|
+
/**
|
|
199
|
+
* change handler
|
|
200
|
+
*/
|
|
201
|
+
onChange?: (value?: string[]) => void;
|
|
202
|
+
}
|
|
203
|
+
);
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
interface BaseSchemaDef {
|
|
25
|
+
id: string;
|
|
26
|
+
label: string;
|
|
27
|
+
datatype: string;
|
|
28
|
+
required?: boolean;
|
|
29
|
+
multiple?: boolean;
|
|
30
|
+
defaultValue?: AnyObject;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface SchemaDef extends BaseSchemaDef {
|
|
34
|
+
datatype: "text" | "color";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface BooleanSchema extends BaseSchemaDef {
|
|
38
|
+
datatype: "boolean";
|
|
39
|
+
defaultValue?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface StringSchema extends BaseSchemaDef {
|
|
43
|
+
datatype: "string";
|
|
44
|
+
options?: string[];
|
|
45
|
+
regex?: string;
|
|
46
|
+
identifierType?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface RangeSchema extends BaseSchemaDef {
|
|
50
|
+
datatype: "range";
|
|
51
|
+
min?: number;
|
|
52
|
+
max?: number;
|
|
53
|
+
step?: number;
|
|
54
|
+
defaultValue?: [number, number];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface NumberSchema extends BaseSchemaDef {
|
|
58
|
+
datatype: "number";
|
|
59
|
+
min?: number;
|
|
60
|
+
max?: number;
|
|
61
|
+
step?: number;
|
|
62
|
+
defaultValue?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface DecimalSchema extends BaseSchemaDef {
|
|
66
|
+
datatype: "decimal";
|
|
67
|
+
min?: number;
|
|
68
|
+
max?: number;
|
|
69
|
+
step?: number;
|
|
70
|
+
defaultValue?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface FileSchema extends BaseSchemaDef {
|
|
74
|
+
datatype: "file";
|
|
75
|
+
accept?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface AvatarSchema extends BaseSchemaDef {
|
|
79
|
+
datatype: "avatar";
|
|
80
|
+
multiple?: false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type SchemaType =
|
|
84
|
+
| SchemaDef
|
|
85
|
+
| BooleanSchema
|
|
86
|
+
| StringSchema
|
|
87
|
+
| RangeSchema
|
|
88
|
+
| NumberSchema
|
|
89
|
+
| DecimalSchema
|
|
90
|
+
| FileSchema
|
|
91
|
+
| AvatarSchema;
|
|
92
|
+
|
|
93
|
+
export interface ChildSchema extends BaseSchemaDef {
|
|
94
|
+
datatype: "schema";
|
|
95
|
+
inline?: boolean;
|
|
96
|
+
schema: SchemaType[];
|
|
97
|
+
defaultValue?: never;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type FormSchema = SchemaType | ChildSchema;
|