@react-fabric/form 1.0.0-beta.32 → 1.0.0-beta.33

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.
Files changed (61) hide show
  1. package/dist/cjs/builder/FileField.js +4 -4
  2. package/dist/cjs/builder/FormField.js +41 -31
  3. package/dist/cjs/builder/SchemaEditor.js +428 -0
  4. package/dist/cjs/form/Controller.js +1 -1
  5. package/dist/cjs/form/Form.js +3 -4
  6. package/dist/cjs/hooks/useFormBuilder.js +27 -15
  7. package/dist/cjs/index.js +8 -2
  8. package/dist/cjs/input/Checkbox.js +4 -3
  9. package/dist/cjs/input/DateInput.js +183 -0
  10. package/dist/cjs/input/Field.js +1 -1
  11. package/dist/cjs/input/{Range.js → RangeSlider.js} +2 -2
  12. package/dist/cjs/input/Switch.js +4 -3
  13. package/dist/cjs/internal/Field.module.css +1 -1
  14. package/dist/cjs/internal/InputWrapper.js +4 -2
  15. package/dist/cjs/types/schema.js +40 -0
  16. package/dist/esm/builder/FileField.js +4 -4
  17. package/dist/esm/builder/FormField.js +41 -31
  18. package/dist/esm/builder/SchemaEditor.js +428 -0
  19. package/dist/esm/form/Controller.js +1 -1
  20. package/dist/esm/form/Form.js +4 -5
  21. package/dist/esm/hooks/useFormBuilder.js +25 -13
  22. package/dist/esm/index.js +4 -1
  23. package/dist/esm/input/Checkbox.js +4 -3
  24. package/dist/esm/input/DateInput.js +183 -0
  25. package/dist/esm/input/Field.js +1 -1
  26. package/dist/esm/input/{Range.js → RangeSlider.js} +2 -2
  27. package/dist/esm/input/Switch.js +4 -3
  28. package/dist/esm/internal/Field.module.css +1 -1
  29. package/dist/esm/internal/InputWrapper.js +4 -2
  30. package/dist/esm/types/schema.js +40 -0
  31. package/dist/types/builder/FileField.d.ts +2 -2
  32. package/dist/types/builder/FormField.d.ts +3 -1
  33. package/dist/types/builder/SchemaEditor.d.ts +10 -0
  34. package/dist/types/form/Controller.d.ts +1 -1
  35. package/dist/types/form/Form.d.ts +5 -1
  36. package/dist/types/hooks/useFormBuilder.d.ts +3 -1
  37. package/dist/types/index.d.ts +4 -2
  38. package/dist/types/input/Checkbox.d.ts +4 -3
  39. package/dist/types/input/DateInput.d.ts +19 -0
  40. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
  41. package/dist/types/input/Switch.d.ts +1 -1
  42. package/dist/types/internal/InputWrapper.d.ts +1 -1
  43. package/dist/types/types/index.d.ts +6 -2
  44. package/dist/types/types/schema.d.ts +52 -33
  45. package/package.json +5 -4
  46. package/src/builder/FileField.tsx +5 -4
  47. package/src/builder/FormField.tsx +42 -29
  48. package/src/builder/SchemaEditor.tsx +441 -0
  49. package/src/form/Controller.tsx +2 -2
  50. package/src/form/Form.tsx +6 -4
  51. package/src/hooks/useFormBuilder.tsx +29 -15
  52. package/src/index.ts +5 -2
  53. package/src/input/Checkbox.tsx +4 -3
  54. package/src/input/DateInput.tsx +248 -0
  55. package/src/input/Field.tsx +2 -1
  56. package/src/input/{Range.tsx → RangeSlider.tsx} +1 -1
  57. package/src/input/Switch.tsx +2 -0
  58. package/src/internal/Field.module.css +12 -1
  59. package/src/internal/InputWrapper.tsx +5 -0
  60. package/src/types/index.ts +8 -4
  61. package/src/types/schema.ts +65 -41
