@react-fabric/form 1.0.0-beta.34 → 1.0.0-beta.36
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/dist/cjs/builder/SchemaEditor.js +46 -413
- 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/Form.js +7 -11
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/types/schema.js +2 -0
- package/dist/esm/builder/SchemaEditor.js +48 -415
- 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/Form.js +7 -11
- package/dist/esm/index.js +1 -1
- package/dist/esm/types/schema.js +2 -1
- package/dist/types/builder/SchemaEditor.d.ts +2 -16
- 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/Form.d.ts +0 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types/schema.d.ts +27 -0
- package/package.json +5 -5
- package/src/builder/SchemaEditor.tsx +21 -406
- 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/Form.tsx +4 -5
- package/src/index.ts +6 -1
- package/src/input/ArrayInput.tsx +1 -0
- package/src/types/schema.ts +31 -0
|
@@ -1,433 +1,66 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
5
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
6
|
-
import { Content, Footer, useOverlayService, Modal, Button, Collapsable, CoreIcons, ToggleButtonGroup } from '@react-fabric/core';
|
|
3
|
+
import { Footer } from '@react-fabric/core';
|
|
7
4
|
import { EMPTY_ARRAY } from '@react-fabric/utilities';
|
|
8
|
-
import { useRef,
|
|
9
|
-
import { Controller } from '../form/Controller.js';
|
|
5
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
10
6
|
import { Form } from '../form/Form.js';
|
|
11
|
-
import { ArrayInput } from '../input/ArrayInput.js';
|
|
12
|
-
import { Checkbox } from '../input/Checkbox.js';
|
|
13
|
-
import { DateInput } from '../input/DateInput.js';
|
|
14
|
-
import { Field } from '../input/Field.js';
|
|
15
|
-
import { HiddenInput } from '../input/Hidden.js';
|
|
16
|
-
import { Input } from '../input/Input.js';
|
|
17
|
-
import { Number } from '../input/Number.js';
|
|
18
|
-
import { Select } from '../select/Select.js';
|
|
19
7
|
import { DATA_TYPES } from '../types/schema.js';
|
|
8
|
+
import { SchemaArrayForm } from './components/SchemaArrayForm.js';
|
|
20
9
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
21
10
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
/*
|
|
12
|
+
* React Fabric
|
|
13
|
+
* @version: 1.0.0
|
|
14
|
+
*
|
|
15
|
+
*
|
|
16
|
+
* The MIT License (MIT)
|
|
17
|
+
* Copyright (c) 2025 Adarsh Pastakia
|
|
18
|
+
*
|
|
19
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
20
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
21
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
22
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
23
|
+
* subject to the following conditions:
|
|
24
|
+
*
|
|
25
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
26
|
+
* portions of the Software.
|
|
27
|
+
*
|
|
28
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
29
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
30
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
31
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
var SchemaEditor = function SchemaEditor(_ref) {
|
|
35
|
+
var schemaDef = _ref.schemaDef,
|
|
36
|
+
dynamic = _ref.dynamic,
|
|
37
|
+
acceptableTypes = _ref.acceptableTypes,
|
|
29
38
|
_ref$optionLists = _ref.optionLists,
|
|
30
39
|
optionLists = _ref$optionLists === void 0 ? EMPTY_ARRAY : _ref$optionLists,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
var _useState = useState(),
|
|
35
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
36
|
-
type = _useState2[0],
|
|
37
|
-
setType = _useState2[1];
|
|
38
|
-
var _useState3 = useState(false),
|
|
39
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
40
|
-
multiple = _useState4[0],
|
|
41
|
-
setMultiple = _useState4[1];
|
|
42
|
-
var _useState5 = useState(false),
|
|
43
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
44
|
-
hasOptions = _useState6[0],
|
|
45
|
-
setHasOptions = _useState6[1];
|
|
46
|
-
var _useState7 = useState(false),
|
|
47
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
48
|
-
customOptions = _useState8[0],
|
|
49
|
-
setCustomOptions = _useState8[1];
|
|
50
|
-
var _useOverlayService = useOverlayService(function (props) {
|
|
51
|
-
return /*#__PURE__*/jsxs(Modal, _objectSpread(_objectSpread({}, props), {}, {
|
|
52
|
-
children: [/*#__PURE__*/jsx(Content, {
|
|
53
|
-
children: /*#__PURE__*/jsx(ArrayInput, {
|
|
54
|
-
name: "".concat(field, ".options"),
|
|
55
|
-
onAdd: function onAdd() {
|
|
56
|
-
return String("");
|
|
57
|
-
},
|
|
58
|
-
children: /*#__PURE__*/jsx(Controller, {
|
|
59
|
-
children: /*#__PURE__*/jsx(Input, {})
|
|
60
|
-
})
|
|
61
|
-
})
|
|
62
|
-
}), /*#__PURE__*/jsx(Footer, {
|
|
63
|
-
flex: true,
|
|
64
|
-
justify: "end",
|
|
65
|
-
className: "sticky bottom-0 border-t bg-base px-6 py-2",
|
|
66
|
-
children: /*#__PURE__*/jsx(Button, {
|
|
67
|
-
variant: "link",
|
|
68
|
-
onClick: props.onClose,
|
|
69
|
-
children: "Close"
|
|
70
|
-
})
|
|
71
|
-
})]
|
|
72
|
-
}));
|
|
73
|
-
}),
|
|
74
|
-
_useOverlayService2 = _slicedToArray(_useOverlayService, 2),
|
|
75
|
-
OptionModal = _useOverlayService2[0],
|
|
76
|
-
openOptionModal = _useOverlayService2[1];
|
|
77
|
-
useEffect(function () {
|
|
78
|
-
setTimeout(function () {
|
|
79
|
-
var model = getModel();
|
|
80
|
-
setType(model.datatype);
|
|
81
|
-
setMultiple(model.multiple);
|
|
82
|
-
setHasOptions(!!model.optionList);
|
|
83
|
-
setCustomOptions(model.optionList === CUSTOM_LIST);
|
|
84
|
-
}, 10);
|
|
85
|
-
}, []);
|
|
86
|
-
var handleTypeChange = useCallback(function (type) {
|
|
87
|
-
if (type) {
|
|
88
|
-
var modelRef = getModel();
|
|
89
|
-
var reset = {
|
|
90
|
-
datatype: type,
|
|
91
|
-
id: field,
|
|
92
|
-
label: modelRef.label,
|
|
93
|
-
defaultValue: modelRef.defaultValue,
|
|
94
|
-
required: modelRef.required,
|
|
95
|
-
multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
|
|
96
|
-
minItems: modelRef.minItems,
|
|
97
|
-
maxItems: modelRef.maxItems
|
|
98
|
-
};
|
|
99
|
-
if (type === DATA_TYPES.STRING) {
|
|
100
|
-
reset = Object.assign(reset, {
|
|
101
|
-
optionList: modelRef.optionList,
|
|
102
|
-
options: modelRef.options,
|
|
103
|
-
regex: modelRef.regex
|
|
104
|
-
});
|
|
105
|
-
} else if (type === DATA_TYPES.DATE) {
|
|
106
|
-
reset = Object.assign(reset, {
|
|
107
|
-
type: "date"
|
|
108
|
-
});
|
|
109
|
-
} else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
|
|
110
|
-
reset = Object.assign(reset, {
|
|
111
|
-
min: modelRef.min,
|
|
112
|
-
max: modelRef.max,
|
|
113
|
-
step: modelRef.step
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
if (type === DATA_TYPES.AVATAR) {
|
|
117
|
-
setMultiple(false);
|
|
118
|
-
}
|
|
119
|
-
onTypeChange(reset);
|
|
120
|
-
setType(type);
|
|
121
|
-
}
|
|
122
|
-
}, []);
|
|
123
|
-
return /*#__PURE__*/jsxs(Collapsable, {
|
|
124
|
-
open: true,
|
|
125
|
-
children: [/*#__PURE__*/jsxs("div", {
|
|
126
|
-
className: "flex flex-nowrap group/field",
|
|
127
|
-
children: [/*#__PURE__*/jsx("span", {
|
|
128
|
-
className: "flex-1 truncate",
|
|
129
|
-
children: field
|
|
130
|
-
}), allowRemove && /*#__PURE__*/jsx(Button, {
|
|
131
|
-
size: "sm",
|
|
132
|
-
color: "danger",
|
|
133
|
-
variant: "link",
|
|
134
|
-
"aria-label": "Delete",
|
|
135
|
-
stopPropagation: true,
|
|
136
|
-
icon: CoreIcons.trash,
|
|
137
|
-
onClick: onRemove,
|
|
138
|
-
className: "invisible group-hover/field:visible"
|
|
139
|
-
})]
|
|
140
|
-
}), /*#__PURE__*/jsxs("div", {
|
|
141
|
-
className: "flex flex-col gap-1 pb-4",
|
|
142
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
143
|
-
name: "".concat(field, ".id"),
|
|
144
|
-
children: /*#__PURE__*/jsx(HiddenInput, {
|
|
145
|
-
hiddenValue: field
|
|
146
|
-
})
|
|
147
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
148
|
-
name: "".concat(field, ".datatype"),
|
|
149
|
-
children: /*#__PURE__*/jsx(Select, {
|
|
150
|
-
inline: true,
|
|
151
|
-
label: "Type",
|
|
152
|
-
placeholder: "Data type",
|
|
153
|
-
options: acceptableTypes,
|
|
154
|
-
onChange: function onChange(val) {
|
|
155
|
-
return handleTypeChange(val);
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
159
|
-
name: "".concat(field, ".label"),
|
|
160
|
-
children: /*#__PURE__*/jsx(Input, {
|
|
161
|
-
inline: true,
|
|
162
|
-
label: "Label",
|
|
163
|
-
allowClear: true,
|
|
164
|
-
placeholder: "Label...",
|
|
165
|
-
decorateEndShowWhenEmpty: true,
|
|
166
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
167
|
-
className: "text-sm text-muted/50 px-1",
|
|
168
|
-
children: "OPTIONAL"
|
|
169
|
-
})
|
|
170
|
-
})
|
|
171
|
-
}), type === DATA_TYPES.STRING && /*#__PURE__*/jsxs(Fragment, {
|
|
172
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
173
|
-
name: "".concat(field, ".regex"),
|
|
174
|
-
children: /*#__PURE__*/jsx(Input, {
|
|
175
|
-
inline: true,
|
|
176
|
-
label: "Regex validation",
|
|
177
|
-
allowClear: true,
|
|
178
|
-
placeholder: "Regex value...",
|
|
179
|
-
decorateEndShowWhenEmpty: true,
|
|
180
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
181
|
-
className: "text-sm text-muted/50 px-1",
|
|
182
|
-
children: "OPTIONAL"
|
|
183
|
-
})
|
|
184
|
-
})
|
|
185
|
-
}), /*#__PURE__*/jsxs(Field, {
|
|
186
|
-
inline: true,
|
|
187
|
-
label: "Option list",
|
|
188
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
189
|
-
name: "".concat(field, ".optionList"),
|
|
190
|
-
children: /*#__PURE__*/jsx(Select, {
|
|
191
|
-
allowClear: true,
|
|
192
|
-
options: [CUSTOM_LIST].concat(_toConsumableArray(optionLists)),
|
|
193
|
-
onChange: function onChange(v) {
|
|
194
|
-
return setCustomOptions(v === CUSTOM_LIST);
|
|
195
|
-
}
|
|
196
|
-
})
|
|
197
|
-
}), customOptions && /*#__PURE__*/jsx(Button, {
|
|
198
|
-
icon: CoreIcons.list,
|
|
199
|
-
"aria-label": "Custom options",
|
|
200
|
-
onClick: function onClick() {
|
|
201
|
-
void openOptionModal();
|
|
202
|
-
}
|
|
203
|
-
})]
|
|
204
|
-
}), OptionModal]
|
|
205
|
-
}), type === DATA_TYPES.FILE && /*#__PURE__*/jsx(Fragment, {
|
|
206
|
-
children: /*#__PURE__*/jsx(Controller, {
|
|
207
|
-
name: "".concat(field, ".accept"),
|
|
208
|
-
children: /*#__PURE__*/jsx(Input, {
|
|
209
|
-
inline: true,
|
|
210
|
-
label: "Accept types",
|
|
211
|
-
allowClear: true,
|
|
212
|
-
placeholder: "Accept value...",
|
|
213
|
-
decorateEndShowWhenEmpty: true,
|
|
214
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
215
|
-
className: "text-sm text-muted/50 px-1",
|
|
216
|
-
children: "OPTIONAL"
|
|
217
|
-
})
|
|
218
|
-
})
|
|
219
|
-
})
|
|
220
|
-
}), [DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(type) && /*#__PURE__*/jsxs(Fragment, {
|
|
221
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
222
|
-
name: "".concat(field, ".min"),
|
|
223
|
-
children: /*#__PURE__*/jsx(Number, {
|
|
224
|
-
inline: true,
|
|
225
|
-
label: "Min value",
|
|
226
|
-
allowClear: true,
|
|
227
|
-
placeholder: "Minimum value...",
|
|
228
|
-
decorateEndShowWhenEmpty: true,
|
|
229
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
230
|
-
className: "text-sm text-muted/50 px-1",
|
|
231
|
-
children: "OPTIONAL"
|
|
232
|
-
})
|
|
233
|
-
})
|
|
234
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
235
|
-
name: "".concat(field, ".max"),
|
|
236
|
-
children: /*#__PURE__*/jsx(Number, {
|
|
237
|
-
inline: true,
|
|
238
|
-
label: "Max value",
|
|
239
|
-
allowClear: true,
|
|
240
|
-
placeholder: "Maximum value...",
|
|
241
|
-
decorateEndShowWhenEmpty: true,
|
|
242
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
243
|
-
className: "text-sm text-muted/50 px-1",
|
|
244
|
-
children: "OPTIONAL"
|
|
245
|
-
})
|
|
246
|
-
})
|
|
247
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
248
|
-
name: "".concat(field, ".step"),
|
|
249
|
-
children: /*#__PURE__*/jsx(Number, {
|
|
250
|
-
inline: true,
|
|
251
|
-
label: "Step value",
|
|
252
|
-
allowClear: true,
|
|
253
|
-
placeholder: "Step value...",
|
|
254
|
-
decorateEndShowWhenEmpty: true,
|
|
255
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
256
|
-
className: "text-sm text-muted/50 px-1",
|
|
257
|
-
children: "OPTIONAL"
|
|
258
|
-
})
|
|
259
|
-
})
|
|
260
|
-
})]
|
|
261
|
-
}), type === DATA_TYPES.DATE && /*#__PURE__*/jsxs(Fragment, {
|
|
262
|
-
children: [/*#__PURE__*/jsx(Field, {
|
|
263
|
-
inline: true,
|
|
264
|
-
label: "Type",
|
|
265
|
-
plain: true,
|
|
266
|
-
children: /*#__PURE__*/jsx(Controller, {
|
|
267
|
-
name: "".concat(field, ".type"),
|
|
268
|
-
children: /*#__PURE__*/jsxs(ToggleButtonGroup, {
|
|
269
|
-
value: "",
|
|
270
|
-
fullWidth: true,
|
|
271
|
-
className: "flex-1",
|
|
272
|
-
children: [/*#__PURE__*/jsx(Button, {
|
|
273
|
-
className: "flex-1",
|
|
274
|
-
value: "date",
|
|
275
|
-
children: "Date"
|
|
276
|
-
}), /*#__PURE__*/jsx(Button, {
|
|
277
|
-
className: "flex-1",
|
|
278
|
-
value: "datetime",
|
|
279
|
-
children: "Date Time"
|
|
280
|
-
})]
|
|
281
|
-
})
|
|
282
|
-
})
|
|
283
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
284
|
-
name: "".concat(field, ".min"),
|
|
285
|
-
children: /*#__PURE__*/jsx(DateInput, {
|
|
286
|
-
inline: true,
|
|
287
|
-
label: "Min value",
|
|
288
|
-
allowClear: true,
|
|
289
|
-
placeholder: "Minimum value...",
|
|
290
|
-
decorateEndShowWhenEmpty: true,
|
|
291
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
292
|
-
className: "text-sm text-muted/50 px-1",
|
|
293
|
-
children: "OPTIONAL"
|
|
294
|
-
})
|
|
295
|
-
})
|
|
296
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
297
|
-
name: "".concat(field, ".max"),
|
|
298
|
-
children: /*#__PURE__*/jsx(DateInput, {
|
|
299
|
-
inline: true,
|
|
300
|
-
label: "Max value",
|
|
301
|
-
allowClear: true,
|
|
302
|
-
placeholder: "Maximum value...",
|
|
303
|
-
decorateEndShowWhenEmpty: true,
|
|
304
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
305
|
-
className: "text-sm text-muted/50 px-1",
|
|
306
|
-
children: "OPTIONAL"
|
|
307
|
-
})
|
|
308
|
-
})
|
|
309
|
-
})]
|
|
310
|
-
}), /*#__PURE__*/jsxs(Field, {
|
|
311
|
-
label: " ",
|
|
312
|
-
inline: true,
|
|
313
|
-
plain: true,
|
|
314
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
315
|
-
name: "".concat(field, ".required"),
|
|
316
|
-
children: /*#__PURE__*/jsx(Checkbox, {
|
|
317
|
-
label: "Required"
|
|
318
|
-
})
|
|
319
|
-
}), type !== DATA_TYPES.AVATAR && /*#__PURE__*/jsx(Controller, {
|
|
320
|
-
name: "".concat(field, ".multiple"),
|
|
321
|
-
children: /*#__PURE__*/jsx(Checkbox, {
|
|
322
|
-
label: "Multiple",
|
|
323
|
-
onChange: setMultiple
|
|
324
|
-
})
|
|
325
|
-
}), hasOptions && /*#__PURE__*/jsx(Controller, {
|
|
326
|
-
name: "".concat(field, ".allowCustom"),
|
|
327
|
-
children: /*#__PURE__*/jsx(Checkbox, {
|
|
328
|
-
label: "Allow custom value"
|
|
329
|
-
})
|
|
330
|
-
})]
|
|
331
|
-
}), multiple && /*#__PURE__*/jsxs(Fragment, {
|
|
332
|
-
children: [/*#__PURE__*/jsx(Controller, {
|
|
333
|
-
name: "".concat(field, ".minItems"),
|
|
334
|
-
children: /*#__PURE__*/jsx(Number, {
|
|
335
|
-
inline: true,
|
|
336
|
-
label: "Min items",
|
|
337
|
-
allowClear: true,
|
|
338
|
-
placeholder: "Minimum items...",
|
|
339
|
-
decorateEndShowWhenEmpty: true,
|
|
340
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
341
|
-
className: "text-sm text-muted/50 px-1",
|
|
342
|
-
children: "OPTIONAL"
|
|
343
|
-
})
|
|
344
|
-
})
|
|
345
|
-
}), /*#__PURE__*/jsx(Controller, {
|
|
346
|
-
name: "".concat(field, ".maxItems"),
|
|
347
|
-
children: /*#__PURE__*/jsx(Number, {
|
|
348
|
-
inline: true,
|
|
349
|
-
label: "Max items",
|
|
350
|
-
allowClear: true,
|
|
351
|
-
placeholder: "Maximum items...",
|
|
352
|
-
decorateEndShowWhenEmpty: true,
|
|
353
|
-
decorateEnd: /*#__PURE__*/jsx("span", {
|
|
354
|
-
className: "text-sm text-muted/50 px-1",
|
|
355
|
-
children: "OPTIONAL"
|
|
356
|
-
})
|
|
357
|
-
})
|
|
358
|
-
})]
|
|
359
|
-
})]
|
|
360
|
-
})]
|
|
361
|
-
}, field);
|
|
362
|
-
};
|
|
363
|
-
var SchemaEditor = function SchemaEditor(_ref2) {
|
|
364
|
-
var schemaDef = _ref2.schemaDef,
|
|
365
|
-
allowRemove = _ref2.allowRemove,
|
|
366
|
-
acceptableTypes = _ref2.acceptableTypes,
|
|
367
|
-
_ref2$optionLists = _ref2.optionLists,
|
|
368
|
-
optionLists = _ref2$optionLists === void 0 ? EMPTY_ARRAY : _ref2$optionLists,
|
|
369
|
-
children = _ref2.children,
|
|
370
|
-
onChange = _ref2.onChange;
|
|
40
|
+
children = _ref.children,
|
|
41
|
+
onSubmit = _ref.onSubmit,
|
|
42
|
+
onChange = _ref.onChange;
|
|
371
43
|
var formRef = useRef(null);
|
|
372
|
-
var _useState9 = useState([]),
|
|
373
|
-
_useState10 = _slicedToArray(_useState9, 2),
|
|
374
|
-
schemaFields = _useState10[0],
|
|
375
|
-
setSchemaFields = _useState10[1];
|
|
376
|
-
useEffect(function () {
|
|
377
|
-
var _formRef$current$getV, _formRef$current, _formRef$current2;
|
|
378
|
-
var schemaMap = Object.fromEntries(schemaDef.map(function (schema) {
|
|
379
|
-
return [schema.id, schema];
|
|
380
|
-
}));
|
|
381
|
-
var currentModel = (_formRef$current$getV = (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.getValues()) !== null && _formRef$current$getV !== void 0 ? _formRef$current$getV : {};
|
|
382
|
-
var schema = Object.fromEntries(Object.keys(schemaMap).map(function (field) {
|
|
383
|
-
var _currentModel$field;
|
|
384
|
-
return [field, (_currentModel$field = currentModel[field]) !== null && _currentModel$field !== void 0 ? _currentModel$field : schemaMap[field]];
|
|
385
|
-
}));
|
|
386
|
-
(_formRef$current2 = formRef.current) === null || _formRef$current2 === void 0 || _formRef$current2.setValues(schema);
|
|
387
|
-
setTimeout(function () {
|
|
388
|
-
setSchemaFields(Object.keys(schema));
|
|
389
|
-
}, 100);
|
|
390
|
-
}, [schemaDef]);
|
|
391
44
|
var handleChange = useCallback(function (model) {
|
|
392
|
-
onChange === null || onChange === void 0 || onChange(
|
|
393
|
-
}, []);
|
|
394
|
-
var handleTypeChange = useCallback(function (field, val) {
|
|
395
|
-
var _formRef$current3;
|
|
396
|
-
(_formRef$current3 = formRef.current) === null || _formRef$current3 === void 0 || _formRef$current3.setValue(field, val);
|
|
45
|
+
onChange === null || onChange === void 0 || onChange(model.schema);
|
|
397
46
|
}, []);
|
|
398
|
-
var
|
|
399
|
-
|
|
400
|
-
var newSchema = (_formRef$current$getV2 = (_formRef$current4 = formRef.current) === null || _formRef$current4 === void 0 ? void 0 : _formRef$current4.getValues()) !== null && _formRef$current$getV2 !== void 0 ? _formRef$current$getV2 : {};
|
|
401
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
402
|
-
delete newSchema[field];
|
|
403
|
-
(_formRef$current5 = formRef.current) === null || _formRef$current5 === void 0 || _formRef$current5.setValues(newSchema);
|
|
404
|
-
setTimeout(function () {
|
|
405
|
-
setSchemaFields(Object.keys(newSchema));
|
|
406
|
-
}, 100);
|
|
47
|
+
var handleSubmit = useCallback(function (model) {
|
|
48
|
+
onSubmit === null || onSubmit === void 0 || onSubmit(model.schema);
|
|
407
49
|
}, []);
|
|
50
|
+
useEffect(function () {
|
|
51
|
+
var _formRef$current;
|
|
52
|
+
(_formRef$current = formRef.current) === null || _formRef$current === void 0 || _formRef$current.setValues({
|
|
53
|
+
schema: schemaDef
|
|
54
|
+
});
|
|
55
|
+
}, [schemaDef]);
|
|
408
56
|
return /*#__PURE__*/jsxs(Form, {
|
|
409
57
|
formRef: formRef,
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
allowRemove: allowRemove,
|
|
417
|
-
optionLists: optionLists,
|
|
418
|
-
onRemove: function onRemove() {
|
|
419
|
-
return handleRemove(field);
|
|
420
|
-
},
|
|
421
|
-
getModel: function getModel() {
|
|
422
|
-
var _formRef$current6;
|
|
423
|
-
return (_formRef$current6 = formRef.current) === null || _formRef$current6 === void 0 ? void 0 : _formRef$current6.getValues()[field];
|
|
424
|
-
},
|
|
425
|
-
onTypeChange: function onTypeChange(val) {
|
|
426
|
-
return handleTypeChange(field, val);
|
|
427
|
-
},
|
|
428
|
-
acceptableTypes: acceptableTypes !== null && acceptableTypes !== void 0 ? acceptableTypes : Object.values(DATA_TYPES)
|
|
429
|
-
}, field);
|
|
430
|
-
})
|
|
58
|
+
onChange: handleChange,
|
|
59
|
+
onSubmit: handleSubmit,
|
|
60
|
+
children: [/*#__PURE__*/jsx(SchemaArrayForm, {
|
|
61
|
+
dynamic: dynamic,
|
|
62
|
+
optionLists: optionLists,
|
|
63
|
+
acceptableTypes: acceptableTypes !== null && acceptableTypes !== void 0 ? acceptableTypes : Object.values(DATA_TYPES)
|
|
431
64
|
}), children && /*#__PURE__*/jsx(Footer, {
|
|
432
65
|
flex: true,
|
|
433
66
|
className: "border-t bg-base px-6 py-2",
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
4
|
+
import { Modal, Content, Footer, Button } from '@react-fabric/core';
|
|
5
|
+
import { yup } from '@react-fabric/utilities';
|
|
6
|
+
import { useCallback } from 'react';
|
|
7
|
+
import { Controller } from '../../form/Controller.js';
|
|
8
|
+
import { Form } from '../../form/Form.js';
|
|
9
|
+
import { ArrayInput } from '../../input/ArrayInput.js';
|
|
10
|
+
import { Input } from '../../input/Input.js';
|
|
11
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
12
|
+
|
|
13
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
14
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
15
|
+
var fieldSchema = yup.object({
|
|
16
|
+
fields: yup.array(yup.string().trim().required()).required().min(1)
|
|
17
|
+
});
|
|
18
|
+
var AddSchemaFields = function AddSchemaFields(props) {
|
|
19
|
+
var handleSubmit = useCallback(function (model) {
|
|
20
|
+
props.onClose(model.fields.map(function (f) {
|
|
21
|
+
return f.split(",").map(function (ff) {
|
|
22
|
+
return ff.trim();
|
|
23
|
+
});
|
|
24
|
+
}).flat(2));
|
|
25
|
+
}, []);
|
|
26
|
+
return /*#__PURE__*/jsx(Modal, _objectSpread(_objectSpread({}, props), {}, {
|
|
27
|
+
children: /*#__PURE__*/jsxs(Form, {
|
|
28
|
+
schema: fieldSchema,
|
|
29
|
+
defaultValues: {
|
|
30
|
+
fields: [" "]
|
|
31
|
+
},
|
|
32
|
+
onSubmit: handleSubmit,
|
|
33
|
+
children: [/*#__PURE__*/jsxs(Content, {
|
|
34
|
+
children: [/*#__PURE__*/jsx(ArrayInput, {
|
|
35
|
+
minItems: 1,
|
|
36
|
+
name: "fields",
|
|
37
|
+
onAdd: function onAdd() {
|
|
38
|
+
return String("");
|
|
39
|
+
},
|
|
40
|
+
children: /*#__PURE__*/jsx(Controller, {
|
|
41
|
+
children: /*#__PURE__*/jsx(Input, {
|
|
42
|
+
autoFocus: true
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
}), /*#__PURE__*/jsx("p", {
|
|
46
|
+
className: "text-sm text-muted",
|
|
47
|
+
children: "Provide list of fields or comma-separated value"
|
|
48
|
+
})]
|
|
49
|
+
}), /*#__PURE__*/jsx(Footer, {
|
|
50
|
+
flex: true,
|
|
51
|
+
justify: "end",
|
|
52
|
+
className: "px-2 py-1",
|
|
53
|
+
children: /*#__PURE__*/jsx(Button, {
|
|
54
|
+
type: "submit",
|
|
55
|
+
children: "Add"
|
|
56
|
+
})
|
|
57
|
+
})]
|
|
58
|
+
})
|
|
59
|
+
}));
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { AddSchemaFields };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
4
|
+
import { DndContext } from '@dnd-kit/core';
|
|
5
|
+
import { restrictToVerticalAxis } from '@dnd-kit/modifiers';
|
|
6
|
+
import { SortableContext } from '@dnd-kit/sortable';
|
|
7
|
+
import { useOverlayService, Header, Button, CoreIcons, Content } from '@react-fabric/core';
|
|
8
|
+
import { isArray } from '@react-fabric/utilities';
|
|
9
|
+
import { useState, useCallback } from 'react';
|
|
10
|
+
import { useFormContext, useFieldArray } from 'react-hook-form';
|
|
11
|
+
import { DATA_TYPES } from '../../types/schema.js';
|
|
12
|
+
import { AddSchemaFields } from './AddSchemaFields.js';
|
|
13
|
+
import { SchemaField } from './SchemaField.js';
|
|
14
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
15
|
+
|
|
16
|
+
var SchemaArrayForm = function SchemaArrayForm(_ref) {
|
|
17
|
+
var dynamic = _ref.dynamic,
|
|
18
|
+
acceptableTypes = _ref.acceptableTypes,
|
|
19
|
+
optionLists = _ref.optionLists;
|
|
20
|
+
var form = useFormContext();
|
|
21
|
+
var _useState = useState(true),
|
|
22
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
23
|
+
defaultOpen = _useState2[0],
|
|
24
|
+
setDefaultOpen = _useState2[1];
|
|
25
|
+
var _useFieldArray = useFieldArray({
|
|
26
|
+
name: "schema",
|
|
27
|
+
control: form.control
|
|
28
|
+
}),
|
|
29
|
+
fields = _useFieldArray.fields,
|
|
30
|
+
append = _useFieldArray.append,
|
|
31
|
+
remove = _useFieldArray.remove,
|
|
32
|
+
move = _useFieldArray.move;
|
|
33
|
+
var handleTypeChange = useCallback(function (index, val) {
|
|
34
|
+
form.setValue("schema.".concat(index), val);
|
|
35
|
+
}, []);
|
|
36
|
+
var handleRemove = useCallback(function (index) {
|
|
37
|
+
remove(index);
|
|
38
|
+
}, []);
|
|
39
|
+
var _useOverlayService = useOverlayService(AddSchemaFields),
|
|
40
|
+
_useOverlayService2 = _slicedToArray(_useOverlayService, 2),
|
|
41
|
+
AddModal = _useOverlayService2[0],
|
|
42
|
+
openAddfields = _useOverlayService2[1];
|
|
43
|
+
var handleAdd = useCallback(function () {
|
|
44
|
+
void openAddfields().then(function (fields) {
|
|
45
|
+
if (isArray(fields)) {
|
|
46
|
+
var existing = form.getValues().schema.map(function (field) {
|
|
47
|
+
return field.id;
|
|
48
|
+
});
|
|
49
|
+
fields.forEach(function (field) {
|
|
50
|
+
if (!existing.includes(field)) {
|
|
51
|
+
append({
|
|
52
|
+
id: field,
|
|
53
|
+
datatype: DATA_TYPES.STRING,
|
|
54
|
+
label: ""
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}, []);
|
|
61
|
+
var Wrapper = useCallback(function (_ref2) {
|
|
62
|
+
var children = _ref2.children;
|
|
63
|
+
if (dynamic) {
|
|
64
|
+
// pass id list to dnd context
|
|
65
|
+
var idMap = fields.map(function (item) {
|
|
66
|
+
return item.id;
|
|
67
|
+
});
|
|
68
|
+
var handleDragEnd = function handleDragEnd(e) {
|
|
69
|
+
if (e.over && e.active.id !== e.over.id) {
|
|
70
|
+
// find index of item and drop over
|
|
71
|
+
var active = idMap.indexOf("".concat(e.active.id));
|
|
72
|
+
var end = idMap.indexOf("".concat(e.over.id));
|
|
73
|
+
move(active, end);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return /*#__PURE__*/jsx(DndContext, {
|
|
77
|
+
onDragEnd: handleDragEnd,
|
|
78
|
+
modifiers: [restrictToVerticalAxis],
|
|
79
|
+
children: /*#__PURE__*/jsx(SortableContext, {
|
|
80
|
+
items: idMap,
|
|
81
|
+
children: children
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return children;
|
|
86
|
+
}, [dynamic, fields]);
|
|
87
|
+
// @ts-expect-error ignore
|
|
88
|
+
Wrapper.displayName = "SortableWrapper";
|
|
89
|
+
return /*#__PURE__*/jsxs(Wrapper, {
|
|
90
|
+
children: [/*#__PURE__*/jsxs(Header, {
|
|
91
|
+
flex: true,
|
|
92
|
+
justify: "end",
|
|
93
|
+
className: "gap-1 px-2",
|
|
94
|
+
children: [dynamic && /*#__PURE__*/jsx(Button, {
|
|
95
|
+
size: "sm",
|
|
96
|
+
variant: "link",
|
|
97
|
+
icon: CoreIcons.insert,
|
|
98
|
+
"aria-label": "add field",
|
|
99
|
+
onClick: handleAdd
|
|
100
|
+
}), /*#__PURE__*/jsx(Button, {
|
|
101
|
+
size: "sm",
|
|
102
|
+
variant: "link",
|
|
103
|
+
icon: CoreIcons.colapseAllVar,
|
|
104
|
+
"aria-label": "collapse all",
|
|
105
|
+
onClick: function onClick() {
|
|
106
|
+
return setDefaultOpen(false);
|
|
107
|
+
}
|
|
108
|
+
}), /*#__PURE__*/jsx(Button, {
|
|
109
|
+
size: "sm",
|
|
110
|
+
variant: "link",
|
|
111
|
+
icon: CoreIcons.expandAllVar,
|
|
112
|
+
"aria-label": "expand all",
|
|
113
|
+
onClick: function onClick() {
|
|
114
|
+
return setDefaultOpen(true);
|
|
115
|
+
}
|
|
116
|
+
})]
|
|
117
|
+
}), /*#__PURE__*/jsx(Content, {
|
|
118
|
+
className: "divide-y",
|
|
119
|
+
children: fields.map(function (field, index) {
|
|
120
|
+
return /*#__PURE__*/jsx(SchemaField, {
|
|
121
|
+
defaultOpen: defaultOpen,
|
|
122
|
+
id: form.getValues().schema[index].id,
|
|
123
|
+
fieldId: field.id,
|
|
124
|
+
field: "schema.".concat(index),
|
|
125
|
+
dynamic: dynamic,
|
|
126
|
+
optionLists: optionLists,
|
|
127
|
+
onRemove: function onRemove() {
|
|
128
|
+
return handleRemove(index);
|
|
129
|
+
},
|
|
130
|
+
getModel: function getModel() {
|
|
131
|
+
return form.getValues().schema[index];
|
|
132
|
+
},
|
|
133
|
+
onTypeChange: function onTypeChange(val) {
|
|
134
|
+
return handleTypeChange(index, val);
|
|
135
|
+
},
|
|
136
|
+
acceptableTypes: acceptableTypes !== null && acceptableTypes !== void 0 ? acceptableTypes : Object.values(DATA_TYPES)
|
|
137
|
+
}, index);
|
|
138
|
+
})
|
|
139
|
+
}), AddModal]
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export { SchemaArrayForm };
|