@react-fabric/form 1.0.0-beta.4 → 1.0.0-beta.40
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/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
- package/css/_colorPicker.css +65 -0
- package/{src/internal/Field.module.css → css/_field.css} +26 -15
- package/{src/input/File.module.css → css/_file.css} +20 -6
- package/css/styles.css +27 -0
- package/dist/cjs/builder/FileField.js +37 -25
- package/dist/cjs/builder/FormField.js +44 -34
- package/dist/cjs/builder/SchemaEditor.js +72 -0
- package/dist/cjs/builder/components/AddSchemaFields.js +62 -0
- package/dist/cjs/builder/components/SchemaArrayForm.js +143 -0
- package/dist/cjs/builder/components/SchemaField.js +413 -0
- package/dist/cjs/form/Controller.js +1 -1
- package/dist/cjs/form/Form.js +19 -10
- package/dist/cjs/hooks/useFormBuilder.js +27 -15
- package/dist/cjs/index.js +9 -2
- package/dist/cjs/input/ArrayInput.js +36 -25
- package/dist/cjs/input/AvatarInput.js +1 -1
- package/dist/cjs/input/Checkbox.js +5 -4
- package/dist/cjs/input/ColorInput.js +70 -28
- package/dist/cjs/input/ColorPicker.js +1 -35
- package/dist/cjs/input/DateInput.js +183 -0
- package/dist/cjs/input/Field.js +4 -4
- package/dist/cjs/input/File.js +2 -3
- package/dist/cjs/input/Hidden.js +3 -1
- package/dist/cjs/input/Input.js +1 -1
- package/dist/cjs/input/Number.js +1 -1
- package/dist/cjs/input/Password.js +1 -1
- package/dist/cjs/input/Radio.js +1 -1
- package/dist/cjs/input/RadioGroup.js +6 -3
- package/dist/cjs/input/{Range.js → RangeSlider.js} +3 -3
- package/dist/cjs/input/Search.js +1 -1
- package/dist/cjs/input/Slider.js +1 -1
- package/dist/cjs/input/Switch.js +27 -5
- package/dist/cjs/input/Textarea.js +7 -3
- package/dist/cjs/internal/ErrorIcon.js +1 -1
- package/dist/cjs/internal/FieldWrapper.js +4 -3
- package/dist/cjs/internal/InputWrapper.js +14 -16
- package/dist/cjs/select/AutoComplete.js +1 -1
- package/dist/cjs/select/List.js +41 -20
- package/dist/cjs/select/Options.js +2 -2
- package/dist/cjs/select/Select.js +11 -8
- package/dist/cjs/select/useSelect.js +12 -7
- package/{src/input/ColorPicker.module.css → dist/cjs/types/schema.js} +18 -27
- package/dist/cjs/utils/getColor.js +6 -0
- package/dist/esm/builder/FileField.js +39 -27
- package/dist/esm/builder/FormField.js +44 -34
- package/dist/esm/builder/SchemaEditor.js +72 -0
- package/dist/esm/builder/components/AddSchemaFields.js +62 -0
- package/dist/esm/builder/components/SchemaArrayForm.js +143 -0
- package/dist/esm/builder/components/SchemaField.js +413 -0
- package/dist/esm/form/Controller.js +1 -1
- package/dist/esm/form/Form.js +19 -10
- package/dist/esm/hooks/useFormBuilder.js +25 -13
- package/dist/esm/index.js +4 -1
- package/dist/esm/input/ArrayInput.js +38 -27
- package/dist/esm/input/AvatarInput.js +1 -1
- package/dist/esm/input/Checkbox.js +5 -4
- package/dist/esm/input/ColorInput.js +72 -30
- package/dist/esm/input/ColorPicker.js +1 -35
- package/dist/esm/input/DateInput.js +183 -0
- package/dist/esm/input/Field.js +5 -5
- package/dist/esm/input/File.js +2 -3
- package/dist/esm/input/Hidden.js +3 -1
- package/dist/esm/input/Input.js +1 -1
- package/dist/esm/input/Number.js +1 -1
- package/dist/esm/input/Password.js +1 -1
- package/dist/esm/input/Radio.js +1 -1
- package/dist/esm/input/RadioGroup.js +7 -4
- package/dist/esm/input/{Range.js → RangeSlider.js} +3 -3
- package/dist/esm/input/Search.js +1 -1
- package/dist/esm/input/Slider.js +1 -1
- package/dist/esm/input/Switch.js +29 -7
- package/dist/esm/input/Textarea.js +7 -3
- package/dist/esm/internal/ErrorIcon.js +1 -1
- package/dist/esm/internal/FieldWrapper.js +4 -3
- package/dist/esm/internal/InputWrapper.js +14 -16
- package/dist/esm/select/AutoComplete.js +1 -1
- package/dist/esm/select/List.js +42 -21
- package/dist/esm/select/Options.js +2 -2
- package/dist/esm/select/Select.js +11 -8
- package/dist/esm/select/useSelect.js +13 -8
- package/dist/esm/types/schema.js +41 -0
- package/dist/esm/utils/getColor.js +6 -1
- package/dist/types/builder/FileField.d.ts +4 -2
- package/dist/types/builder/FormField.d.ts +3 -1
- package/dist/types/builder/SchemaEditor.d.ts +2 -0
- package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
- package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
- package/dist/types/builder/components/SchemaField.d.ts +10 -0
- package/dist/types/form/Controller.d.ts +1 -1
- package/dist/types/hooks/useFormBuilder.d.ts +3 -1
- package/dist/types/index.d.ts +4 -2
- package/dist/types/input/ArrayInput.d.ts +20 -3
- package/dist/types/input/Checkbox.d.ts +4 -3
- package/dist/types/input/ColorInput.d.ts +1 -1
- package/dist/types/input/DateInput.d.ts +19 -0
- package/dist/types/input/RadioGroup.d.ts +18 -2
- package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
- package/dist/types/input/Switch.d.ts +7 -3
- package/dist/types/input/Textarea.d.ts +5 -1
- package/dist/types/internal/InputWrapper.d.ts +1 -1
- package/dist/types/select/List.d.ts +1 -1
- package/dist/types/types/index.d.ts +9 -5
- package/dist/types/types/schema.d.ts +79 -33
- package/package.json +10 -7
- package/src/builder/FileField.tsx +53 -27
- package/src/builder/FormField.tsx +45 -32
- package/src/builder/SchemaEditor.tsx +72 -0
- package/src/builder/components/AddSchemaFields.tsx +74 -0
- package/src/builder/components/SchemaArrayForm.tsx +158 -0
- package/src/builder/components/SchemaField.tsx +435 -0
- package/src/form/Controller.tsx +2 -2
- package/src/form/Form.tsx +15 -8
- package/src/hooks/useFormBuilder.tsx +29 -15
- package/src/index.ts +10 -2
- package/src/input/ArrayInput.tsx +80 -42
- package/src/input/AvatarInput.tsx +1 -1
- package/src/input/Checkbox.tsx +5 -4
- package/src/input/ColorInput.tsx +102 -34
- package/src/input/ColorPicker.tsx +1 -35
- package/src/input/DateInput.tsx +248 -0
- package/src/input/Field.tsx +9 -11
- package/src/input/File.tsx +3 -6
- package/src/input/Hidden.tsx +4 -1
- package/src/input/Input.tsx +2 -2
- package/src/input/Number.tsx +2 -2
- package/src/input/Password.tsx +2 -2
- package/src/input/Radio.tsx +1 -1
- package/src/input/RadioGroup.tsx +27 -4
- package/src/input/{Range.tsx → RangeSlider.tsx} +2 -2
- package/src/input/Search.tsx +2 -2
- package/src/input/Slider.tsx +1 -1
- package/src/input/Switch.tsx +42 -6
- package/src/input/Textarea.tsx +11 -3
- package/src/internal/ErrorIcon.tsx +1 -1
- package/src/internal/FieldWrapper.tsx +3 -3
- package/src/internal/InputWrapper.tsx +21 -20
- package/src/select/AutoComplete.tsx +1 -1
- package/src/select/List.tsx +34 -7
- package/src/select/Options.tsx +1 -1
- package/src/select/Select.tsx +5 -2
- package/src/select/useSelect.tsx +27 -18
- package/src/types/index.ts +14 -7
- package/src/types/schema.ts +96 -41
- package/dist/cjs/input/ArrayInput.module.css +0 -1
- package/dist/cjs/input/ColorPicker.module.css +0 -1
- package/dist/cjs/input/File.module.css +0 -1
- package/dist/cjs/internal/Field.module.css +0 -1
- package/dist/esm/input/ArrayInput.module.css +0 -1
- package/dist/esm/input/ColorPicker.module.css +0 -1
- package/dist/esm/input/File.module.css +0 -1
- package/dist/esm/internal/Field.module.css +0 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 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 { DndContext, type DragEndEvent } from "@dnd-kit/core";
|
|
25
|
+
import { restrictToVerticalAxis } from "@dnd-kit/modifiers";
|
|
26
|
+
import { SortableContext } from "@dnd-kit/sortable";
|
|
27
|
+
import {
|
|
28
|
+
Button,
|
|
29
|
+
Content,
|
|
30
|
+
CoreIcons,
|
|
31
|
+
Header,
|
|
32
|
+
useOverlayService,
|
|
33
|
+
} from "@react-fabric/core";
|
|
34
|
+
import { isArray } from "@react-fabric/utilities";
|
|
35
|
+
import { type PropsWithChildren, useCallback, useState } from "react";
|
|
36
|
+
import { useFieldArray, useFormContext } from "react-hook-form";
|
|
37
|
+
import { DATA_TYPES, type SchemaEditorProps } from "../../types/schema";
|
|
38
|
+
import { AddSchemaFields } from "./AddSchemaFields";
|
|
39
|
+
import { SchemaField } from "./SchemaField";
|
|
40
|
+
|
|
41
|
+
export const SchemaArrayForm = ({
|
|
42
|
+
dynamic,
|
|
43
|
+
acceptableTypes,
|
|
44
|
+
optionLists,
|
|
45
|
+
}: Partial<SchemaEditorProps>) => {
|
|
46
|
+
const form = useFormContext();
|
|
47
|
+
const [defaultOpen, setDefaultOpen] = useState(true);
|
|
48
|
+
const { fields, append, remove, move } = useFieldArray({
|
|
49
|
+
name: "schema",
|
|
50
|
+
control: form.control,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const handleTypeChange = useCallback((index: number, val: KeyValue) => {
|
|
54
|
+
form.setValue(`schema.${index}`, val);
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
const handleRemove = useCallback((index: number) => {
|
|
58
|
+
remove(index);
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
const [AddModal, openAddfields] = useOverlayService(AddSchemaFields);
|
|
62
|
+
const handleAdd = useCallback(() => {
|
|
63
|
+
void openAddfields().then((fields) => {
|
|
64
|
+
if (isArray(fields)) {
|
|
65
|
+
const existing = form
|
|
66
|
+
.getValues()
|
|
67
|
+
.schema.map((field: KeyValue) => field.id);
|
|
68
|
+
fields.forEach((field) => {
|
|
69
|
+
if (!existing.includes(field)) {
|
|
70
|
+
append({
|
|
71
|
+
id: field,
|
|
72
|
+
datatype: DATA_TYPES.STRING,
|
|
73
|
+
label: "",
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}, []);
|
|
80
|
+
|
|
81
|
+
const Wrapper = useCallback(
|
|
82
|
+
({ children }: PropsWithChildren) => {
|
|
83
|
+
if (dynamic) {
|
|
84
|
+
// pass id list to dnd context
|
|
85
|
+
const idMap = fields.map((item) => item.id);
|
|
86
|
+
const handleDragEnd = (e: DragEndEvent) => {
|
|
87
|
+
if (e.over && e.active.id !== e.over.id) {
|
|
88
|
+
// find index of item and drop over
|
|
89
|
+
const active = idMap.indexOf(`${e.active.id}`);
|
|
90
|
+
const end = idMap.indexOf(`${e.over.id}`);
|
|
91
|
+
move(active, end);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return (
|
|
95
|
+
<DndContext
|
|
96
|
+
onDragEnd={handleDragEnd}
|
|
97
|
+
modifiers={[restrictToVerticalAxis]}
|
|
98
|
+
>
|
|
99
|
+
<SortableContext items={idMap}>{children}</SortableContext>
|
|
100
|
+
</DndContext>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return children;
|
|
105
|
+
},
|
|
106
|
+
[dynamic, fields],
|
|
107
|
+
);
|
|
108
|
+
// @ts-expect-error ignore
|
|
109
|
+
Wrapper.displayName = "SortableWrapper";
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<Wrapper>
|
|
113
|
+
<Header flex justify="end" className="gap-1 px-2">
|
|
114
|
+
{dynamic && (
|
|
115
|
+
<Button
|
|
116
|
+
size="sm"
|
|
117
|
+
variant="link"
|
|
118
|
+
icon={CoreIcons.insert}
|
|
119
|
+
aria-label="add field"
|
|
120
|
+
onClick={handleAdd}
|
|
121
|
+
/>
|
|
122
|
+
)}
|
|
123
|
+
<Button
|
|
124
|
+
size="sm"
|
|
125
|
+
variant="link"
|
|
126
|
+
icon={CoreIcons.colapseAllVar}
|
|
127
|
+
aria-label="collapse all"
|
|
128
|
+
onClick={() => setDefaultOpen(false)}
|
|
129
|
+
/>
|
|
130
|
+
<Button
|
|
131
|
+
size="sm"
|
|
132
|
+
variant="link"
|
|
133
|
+
icon={CoreIcons.expandAllVar}
|
|
134
|
+
aria-label="expand all"
|
|
135
|
+
onClick={() => setDefaultOpen(true)}
|
|
136
|
+
/>
|
|
137
|
+
</Header>
|
|
138
|
+
<Content className="divide-y">
|
|
139
|
+
{fields.map((field, index) => (
|
|
140
|
+
<SchemaField
|
|
141
|
+
key={index}
|
|
142
|
+
defaultOpen={defaultOpen}
|
|
143
|
+
id={form.getValues().schema[index].id}
|
|
144
|
+
fieldId={field.id}
|
|
145
|
+
field={`schema.${index}`}
|
|
146
|
+
dynamic={dynamic}
|
|
147
|
+
optionLists={optionLists}
|
|
148
|
+
onRemove={() => handleRemove(index)}
|
|
149
|
+
getModel={() => form.getValues().schema[index]}
|
|
150
|
+
onTypeChange={(val: KeyValue) => handleTypeChange(index, val)}
|
|
151
|
+
acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
|
|
152
|
+
/>
|
|
153
|
+
))}
|
|
154
|
+
</Content>
|
|
155
|
+
{AddModal}
|
|
156
|
+
</Wrapper>
|
|
157
|
+
);
|
|
158
|
+
};
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 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 { useSortable } from "@dnd-kit/sortable";
|
|
25
|
+
import { CSS } from "@dnd-kit/utilities";
|
|
26
|
+
import {
|
|
27
|
+
Button,
|
|
28
|
+
Collapsable,
|
|
29
|
+
Content,
|
|
30
|
+
CoreIcons,
|
|
31
|
+
Footer,
|
|
32
|
+
Modal,
|
|
33
|
+
type ModalProps,
|
|
34
|
+
ToggleButtonGroup,
|
|
35
|
+
useOverlayService,
|
|
36
|
+
} from "@react-fabric/core";
|
|
37
|
+
import { EMPTY_ARRAY } from "@react-fabric/utilities";
|
|
38
|
+
import classNames from "classnames";
|
|
39
|
+
import {
|
|
40
|
+
Fragment,
|
|
41
|
+
type PropsWithChildren,
|
|
42
|
+
useCallback,
|
|
43
|
+
useEffect,
|
|
44
|
+
useState,
|
|
45
|
+
} from "react";
|
|
46
|
+
import { Controller } from "../../form/Controller";
|
|
47
|
+
import { ArrayInput } from "../../input/ArrayInput";
|
|
48
|
+
import { Checkbox } from "../../input/Checkbox";
|
|
49
|
+
import { DateInput } from "../../input/DateInput";
|
|
50
|
+
import { Field } from "../../input/Field";
|
|
51
|
+
import { HiddenInput } from "../../input/Hidden";
|
|
52
|
+
import { Input } from "../../input/Input";
|
|
53
|
+
import { Number } from "../../input/Number";
|
|
54
|
+
import { Select } from "../../select/Select";
|
|
55
|
+
import {
|
|
56
|
+
DATA_TYPES,
|
|
57
|
+
type DateSchema,
|
|
58
|
+
type DefaultSchema,
|
|
59
|
+
type NumberSchema,
|
|
60
|
+
SCHEMA_CUSTOM_LIST,
|
|
61
|
+
type SchemaEditorProps,
|
|
62
|
+
type StringSchema,
|
|
63
|
+
type ValueType,
|
|
64
|
+
} from "../../types/schema";
|
|
65
|
+
|
|
66
|
+
export const SchemaField = ({
|
|
67
|
+
id,
|
|
68
|
+
field,
|
|
69
|
+
fieldId,
|
|
70
|
+
getModel,
|
|
71
|
+
onRemove,
|
|
72
|
+
defaultOpen = true,
|
|
73
|
+
optionLists = EMPTY_ARRAY,
|
|
74
|
+
dynamic,
|
|
75
|
+
onTypeChange,
|
|
76
|
+
acceptableTypes,
|
|
77
|
+
}: {
|
|
78
|
+
id: string;
|
|
79
|
+
field: string;
|
|
80
|
+
fieldId: string;
|
|
81
|
+
defaultOpen?: boolean;
|
|
82
|
+
onRemove: () => void;
|
|
83
|
+
getModel: () => KeyValue;
|
|
84
|
+
onTypeChange: (val: KeyValue) => void;
|
|
85
|
+
} & Omit<SchemaEditorProps, "schemaDef" | "onChange">) => {
|
|
86
|
+
const [type, setType] = useState<DATA_TYPES>();
|
|
87
|
+
const [multiple, setMultiple] = useState(false);
|
|
88
|
+
const [hasOptions, setHasOptions] = useState(false);
|
|
89
|
+
const [customOptions, setCustomOptions] = useState(false);
|
|
90
|
+
|
|
91
|
+
const SortableItem = useCallback(
|
|
92
|
+
({
|
|
93
|
+
id,
|
|
94
|
+
children,
|
|
95
|
+
className,
|
|
96
|
+
}: PropsWithChildren<{ id: AnyObject; className: string }>) => {
|
|
97
|
+
if (dynamic) {
|
|
98
|
+
const {
|
|
99
|
+
attributes,
|
|
100
|
+
listeners,
|
|
101
|
+
setNodeRef,
|
|
102
|
+
transform,
|
|
103
|
+
transition,
|
|
104
|
+
isDragging,
|
|
105
|
+
} = useSortable({ id });
|
|
106
|
+
return (
|
|
107
|
+
<div
|
|
108
|
+
className={className}
|
|
109
|
+
ref={setNodeRef}
|
|
110
|
+
style={{
|
|
111
|
+
transform: CSS.Transform.toString(transform),
|
|
112
|
+
transition,
|
|
113
|
+
}}
|
|
114
|
+
>
|
|
115
|
+
<div
|
|
116
|
+
className={classNames(
|
|
117
|
+
"invisible group-hover/field:visible my-2 w-4",
|
|
118
|
+
isDragging ? "cursor-grabbing" : "cursor-grab",
|
|
119
|
+
)}
|
|
120
|
+
{...attributes}
|
|
121
|
+
{...listeners}
|
|
122
|
+
style={{
|
|
123
|
+
backgroundImage: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='4' width='4'%3E%3Ccircle cx='2' cy='2' r='1' fill='%238888' /%3E%3C/svg%3E ")`,
|
|
124
|
+
}}
|
|
125
|
+
/>
|
|
126
|
+
{children}
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return <div className={className}>{children}</div>;
|
|
132
|
+
},
|
|
133
|
+
[dynamic],
|
|
134
|
+
);
|
|
135
|
+
// @ts-expect-error ignore
|
|
136
|
+
SortableItem.displayName = "SortableItem";
|
|
137
|
+
|
|
138
|
+
const [OptionModal, openOptionModal] = useOverlayService(
|
|
139
|
+
(props: ModalProps) => (
|
|
140
|
+
<Modal {...props}>
|
|
141
|
+
<Content>
|
|
142
|
+
<ArrayInput name={`${field}.options`} onAdd={() => String("")}>
|
|
143
|
+
<Controller>
|
|
144
|
+
<Input />
|
|
145
|
+
</Controller>
|
|
146
|
+
</ArrayInput>
|
|
147
|
+
</Content>
|
|
148
|
+
<Footer
|
|
149
|
+
flex
|
|
150
|
+
justify="end"
|
|
151
|
+
className="sticky bottom-0 border-t bg-base px-6 py-2"
|
|
152
|
+
>
|
|
153
|
+
<Button variant="link" onClick={props.onClose}>
|
|
154
|
+
Close
|
|
155
|
+
</Button>
|
|
156
|
+
</Footer>
|
|
157
|
+
</Modal>
|
|
158
|
+
),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
setTimeout(() => {
|
|
163
|
+
const model = getModel();
|
|
164
|
+
setType(model.datatype);
|
|
165
|
+
setMultiple(model.multiple);
|
|
166
|
+
setHasOptions(!!model.optionList);
|
|
167
|
+
setCustomOptions(model.optionList === SCHEMA_CUSTOM_LIST);
|
|
168
|
+
}, 10);
|
|
169
|
+
}, []);
|
|
170
|
+
|
|
171
|
+
const handleTypeChange = useCallback(
|
|
172
|
+
(type: DATA_TYPES) => {
|
|
173
|
+
if (type) {
|
|
174
|
+
const modelRef = getModel();
|
|
175
|
+
let reset = {
|
|
176
|
+
datatype: type,
|
|
177
|
+
id,
|
|
178
|
+
label: modelRef.label,
|
|
179
|
+
defaultValue: modelRef.defaultValue,
|
|
180
|
+
required: modelRef.required,
|
|
181
|
+
multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
|
|
182
|
+
minItems: modelRef.minItems,
|
|
183
|
+
maxItems: modelRef.maxItems,
|
|
184
|
+
} as DefaultSchema & ValueType;
|
|
185
|
+
if (type === DATA_TYPES.STRING) {
|
|
186
|
+
reset = Object.assign(reset, {
|
|
187
|
+
optionList: modelRef.optionList,
|
|
188
|
+
options: modelRef.options,
|
|
189
|
+
regex: modelRef.regex,
|
|
190
|
+
} as StringSchema);
|
|
191
|
+
} else if (type === DATA_TYPES.DATE) {
|
|
192
|
+
reset = Object.assign(reset, {
|
|
193
|
+
type: "date",
|
|
194
|
+
} as DateSchema);
|
|
195
|
+
} else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
|
|
196
|
+
reset = Object.assign(reset, {
|
|
197
|
+
min: modelRef.min,
|
|
198
|
+
max: modelRef.max,
|
|
199
|
+
step: modelRef.step,
|
|
200
|
+
} as NumberSchema);
|
|
201
|
+
}
|
|
202
|
+
if (type === DATA_TYPES.AVATAR) {
|
|
203
|
+
setMultiple(false);
|
|
204
|
+
}
|
|
205
|
+
onTypeChange(reset);
|
|
206
|
+
setType(type);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
[id],
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<SortableItem id={fieldId} className="flex flex-nowrap gap-1 group/field">
|
|
214
|
+
<Collapsable key={fieldId} open={defaultOpen} className="flex-1">
|
|
215
|
+
<div className="flex flex-nowrap items-center">
|
|
216
|
+
<span className="flex-1 truncate">{id}</span>
|
|
217
|
+
{dynamic && (
|
|
218
|
+
<Button
|
|
219
|
+
size="sm"
|
|
220
|
+
color="danger"
|
|
221
|
+
variant="link"
|
|
222
|
+
aria-label="Delete"
|
|
223
|
+
stopPropagation
|
|
224
|
+
icon={CoreIcons.trash}
|
|
225
|
+
onClick={onRemove}
|
|
226
|
+
className="invisible group-hover/field:visible"
|
|
227
|
+
/>
|
|
228
|
+
)}
|
|
229
|
+
</div>
|
|
230
|
+
<div className="flex flex-col gap-1 pb-4">
|
|
231
|
+
<Controller name={`${field}.id`}>
|
|
232
|
+
<HiddenInput hiddenValue={id} />
|
|
233
|
+
</Controller>
|
|
234
|
+
<Controller name={`${field}.datatype`}>
|
|
235
|
+
<Select
|
|
236
|
+
inline
|
|
237
|
+
label="Type"
|
|
238
|
+
placeholder="Data type"
|
|
239
|
+
options={acceptableTypes}
|
|
240
|
+
onChange={(val) => handleTypeChange(val as unknown as DATA_TYPES)}
|
|
241
|
+
/>
|
|
242
|
+
</Controller>
|
|
243
|
+
<Controller name={`${field}.label`}>
|
|
244
|
+
<Input
|
|
245
|
+
inline
|
|
246
|
+
label="Label"
|
|
247
|
+
allowClear
|
|
248
|
+
placeholder="Label..."
|
|
249
|
+
decorateEndShowWhenEmpty
|
|
250
|
+
decorateEnd={
|
|
251
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
252
|
+
}
|
|
253
|
+
/>
|
|
254
|
+
</Controller>
|
|
255
|
+
{type === DATA_TYPES.STRING && (
|
|
256
|
+
<Fragment>
|
|
257
|
+
<Controller name={`${field}.regex`}>
|
|
258
|
+
<Input
|
|
259
|
+
inline
|
|
260
|
+
label="Regex validation"
|
|
261
|
+
allowClear
|
|
262
|
+
placeholder="Regex value..."
|
|
263
|
+
decorateEndShowWhenEmpty
|
|
264
|
+
decorateEnd={
|
|
265
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
266
|
+
}
|
|
267
|
+
/>
|
|
268
|
+
</Controller>
|
|
269
|
+
<Field inline label="Option list">
|
|
270
|
+
<Controller name={`${field}.optionList`}>
|
|
271
|
+
<Select
|
|
272
|
+
allowClear
|
|
273
|
+
options={[SCHEMA_CUSTOM_LIST, ...optionLists]}
|
|
274
|
+
onChange={(v) => setCustomOptions(v === SCHEMA_CUSTOM_LIST)}
|
|
275
|
+
/>
|
|
276
|
+
</Controller>
|
|
277
|
+
{customOptions && (
|
|
278
|
+
<Button
|
|
279
|
+
icon={CoreIcons.list}
|
|
280
|
+
aria-label="Custom options"
|
|
281
|
+
onClick={() => {
|
|
282
|
+
void openOptionModal({ title: `${id} Options` });
|
|
283
|
+
}}
|
|
284
|
+
/>
|
|
285
|
+
)}
|
|
286
|
+
</Field>
|
|
287
|
+
{OptionModal}
|
|
288
|
+
</Fragment>
|
|
289
|
+
)}
|
|
290
|
+
{type === DATA_TYPES.FILE && (
|
|
291
|
+
<Fragment>
|
|
292
|
+
<Controller name={`${field}.accept`}>
|
|
293
|
+
<Input
|
|
294
|
+
inline
|
|
295
|
+
label="Accept types"
|
|
296
|
+
allowClear
|
|
297
|
+
placeholder="Accept value..."
|
|
298
|
+
decorateEndShowWhenEmpty
|
|
299
|
+
decorateEnd={
|
|
300
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
301
|
+
}
|
|
302
|
+
/>
|
|
303
|
+
</Controller>
|
|
304
|
+
</Fragment>
|
|
305
|
+
)}
|
|
306
|
+
{[DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(
|
|
307
|
+
type as unknown as DATA_TYPES,
|
|
308
|
+
) && (
|
|
309
|
+
<Fragment>
|
|
310
|
+
<Controller name={`${field}.min`}>
|
|
311
|
+
<Number
|
|
312
|
+
inline
|
|
313
|
+
label="Min value"
|
|
314
|
+
allowClear
|
|
315
|
+
placeholder="Minimum value..."
|
|
316
|
+
decorateEndShowWhenEmpty
|
|
317
|
+
decorateEnd={
|
|
318
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
319
|
+
}
|
|
320
|
+
/>
|
|
321
|
+
</Controller>
|
|
322
|
+
<Controller name={`${field}.max`}>
|
|
323
|
+
<Number
|
|
324
|
+
inline
|
|
325
|
+
label="Max value"
|
|
326
|
+
allowClear
|
|
327
|
+
placeholder="Maximum value..."
|
|
328
|
+
decorateEndShowWhenEmpty
|
|
329
|
+
decorateEnd={
|
|
330
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
331
|
+
}
|
|
332
|
+
/>
|
|
333
|
+
</Controller>
|
|
334
|
+
<Controller name={`${field}.step`}>
|
|
335
|
+
<Number
|
|
336
|
+
inline
|
|
337
|
+
label="Step value"
|
|
338
|
+
allowClear
|
|
339
|
+
placeholder="Step value..."
|
|
340
|
+
decorateEndShowWhenEmpty
|
|
341
|
+
decorateEnd={
|
|
342
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
343
|
+
}
|
|
344
|
+
/>
|
|
345
|
+
</Controller>
|
|
346
|
+
</Fragment>
|
|
347
|
+
)}
|
|
348
|
+
{type === DATA_TYPES.DATE && (
|
|
349
|
+
<Fragment>
|
|
350
|
+
<Field inline label="Type" plain>
|
|
351
|
+
<Controller name={`${field}.type`}>
|
|
352
|
+
<ToggleButtonGroup value="" fullWidth className="flex-1">
|
|
353
|
+
<Button className="flex-1" value="date">
|
|
354
|
+
Date
|
|
355
|
+
</Button>
|
|
356
|
+
<Button className="flex-1" value="datetime">
|
|
357
|
+
Date Time
|
|
358
|
+
</Button>
|
|
359
|
+
</ToggleButtonGroup>
|
|
360
|
+
</Controller>
|
|
361
|
+
</Field>
|
|
362
|
+
<Controller name={`${field}.min`}>
|
|
363
|
+
<DateInput
|
|
364
|
+
inline
|
|
365
|
+
label="Min value"
|
|
366
|
+
allowClear
|
|
367
|
+
placeholder="Minimum value..."
|
|
368
|
+
decorateEndShowWhenEmpty
|
|
369
|
+
decorateEnd={
|
|
370
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
371
|
+
}
|
|
372
|
+
/>
|
|
373
|
+
</Controller>
|
|
374
|
+
<Controller name={`${field}.max`}>
|
|
375
|
+
<DateInput
|
|
376
|
+
inline
|
|
377
|
+
label="Max value"
|
|
378
|
+
allowClear
|
|
379
|
+
placeholder="Maximum value..."
|
|
380
|
+
decorateEndShowWhenEmpty
|
|
381
|
+
decorateEnd={
|
|
382
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
383
|
+
}
|
|
384
|
+
/>
|
|
385
|
+
</Controller>
|
|
386
|
+
</Fragment>
|
|
387
|
+
)}
|
|
388
|
+
<Field label=" " inline plain>
|
|
389
|
+
<Controller name={`${field}.required`}>
|
|
390
|
+
<Checkbox label="Required" />
|
|
391
|
+
</Controller>
|
|
392
|
+
{type !== DATA_TYPES.AVATAR && (
|
|
393
|
+
<Controller name={`${field}.multiple`}>
|
|
394
|
+
<Checkbox label="Multiple" onChange={setMultiple} />
|
|
395
|
+
</Controller>
|
|
396
|
+
)}
|
|
397
|
+
{hasOptions && (
|
|
398
|
+
<Controller name={`${field}.allowCustom`}>
|
|
399
|
+
<Checkbox label="Allow custom value" />
|
|
400
|
+
</Controller>
|
|
401
|
+
)}
|
|
402
|
+
</Field>
|
|
403
|
+
{multiple && (
|
|
404
|
+
<Fragment>
|
|
405
|
+
<Controller name={`${field}.minItems`}>
|
|
406
|
+
<Number
|
|
407
|
+
inline
|
|
408
|
+
label="Min items"
|
|
409
|
+
allowClear
|
|
410
|
+
placeholder="Minimum items..."
|
|
411
|
+
decorateEndShowWhenEmpty
|
|
412
|
+
decorateEnd={
|
|
413
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
414
|
+
}
|
|
415
|
+
/>
|
|
416
|
+
</Controller>
|
|
417
|
+
<Controller name={`${field}.maxItems`}>
|
|
418
|
+
<Number
|
|
419
|
+
inline
|
|
420
|
+
label="Max items"
|
|
421
|
+
allowClear
|
|
422
|
+
placeholder="Maximum items..."
|
|
423
|
+
decorateEndShowWhenEmpty
|
|
424
|
+
decorateEnd={
|
|
425
|
+
<span className="text-sm text-tint-300 px-1">OPTIONAL</span>
|
|
426
|
+
}
|
|
427
|
+
/>
|
|
428
|
+
</Controller>
|
|
429
|
+
</Fragment>
|
|
430
|
+
)}
|
|
431
|
+
</div>
|
|
432
|
+
</Collapsable>
|
|
433
|
+
</SortableItem>
|
|
434
|
+
);
|
|
435
|
+
};
|
package/src/form/Controller.tsx
CHANGED
|
@@ -46,7 +46,7 @@ export interface ControllerProps {
|
|
|
46
46
|
disabled?: boolean;
|
|
47
47
|
onBlur: () => void;
|
|
48
48
|
onChange: (value: AnyObject) => void;
|
|
49
|
-
|
|
49
|
+
ref?: React.Ref<HTMLInputElement>;
|
|
50
50
|
}) => ReactElement);
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -86,7 +86,7 @@ export const Controller = ({ name, children, ...rest }: ControllerProps) => {
|
|
|
86
86
|
: children({
|
|
87
87
|
...field,
|
|
88
88
|
...rest,
|
|
89
|
-
|
|
89
|
+
ref: (el: AnyObject) => {
|
|
90
90
|
el && ref(el);
|
|
91
91
|
},
|
|
92
92
|
readOnly: isSubmitting,
|
package/src/form/Form.tsx
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
import { yupResolver } from "@hookform/resolvers/yup";
|
|
25
25
|
import { useDebounce } from "@react-fabric/core";
|
|
26
|
-
import { type yup } from "@react-fabric/utilities";
|
|
26
|
+
import { EMPTY_OBJECT, type yup } from "@react-fabric/utilities";
|
|
27
27
|
import {
|
|
28
28
|
useCallback,
|
|
29
29
|
useEffect,
|
|
@@ -77,7 +77,7 @@ export const Form = <K extends KeyValue>({
|
|
|
77
77
|
formRef,
|
|
78
78
|
schema,
|
|
79
79
|
children,
|
|
80
|
-
defaultValues,
|
|
80
|
+
defaultValues = EMPTY_OBJECT as K,
|
|
81
81
|
onSubmit = DEFAULT_SUBMIT,
|
|
82
82
|
onChange,
|
|
83
83
|
...rest
|
|
@@ -118,12 +118,16 @@ export const Form = <K extends KeyValue>({
|
|
|
118
118
|
submit: () => form.handleSubmit(onSubmit),
|
|
119
119
|
validate: async () => await form.trigger(),
|
|
120
120
|
getValues: () => form.getValues(),
|
|
121
|
-
setValues: (v) => form.reset(v),
|
|
121
|
+
setValues: (v) => setTimeout(() => form.reset(v), 50),
|
|
122
122
|
setValue: (k, v) =>
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
setTimeout(
|
|
124
|
+
() =>
|
|
125
|
+
form.setValue(k as AnyObject, v, {
|
|
126
|
+
shouldDirty: false,
|
|
127
|
+
shouldTouch: false,
|
|
128
|
+
}),
|
|
129
|
+
50,
|
|
130
|
+
),
|
|
127
131
|
}),
|
|
128
132
|
[onSubmit, form, form.formState, defaultValues],
|
|
129
133
|
);
|
|
@@ -146,7 +150,10 @@ export const Form = <K extends KeyValue>({
|
|
|
146
150
|
<form
|
|
147
151
|
ref={ref}
|
|
148
152
|
onReset={() => handleReset(defaultValues)}
|
|
149
|
-
onSubmit={
|
|
153
|
+
onSubmit={(e) => {
|
|
154
|
+
e.stopPropagation();
|
|
155
|
+
void form.handleSubmit(onSubmit, onInvalid)(e);
|
|
156
|
+
}}
|
|
150
157
|
data-loading={form.formState.isSubmitting}
|
|
151
158
|
className="contents"
|
|
152
159
|
autoComplete="off"
|