@@ -0,0 +1,428 @@
1
+ 'use client';
2
+
3
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
6
+ import { Content, Footer, Button, useOverlayService, Modal, Collapsable, CoreIcons, ToggleButtonGroup } from '@react-fabric/core';
7
+ import { EMPTY_ARRAY } from '@react-fabric/utilities';
8
+ import { useRef, useState, useEffect, useCallback, Fragment } from 'react';
9
+ import { Controller } from '../form/Controller.js';
10
+ 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 { Input } from '../input/Input.js';
16
+ import { Number } from '../input/Number.js';
17
+ import { Select } from '../select/Select.js';
18
+ import { DATA_TYPES } from '../types/schema.js';
19
+ import { jsxs, jsx } from 'react/jsx-runtime';
20
+
21
+ 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; }
22
+ 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; }
23
+ var CUSTOM_LIST = "--CUSTOM--";
24
+ var SchemaField = function SchemaField(_ref) {
25
+ var field = _ref.field,
26
+ getModel = _ref.getModel,
27
+ _ref$optionLists = _ref.optionLists,
28
+ optionLists = _ref$optionLists === void 0 ? EMPTY_ARRAY : _ref$optionLists,
29
+ allowRemove = _ref.allowRemove,
30
+ onTypeChange = _ref.onTypeChange,
31
+ acceptableTypes = _ref.acceptableTypes;
32
+ var _useState = useState(),
33
+ _useState2 = _slicedToArray(_useState, 2),
34
+ type = _useState2[0],
35
+ setType = _useState2[1];
36
+ var _useState3 = useState(false),
37
+ _useState4 = _slicedToArray(_useState3, 2),
38
+ multiple = _useState4[0],
39
+ setMultiple = _useState4[1];
40
+ var _useState5 = useState(false),
41
+ _useState6 = _slicedToArray(_useState5, 2),
42
+ hasOptions = _useState6[0],
43
+ setHasOptions = _useState6[1];
44
+ var _useState7 = useState(false),
45
+ _useState8 = _slicedToArray(_useState7, 2),
46
+ customOptions = _useState8[0],
47
+ setCustomOptions = _useState8[1];
48
+ var _useOverlayService = useOverlayService(function (props) {
49
+ return /*#__PURE__*/jsxs(Modal, _objectSpread(_objectSpread({}, props), {}, {
50
+ children: [/*#__PURE__*/jsx(Content, {
51
+ children: /*#__PURE__*/jsx(ArrayInput, {
52
+ name: "".concat(field, ".options"),
53
+ onAdd: function onAdd() {
54
+ return String("");
55
+ },
56
+ children: /*#__PURE__*/jsx(Controller, {
57
+ children: /*#__PURE__*/jsx(Input, {})
58
+ })
59
+ })
60
+ }), /*#__PURE__*/jsx(Footer, {
61
+ flex: true,
62
+ justify: "end",
63
+ className: "sticky bottom-0 border-t bg-base px-6 py-2",
64
+ children: /*#__PURE__*/jsx(Button, {
65
+ variant: "link",
66
+ onClick: props.onClose,
67
+ children: "Close"
68
+ })
69
+ })]
70
+ }));
71
+ }),
72
+ _useOverlayService2 = _slicedToArray(_useOverlayService, 2),
73
+ OptionModal = _useOverlayService2[0],
74
+ openOptionModal = _useOverlayService2[1];
75
+ useEffect(function () {
76
+ setTimeout(function () {
77
+ var model = getModel();
78
+ setType(model.datatype);
79
+ setMultiple(model.multiple);
80
+ setHasOptions(!!model.optionList);
81
+ setCustomOptions(model.optionList === CUSTOM_LIST);
82
+ }, 10);
83
+ }, []);
84
+ useEffect(function () {
85
+ if (type) {
86
+ var modelRef = getModel();
87
+ var reset = {
88
+ datatype: type,
89
+ id: field,
90
+ label: modelRef.label,
91
+ defaultValue: modelRef.defaultValue,
92
+ required: modelRef.required,
93
+ multiple: type === DATA_TYPES.AVATAR ? false : modelRef.multiple,
94
+ minItems: modelRef.minItems,
95
+ maxItems: modelRef.maxItems
96
+ };
97
+ if (type === DATA_TYPES.STRING) {
98
+ reset = Object.assign(reset, {
99
+ optionList: modelRef.optionList,
100
+ options: modelRef.options,
101
+ regex: modelRef.regex
102
+ });
103
+ } else if (type === DATA_TYPES.DATE) {
104
+ reset = Object.assign(reset, {
105
+ type: "date"
106
+ });
107
+ } else if (type === DATA_TYPES.NUMBER || type === DATA_TYPES.RANGE) {
108
+ reset = Object.assign(reset, {
109
+ min: modelRef.min,
110
+ max: modelRef.max,
111
+ step: modelRef.step
112
+ });
113
+ }
114
+ if (type === DATA_TYPES.AVATAR) {
115
+ setMultiple(false);
116
+ }
117
+ onTypeChange(reset);
118
+ }
119
+ }, [type]);
120
+ return /*#__PURE__*/jsxs(Collapsable, {
121
+ open: true,
122
+ children: [/*#__PURE__*/jsxs("div", {
123
+ className: "flex flex-nowrap group/field",
124
+ children: [/*#__PURE__*/jsx("span", {
125
+ className: "flex-1 truncate",
126
+ children: field
127
+ }), allowRemove && /*#__PURE__*/jsx(Button, {
128
+ size: "sm",
129
+ color: "danger",
130
+ variant: "link",
131
+ "aria-label": "Delete",
132
+ stopPropagation: true,
133
+ icon: CoreIcons.trash,
134
+ className: "invisible group-hover/field:visible"
135
+ })]
136
+ }), /*#__PURE__*/jsxs("div", {
137
+ className: "flex flex-col gap-1 pb-4",
138
+ children: [/*#__PURE__*/jsx(Controller, {
139
+ name: "".concat(field, ".datatype"),
140
+ children: /*#__PURE__*/jsx(Select, {
141
+ inline: true,
142
+ label: "Type",
143
+ placeholder: "Data type",
144
+ options: acceptableTypes,
145
+ onChange: function onChange(val) {
146
+ return setType(val);
147
+ }
148
+ })
149
+ }), /*#__PURE__*/jsx(Controller, {
150
+ name: "".concat(field, ".label"),
151
+ children: /*#__PURE__*/jsx(Input, {
152
+ inline: true,
153
+ label: "Label",
154
+ allowClear: true,
155
+ placeholder: "Label...",
156
+ decorateEndShowWhenEmpty: true,
157
+ decorateEnd: /*#__PURE__*/jsx("span", {
158
+ className: "text-sm text-muted/50 px-1",
159
+ children: "OPTIONAL"
160
+ })
161
+ })
162
+ }), type === DATA_TYPES.STRING && /*#__PURE__*/jsxs(Fragment, {
163
+ children: [/*#__PURE__*/jsx(Controller, {
164
+ name: "".concat(field, ".regex"),
165
+ children: /*#__PURE__*/jsx(Input, {
166
+ inline: true,
167
+ label: "Regex validation",
168
+ allowClear: true,
169
+ placeholder: "Regex value...",
170
+ decorateEndShowWhenEmpty: true,
171
+ decorateEnd: /*#__PURE__*/jsx("span", {
172
+ className: "text-sm text-muted/50 px-1",
173
+ children: "OPTIONAL"
174
+ })
175
+ })
176
+ }), /*#__PURE__*/jsxs(Field, {
177
+ inline: true,
178
+ label: "Option list",
179
+ children: [/*#__PURE__*/jsx(Controller, {
180
+ name: "".concat(field, ".optionList"),
181
+ children: /*#__PURE__*/jsx(Select, {
182
+ allowClear: true,
183
+ options: [CUSTOM_LIST].concat(_toConsumableArray(optionLists)),
184
+ onChange: function onChange(v) {
185
+ return setCustomOptions(v === CUSTOM_LIST);
186
+ }
187
+ })
188
+ }), customOptions && /*#__PURE__*/jsx(Button, {
189
+ icon: CoreIcons.list,
190
+ "aria-label": "Custom options",
191
+ onClick: function onClick() {
192
+ void openOptionModal();
193
+ }
194
+ })]
195
+ }), OptionModal]
196
+ }), type === DATA_TYPES.FILE && /*#__PURE__*/jsx(Fragment, {
197
+ children: /*#__PURE__*/jsx(Controller, {
198
+ name: "".concat(field, ".accept"),
199
+ children: /*#__PURE__*/jsx(Input, {
200
+ inline: true,
201
+ label: "Accept types",
202
+ allowClear: true,
203
+ placeholder: "Accept value...",
204
+ decorateEndShowWhenEmpty: true,
205
+ decorateEnd: /*#__PURE__*/jsx("span", {
206
+ className: "text-sm text-muted/50 px-1",
207
+ children: "OPTIONAL"
208
+ })
209
+ })
210
+ })
211
+ }), [DATA_TYPES.NUMBER, DATA_TYPES.RANGE].includes(type) && /*#__PURE__*/jsxs(Fragment, {
212
+ children: [/*#__PURE__*/jsx(Controller, {
213
+ name: "".concat(field, ".min"),
214
+ children: /*#__PURE__*/jsx(Number, {
215
+ inline: true,
216
+ label: "Min value",
217
+ allowClear: true,
218
+ placeholder: "Minimum value...",
219
+ decorateEndShowWhenEmpty: true,
220
+ decorateEnd: /*#__PURE__*/jsx("span", {
221
+ className: "text-sm text-muted/50 px-1",
222
+ children: "OPTIONAL"
223
+ })
224
+ })
225
+ }), /*#__PURE__*/jsx(Controller, {
226
+ name: "".concat(field, ".max"),
227
+ children: /*#__PURE__*/jsx(Number, {
228
+ inline: true,
229
+ label: "Max value",
230
+ allowClear: true,
231
+ placeholder: "Maximum value...",
232
+ decorateEndShowWhenEmpty: true,
233
+ decorateEnd: /*#__PURE__*/jsx("span", {
234
+ className: "text-sm text-muted/50 px-1",
235
+ children: "OPTIONAL"
236
+ })
237
+ })
238
+ }), /*#__PURE__*/jsx(Controller, {
239
+ name: "".concat(field, ".step"),
240
+ children: /*#__PURE__*/jsx(Number, {
241
+ inline: true,
242
+ label: "Step value",
243
+ allowClear: true,
244
+ placeholder: "Step value...",
245
+ decorateEndShowWhenEmpty: true,
246
+ decorateEnd: /*#__PURE__*/jsx("span", {
247
+ className: "text-sm text-muted/50 px-1",
248
+ children: "OPTIONAL"
249
+ })
250
+ })
251
+ })]
252
+ }), type === DATA_TYPES.DATE && /*#__PURE__*/jsxs(Fragment, {
253
+ children: [/*#__PURE__*/jsx(Field, {
254
+ inline: true,
255
+ label: "Type",
256
+ plain: true,
257
+ children: /*#__PURE__*/jsx(Controller, {
258
+ name: "".concat(field, ".type"),
259
+ children: /*#__PURE__*/jsxs(ToggleButtonGroup, {
260
+ value: "",
261
+ fullWidth: true,
262
+ className: "flex-1",
263
+ children: [/*#__PURE__*/jsx(Button, {
264
+ className: "flex-1",
265
+ value: "date",
266
+ children: "Date"
267
+ }), /*#__PURE__*/jsx(Button, {
268
+ className: "flex-1",
269
+ value: "datetime",
270
+ children: "Date Time"
271
+ })]
272
+ })
273
+ })
274
+ }), /*#__PURE__*/jsx(Controller, {
275
+ name: "".concat(field, ".min"),
276
+ children: /*#__PURE__*/jsx(DateInput, {
277
+ inline: true,
278
+ label: "Min value",
279
+ allowClear: true,
280
+ placeholder: "Minimum value...",
281
+ decorateEndShowWhenEmpty: true,
282
+ decorateEnd: /*#__PURE__*/jsx("span", {
283
+ className: "text-sm text-muted/50 px-1",
284
+ children: "OPTIONAL"
285
+ })
286
+ })
287
+ }), /*#__PURE__*/jsx(Controller, {
288
+ name: "".concat(field, ".max"),
289
+ children: /*#__PURE__*/jsx(DateInput, {
290
+ inline: true,
291
+ label: "Max value",
292
+ allowClear: true,
293
+ placeholder: "Maximum value...",
294
+ decorateEndShowWhenEmpty: true,
295
+ decorateEnd: /*#__PURE__*/jsx("span", {
296
+ className: "text-sm text-muted/50 px-1",
297
+ children: "OPTIONAL"
298
+ })
299
+ })
300
+ })]
301
+ }), /*#__PURE__*/jsxs(Field, {
302
+ label: " ",
303
+ inline: true,
304
+ plain: true,
305
+ children: [/*#__PURE__*/jsx(Controller, {
306
+ name: "".concat(field, ".required"),
307
+ children: /*#__PURE__*/jsx(Checkbox, {
308
+ label: "Required"
309
+ })
310
+ }), type !== DATA_TYPES.AVATAR && /*#__PURE__*/jsx(Controller, {
311
+ name: "".concat(field, ".multiple"),
312
+ children: /*#__PURE__*/jsx(Checkbox, {
313
+ label: "Multiple",
314
+ onChange: setMultiple
315
+ })
316
+ }), hasOptions && /*#__PURE__*/jsx(Controller, {
317
+ name: "".concat(field, ".allowCustom"),
318
+ children: /*#__PURE__*/jsx(Checkbox, {
319
+ label: "Allow custom value"
320
+ })
321
+ })]
322
+ }), multiple && /*#__PURE__*/jsxs(Fragment, {
323
+ children: [/*#__PURE__*/jsx(Controller, {
324
+ name: "".concat(field, ".minItems"),
325
+ children: /*#__PURE__*/jsx(Number, {
326
+ inline: true,
327
+ label: "Min items",
328
+ allowClear: true,
329
+ placeholder: "Minimum items...",
330
+ decorateEndShowWhenEmpty: true,
331
+ decorateEnd: /*#__PURE__*/jsx("span", {
332
+ className: "text-sm text-muted/50 px-1",
333
+ children: "OPTIONAL"
334
+ })
335
+ })
336
+ }), /*#__PURE__*/jsx(Controller, {
337
+ name: "".concat(field, ".maxItems"),
338
+ children: /*#__PURE__*/jsx(Number, {
339
+ inline: true,
340
+ label: "Max items",
341
+ allowClear: true,
342
+ placeholder: "Maximum items...",
343
+ decorateEndShowWhenEmpty: true,
344
+ decorateEnd: /*#__PURE__*/jsx("span", {
345
+ className: "text-sm text-muted/50 px-1",
346
+ children: "OPTIONAL"
347
+ })
348
+ })
349
+ })]
350
+ })]
351
+ })]
352
+ }, field);
353
+ };
354
+ var SchemaEditor = function SchemaEditor(_ref2) {
355
+ var schemaDef = _ref2.schemaDef,
356
+ allowRemove = _ref2.allowRemove,
357
+ acceptableTypes = _ref2.acceptableTypes,
358
+ _ref2$optionLists = _ref2.optionLists,
359
+ optionLists = _ref2$optionLists === void 0 ? EMPTY_ARRAY : _ref2$optionLists,
360
+ onChange = _ref2.onChange;
361
+ var formRef = useRef(null);
362
+ var schemaRef = useRef({});
363
+ var _useState9 = useState({}),
364
+ _useState10 = _slicedToArray(_useState9, 2),
365
+ schema = _useState10[0],
366
+ setSchema = _useState10[1];
367
+ var _useState11 = useState([]),
368
+ _useState12 = _slicedToArray(_useState11, 2),
369
+ schemaFields = _useState12[0],
370
+ setSchemaFields = _useState12[1];
371
+ useEffect(function () {
372
+ var schemaMap = Object.fromEntries(schemaDef.map(function (schema) {
373
+ return [schema.id, schema];
374
+ }));
375
+ var schema = Object.fromEntries(Object.keys(schemaMap).map(function (field) {
376
+ var _schemaRef$current$fi;
377
+ return [field, (_schemaRef$current$fi = schemaRef.current[field]) !== null && _schemaRef$current$fi !== void 0 ? _schemaRef$current$fi : schemaMap[field]];
378
+ }));
379
+ setSchema(schema);
380
+ setSchemaFields(Object.keys(schema));
381
+ }, [schemaDef]);
382
+ var handleChange = useCallback(function (model) {
383
+ onChange === null || onChange === void 0 || onChange(Object.values(model));
384
+ }, []);
385
+ var handleTypeChange = useCallback(function (field, val) {
386
+ var _formRef$current;
387
+ (_formRef$current = formRef.current) === null || _formRef$current === void 0 || _formRef$current.setValue(field, val);
388
+ }, []);
389
+ return /*#__PURE__*/jsxs(Form, {
390
+ formRef: formRef,
391
+ values: schema,
392
+ defaultValues: schema,
393
+ onSubmit: handleChange,
394
+ children: [/*#__PURE__*/jsx(Content, {
395
+ className: "divide-y",
396
+ children: schemaFields.map(function (field) {
397
+ return /*#__PURE__*/jsx(SchemaField, {
398
+ field: field,
399
+ allowRemove: allowRemove,
400
+ optionLists: optionLists,
401
+ getModel: function getModel() {
402
+ var _formRef$current2;
403
+ return (_formRef$current2 = formRef.current) === null || _formRef$current2 === void 0 ? void 0 : _formRef$current2.getValues()[field];
404
+ },
405
+ onTypeChange: function onTypeChange(val) {
406
+ return handleTypeChange(field, val);
407
+ },
408
+ acceptableTypes: acceptableTypes !== null && acceptableTypes !== void 0 ? acceptableTypes : Object.values(DATA_TYPES)
409
+ }, field);
410
+ })
411
+ }), /*#__PURE__*/jsxs(Footer, {
412
+ flex: true,
413
+ justify: "end",
414
+ className: "sticky bottom-0 border-t bg-base px-6 py-2",
415
+ children: [/*#__PURE__*/jsx(Button, {
416
+ type: "reset",
417
+ variant: "link",
418
+ children: "Reset"
419
+ }), /*#__PURE__*/jsx(Button, {
420
+ type: "submit",
421
+ variant: "solid",
422
+ children: "Submit"
423
+ })]
424
+ })]
425
+ });
426
+ };
427
+
428
+ export { SchemaEditor };
@@ -46,7 +46,7 @@ var Controller = function Controller(_ref) {
46
46
  (_children$props$onCha = (_children$props = children.props).onChange) === null || _children$props$onCha === void 0 || _children$props$onCha.call(_children$props, v);
47
47
  }
48
48
  })) : children(_objectSpread(_objectSpread(_objectSpread({}, field), rest), {}, {
49
- inputRef: function inputRef(el) {
49
+ ref: function ref(el) {
50
50
  el && _ref3(el);
51
51
  },
52
52
  readOnly: isSubmitting,
@@ -7,11 +7,11 @@ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProper
7
7
  import _regeneratorRuntime from '@babel/runtime/regenerator';
8
8
  import { yupResolver } from '@hookform/resolvers/yup';
9
9
  import { useDebounce } from '@react-fabric/core';
10
- import { useRef, useEffect, useTransition, useCallback, useImperativeHandle } from 'react';
10
+ import { useRef, useTransition, useEffect, useCallback, useImperativeHandle } from 'react';
11
11
  import { useForm, FormProvider } from 'react-hook-form';
12
12
  import { jsx, jsxs } from 'react/jsx-runtime';
13
13
 
14
- var _excluded = ["formRef", "schema", "children", "defaultValues", "onSubmit", "onChange"];
14
+ var _excluded = ["formRef", "schema", "children", "values", "defaultValues", "onSubmit", "onChange"];
15
15
  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; }
16
16
  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; }
17
17
  var DEFAULT_SUBMIT = function DEFAULT_SUBMIT() {
@@ -21,6 +21,7 @@ var Form = function Form(_ref) {
21
21
  var formRef = _ref.formRef,
22
22
  schema = _ref.schema,
23
23
  children = _ref.children,
24
+ values = _ref.values,
24
25
  defaultValues = _ref.defaultValues,
25
26
  _ref$onSubmit = _ref.onSubmit,
26
27
  onSubmit = _ref$onSubmit === void 0 ? DEFAULT_SUBMIT : _ref$onSubmit,
@@ -28,13 +29,11 @@ var Form = function Form(_ref) {
28
29
  rest = _objectWithoutProperties(_ref, _excluded);
29
30
  var ref = useRef(null);
30
31
  var form = useForm({
32
+ values: values,
31
33
  shouldFocusError: true,
32
34
  resolver: schema && yupResolver(schema),
33
35
  defaultValues: defaultValues
34
36
  });
35
- useEffect(function () {
36
- form.reset();
37
- }, []);
38
37
  var _useTransition = useTransition(),
39
38
  _useTransition2 = _slicedToArray(_useTransition, 2),
40
39
  startTransition = _useTransition2[1];
@@ -1,33 +1,42 @@
1
1
  'use client';
2
2
 
3
3
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+ import { EmptyContent } from '@react-fabric/core';
4
5
  import { yup, isNil, isEmpty } from '@react-fabric/utilities';
5
6
  import { useMemo, Fragment } from 'react';
6
7
  import { FormField } from '../builder/FormField.js';
7
- import { jsx } from 'react/jsx-runtime';
8
+ import { DATA_TYPES } from '../types/schema.js';
9
+ import { jsxs, jsx } from 'react/jsx-runtime';
8
10
 
9
11
  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; }
10
12
  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; }
11
13
  var makeYupField = function makeYupField(field) {
12
- var yupField = yup.string();
13
- if (field.datatype === "schema") {
14
+ var yupField = yup.string().trim();
15
+ if (field.datatype === DATA_TYPES.SCHEMA) {
14
16
  var child = Object.fromEntries(field.schema.map(function (field) {
15
17
  return [field.id, makeYupField(field)];
16
18
  }));
17
19
  yupField = yup.object(child);
18
20
  }
19
- if (field.datatype === "boolean") yupField = yup["boolean"]();
20
- if (field.datatype === "decimal" || field.datatype === "number" || field.datatype === "range") {
21
+ if (field.datatype === DATA_TYPES.BOOL) {
22
+ yupField = yup["boolean"]();
23
+ if (field.required) yupField = yupField.test({
24
+ test: function test(value) {
25
+ return !!value;
26
+ }
27
+ });
28
+ }
29
+ if (field.datatype === DATA_TYPES.NUMBER || field.datatype === DATA_TYPES.RANGE) {
21
30
  yupField = yup.number();
22
31
  !isNil(field.min) && (yupField = yupField.min(field.min));
23
32
  !isNil(field.max) && (yupField = yupField.max(field.max));
24
- if (field.datatype === "range") yupField = yup.array(yupField);
33
+ if (field.datatype === DATA_TYPES.RANGE) yupField = yup.array(yupField);
25
34
  }
26
- if (field.datatype === "string") {
27
- field.options && (yupField = yupField.oneOf(field.options));
35
+ if (field.datatype === DATA_TYPES.STRING) {
36
+ field.options && !field.allowCustom && (yupField = yupField.oneOf(field.options));
28
37
  field.regex && (yupField = yupField.matches(new RegExp(field.regex)));
29
38
  }
30
- if (field.datatype === "file") {
39
+ if (field.datatype === DATA_TYPES.FILE) {
31
40
  yupField = yup.object({
32
41
  filename: yup.string().required(),
33
42
  path: yup.string().required()
@@ -64,19 +73,22 @@ var useFormBuilder = function useFormBuilder(schema) {
64
73
  var schemaDef = yup.object(Object.fromEntries(filteredSchema.map(function (field) {
65
74
  return [field.id, makeYupField(field)];
66
75
  })));
67
- var formDef = /*#__PURE__*/jsx(Fragment, {
68
- children: filteredSchema.map(function (field, idx) {
76
+ var formDef = /*#__PURE__*/jsxs(Fragment, {
77
+ children: [filteredSchema.map(function (field, idx) {
69
78
  return /*#__PURE__*/jsx("div", {
70
79
  className: "mb-4",
71
- children: field.datatype === "schema" ? null : /*#__PURE__*/jsx(FormField, _objectSpread(_objectSpread({}, field), {}, {
80
+ children: field.datatype === DATA_TYPES.SCHEMA ? null : /*#__PURE__*/jsx(FormField, _objectSpread(_objectSpread({}, field), {}, {
72
81
  autoFocus: idx === 0,
73
82
  fileUrl: options.fileUrl,
83
+ optionLists: options.optionLists,
74
84
  uploadHandler: options.uploadHandler,
75
85
  inline: options.inline,
76
86
  prefix: options.prefix
77
87
  }))
78
88
  }, field.id);
79
- })
89
+ }), filteredSchema.length === 0 && /*#__PURE__*/jsx(EmptyContent, {
90
+ message: "No schema defined"
91
+ })]
80
92
  });
81
93
  return {
82
94
  formDef: formDef,
package/dist/esm/index.js CHANGED
@@ -3,12 +3,15 @@
3
3
  import './i18n/index.js';
4
4
  export { Controller } from './form/Controller.js';
5
5
  export { Form } from './form/Form.js';
6
+ export { SchemaEditor } from './builder/SchemaEditor.js';
6
7
  export { useFileUploader } from './hooks/useFileUploader.js';
7
8
  export { useFormBuilder } from './hooks/useFormBuilder.js';
9
+ export { DATA_TYPES } from './types/schema.js';
8
10
  export { ArrayInput } from './input/ArrayInput.js';
9
11
  export { Checkbox } from './input/Checkbox.js';
10
12
  export { ColorInput } from './input/ColorInput.js';
11
13
  export { ColorPicker } from './input/ColorPicker.js';
14
+ export { DateInput } from './input/DateInput.js';
12
15
  export { Field } from './input/Field.js';
13
16
  export { FileInput } from './input/File.js';
14
17
  export { HiddenInput } from './input/Hidden.js';
@@ -17,7 +20,7 @@ export { Number } from './input/Number.js';
17
20
  export { Password } from './input/Password.js';
18
21
  export { Radio } from './input/Radio.js';
19
22
  export { RadioGroup } from './input/RadioGroup.js';
20
- export { Range } from './input/Range.js';
23
+ export { RangeSlider } from './input/RangeSlider.js';
21
24
  export { Search } from './input/Search.js';
22
25
  export { Slider } from './input/Slider.js';
23
26
  export { Switch } from './input/Switch.js';
@@ -9,7 +9,7 @@ import { useState, useDeferredValue, useMemo, useEffect, useCallback } from 'rea
9
9
  import { ErrorIcon } from '../internal/ErrorIcon.js';
10
10
  import { jsxs, jsx } from 'react/jsx-runtime';
11
11
 
12
- var _excluded = ["ref", "label", "value", "name", "checked", "invalid", "disabled", "error", "width", "icon", "iconChecked", "onChange"];
12
+ var _excluded = ["ref", "label", "value", "name", "checked", "invalid", "disabled", "error", "width", "icon", "iconChecked", "required", "onChange"];
13
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
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
15
  var Checkbox = function Checkbox(_ref) {
@@ -23,8 +23,9 @@ var Checkbox = function Checkbox(_ref) {
23
23
  error = _ref.error,
24
24
  width = _ref.width,
25
25
  icon = _ref.icon,
26
- iconChecked = _ref.iconChecked,
27
- onChange = _ref.onChange,
26
+ iconChecked = _ref.iconChecked;
27
+ _ref.required;
28
+ var onChange = _ref.onChange,
28
29
  rest = _objectWithoutProperties(_ref, _excluded);
29
30
  var _useState = useState(false),
30
31
  _useState2 = _slicedToArray(_useState, 2),