@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.
@@ -1,433 +1,66 @@
1
1
  'use strict';
2
2
 
3
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
4
- var _defineProperty = require('@babel/runtime/helpers/defineProperty');
5
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
6
3
  var core = require('@react-fabric/core');
7
4
  var utilities = require('@react-fabric/utilities');
8
5
  var react = require('react');
9
- var Controller = require('../form/Controller.js');
10
6
  var Form = require('../form/Form.js');
11
- var ArrayInput = require('../input/ArrayInput.js');
12
- var Checkbox = require('../input/Checkbox.js');
13
- var DateInput = require('../input/DateInput.js');
14
- var Field = require('../input/Field.js');
15
- var Hidden = require('../input/Hidden.js');
16
- var Input = require('../input/Input.js');
17
- var Number = require('../input/Number.js');
18
- var Select = require('../select/Select.js');
19
7
  var schema = require('../types/schema.js');
8
+ var SchemaArrayForm = require('./components/SchemaArrayForm.js');
20
9
  var jsxRuntime = require('react/jsx-runtime');
21
10
 
22
- 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; }
23
- 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; }
24
- var CUSTOM_LIST = "--CUSTOM--";
25
- var SchemaField = function SchemaField(_ref) {
26
- var field = _ref.field,
27
- getModel = _ref.getModel,
28
- onRemove = _ref.onRemove,
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 ? utilities.EMPTY_ARRAY : _ref$optionLists,
31
- allowRemove = _ref.allowRemove,
32
- onTypeChange = _ref.onTypeChange,
33
- acceptableTypes = _ref.acceptableTypes;
34
- var _useState = react.useState(),
35
- _useState2 = _slicedToArray(_useState, 2),
36
- type = _useState2[0],
37
- setType = _useState2[1];
38
- var _useState3 = react.useState(false),
39
- _useState4 = _slicedToArray(_useState3, 2),
40
- multiple = _useState4[0],
41
- setMultiple = _useState4[1];
42
- var _useState5 = react.useState(false),
43
- _useState6 = _slicedToArray(_useState5, 2),
44
- hasOptions = _useState6[0],
45
- setHasOptions = _useState6[1];
46
- var _useState7 = react.useState(false),
47
- _useState8 = _slicedToArray(_useState7, 2),
48
- customOptions = _useState8[0],
49
- setCustomOptions = _useState8[1];
50
- var _useOverlayService = core.useOverlayService(function (props) {
51
- return /*#__PURE__*/jsxRuntime.jsxs(core.Modal, _objectSpread(_objectSpread({}, props), {}, {
52
- children: [/*#__PURE__*/jsxRuntime.jsx(core.Content, {
53
- children: /*#__PURE__*/jsxRuntime.jsx(ArrayInput.ArrayInput, {
54
- name: "".concat(field, ".options"),
55
- onAdd: function onAdd() {
56
- return String("");
57
- },
58
- children: /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
59
- children: /*#__PURE__*/jsxRuntime.jsx(Input.Input, {})
60
- })
61
- })
62
- }), /*#__PURE__*/jsxRuntime.jsx(core.Footer, {
63
- flex: true,
64
- justify: "end",
65
- className: "sticky bottom-0 border-t bg-base px-6 py-2",
66
- children: /*#__PURE__*/jsxRuntime.jsx(core.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
- react.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 = react.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 === schema.DATA_TYPES.AVATAR ? false : modelRef.multiple,
96
- minItems: modelRef.minItems,
97
- maxItems: modelRef.maxItems
98
- };
99
- if (type === schema.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 === schema.DATA_TYPES.DATE) {
106
- reset = Object.assign(reset, {
107
- type: "date"
108
- });
109
- } else if (type === schema.DATA_TYPES.NUMBER || type === schema.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 === schema.DATA_TYPES.AVATAR) {
117
- setMultiple(false);
118
- }
119
- onTypeChange(reset);
120
- setType(type);
121
- }
122
- }, []);
123
- return /*#__PURE__*/jsxRuntime.jsxs(core.Collapsable, {
124
- open: true,
125
- children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
126
- className: "flex flex-nowrap group/field",
127
- children: [/*#__PURE__*/jsxRuntime.jsx("span", {
128
- className: "flex-1 truncate",
129
- children: field
130
- }), allowRemove && /*#__PURE__*/jsxRuntime.jsx(core.Button, {
131
- size: "sm",
132
- color: "danger",
133
- variant: "link",
134
- "aria-label": "Delete",
135
- stopPropagation: true,
136
- icon: core.CoreIcons.trash,
137
- onClick: onRemove,
138
- className: "invisible group-hover/field:visible"
139
- })]
140
- }), /*#__PURE__*/jsxRuntime.jsxs("div", {
141
- className: "flex flex-col gap-1 pb-4",
142
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
143
- name: "".concat(field, ".id"),
144
- children: /*#__PURE__*/jsxRuntime.jsx(Hidden.HiddenInput, {
145
- hiddenValue: field
146
- })
147
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
148
- name: "".concat(field, ".datatype"),
149
- children: /*#__PURE__*/jsxRuntime.jsx(Select.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__*/jsxRuntime.jsx(Controller.Controller, {
159
- name: "".concat(field, ".label"),
160
- children: /*#__PURE__*/jsxRuntime.jsx(Input.Input, {
161
- inline: true,
162
- label: "Label",
163
- allowClear: true,
164
- placeholder: "Label...",
165
- decorateEndShowWhenEmpty: true,
166
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
167
- className: "text-sm text-muted/50 px-1",
168
- children: "OPTIONAL"
169
- })
170
- })
171
- }), type === schema.DATA_TYPES.STRING && /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
172
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
173
- name: "".concat(field, ".regex"),
174
- children: /*#__PURE__*/jsxRuntime.jsx(Input.Input, {
175
- inline: true,
176
- label: "Regex validation",
177
- allowClear: true,
178
- placeholder: "Regex value...",
179
- decorateEndShowWhenEmpty: true,
180
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
181
- className: "text-sm text-muted/50 px-1",
182
- children: "OPTIONAL"
183
- })
184
- })
185
- }), /*#__PURE__*/jsxRuntime.jsxs(Field.Field, {
186
- inline: true,
187
- label: "Option list",
188
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
189
- name: "".concat(field, ".optionList"),
190
- children: /*#__PURE__*/jsxRuntime.jsx(Select.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__*/jsxRuntime.jsx(core.Button, {
198
- icon: core.CoreIcons.list,
199
- "aria-label": "Custom options",
200
- onClick: function onClick() {
201
- void openOptionModal();
202
- }
203
- })]
204
- }), OptionModal]
205
- }), type === schema.DATA_TYPES.FILE && /*#__PURE__*/jsxRuntime.jsx(react.Fragment, {
206
- children: /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
207
- name: "".concat(field, ".accept"),
208
- children: /*#__PURE__*/jsxRuntime.jsx(Input.Input, {
209
- inline: true,
210
- label: "Accept types",
211
- allowClear: true,
212
- placeholder: "Accept value...",
213
- decorateEndShowWhenEmpty: true,
214
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
215
- className: "text-sm text-muted/50 px-1",
216
- children: "OPTIONAL"
217
- })
218
- })
219
- })
220
- }), [schema.DATA_TYPES.NUMBER, schema.DATA_TYPES.RANGE].includes(type) && /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
221
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
222
- name: "".concat(field, ".min"),
223
- children: /*#__PURE__*/jsxRuntime.jsx(Number.Number, {
224
- inline: true,
225
- label: "Min value",
226
- allowClear: true,
227
- placeholder: "Minimum value...",
228
- decorateEndShowWhenEmpty: true,
229
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
230
- className: "text-sm text-muted/50 px-1",
231
- children: "OPTIONAL"
232
- })
233
- })
234
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
235
- name: "".concat(field, ".max"),
236
- children: /*#__PURE__*/jsxRuntime.jsx(Number.Number, {
237
- inline: true,
238
- label: "Max value",
239
- allowClear: true,
240
- placeholder: "Maximum value...",
241
- decorateEndShowWhenEmpty: true,
242
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
243
- className: "text-sm text-muted/50 px-1",
244
- children: "OPTIONAL"
245
- })
246
- })
247
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
248
- name: "".concat(field, ".step"),
249
- children: /*#__PURE__*/jsxRuntime.jsx(Number.Number, {
250
- inline: true,
251
- label: "Step value",
252
- allowClear: true,
253
- placeholder: "Step value...",
254
- decorateEndShowWhenEmpty: true,
255
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
256
- className: "text-sm text-muted/50 px-1",
257
- children: "OPTIONAL"
258
- })
259
- })
260
- })]
261
- }), type === schema.DATA_TYPES.DATE && /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
262
- children: [/*#__PURE__*/jsxRuntime.jsx(Field.Field, {
263
- inline: true,
264
- label: "Type",
265
- plain: true,
266
- children: /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
267
- name: "".concat(field, ".type"),
268
- children: /*#__PURE__*/jsxRuntime.jsxs(core.ToggleButtonGroup, {
269
- value: "",
270
- fullWidth: true,
271
- className: "flex-1",
272
- children: [/*#__PURE__*/jsxRuntime.jsx(core.Button, {
273
- className: "flex-1",
274
- value: "date",
275
- children: "Date"
276
- }), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
277
- className: "flex-1",
278
- value: "datetime",
279
- children: "Date Time"
280
- })]
281
- })
282
- })
283
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
284
- name: "".concat(field, ".min"),
285
- children: /*#__PURE__*/jsxRuntime.jsx(DateInput.DateInput, {
286
- inline: true,
287
- label: "Min value",
288
- allowClear: true,
289
- placeholder: "Minimum value...",
290
- decorateEndShowWhenEmpty: true,
291
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
292
- className: "text-sm text-muted/50 px-1",
293
- children: "OPTIONAL"
294
- })
295
- })
296
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
297
- name: "".concat(field, ".max"),
298
- children: /*#__PURE__*/jsxRuntime.jsx(DateInput.DateInput, {
299
- inline: true,
300
- label: "Max value",
301
- allowClear: true,
302
- placeholder: "Maximum value...",
303
- decorateEndShowWhenEmpty: true,
304
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
305
- className: "text-sm text-muted/50 px-1",
306
- children: "OPTIONAL"
307
- })
308
- })
309
- })]
310
- }), /*#__PURE__*/jsxRuntime.jsxs(Field.Field, {
311
- label: " ",
312
- inline: true,
313
- plain: true,
314
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
315
- name: "".concat(field, ".required"),
316
- children: /*#__PURE__*/jsxRuntime.jsx(Checkbox.Checkbox, {
317
- label: "Required"
318
- })
319
- }), type !== schema.DATA_TYPES.AVATAR && /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
320
- name: "".concat(field, ".multiple"),
321
- children: /*#__PURE__*/jsxRuntime.jsx(Checkbox.Checkbox, {
322
- label: "Multiple",
323
- onChange: setMultiple
324
- })
325
- }), hasOptions && /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
326
- name: "".concat(field, ".allowCustom"),
327
- children: /*#__PURE__*/jsxRuntime.jsx(Checkbox.Checkbox, {
328
- label: "Allow custom value"
329
- })
330
- })]
331
- }), multiple && /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
332
- children: [/*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
333
- name: "".concat(field, ".minItems"),
334
- children: /*#__PURE__*/jsxRuntime.jsx(Number.Number, {
335
- inline: true,
336
- label: "Min items",
337
- allowClear: true,
338
- placeholder: "Minimum items...",
339
- decorateEndShowWhenEmpty: true,
340
- decorateEnd: /*#__PURE__*/jsxRuntime.jsx("span", {
341
- className: "text-sm text-muted/50 px-1",
342
- children: "OPTIONAL"
343
- })
344
- })
345
- }), /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
346
- name: "".concat(field, ".maxItems"),
347
- children: /*#__PURE__*/jsxRuntime.jsx(Number.Number, {
348
- inline: true,
349
- label: "Max items",
350
- allowClear: true,
351
- placeholder: "Maximum items...",
352
- decorateEndShowWhenEmpty: true,
353
- decorateEnd: /*#__PURE__*/jsxRuntime.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 ? utilities.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 = react.useRef(null);
372
- var _useState9 = react.useState([]),
373
- _useState10 = _slicedToArray(_useState9, 2),
374
- schemaFields = _useState10[0],
375
- setSchemaFields = _useState10[1];
376
- react.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 = react.useCallback(function (model) {
392
- onChange === null || onChange === void 0 || onChange(Object.values(model));
393
- }, []);
394
- var handleTypeChange = react.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 handleRemove = react.useCallback(function (field) {
399
- var _formRef$current$getV2, _formRef$current4, _formRef$current5;
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 = react.useCallback(function (model) {
48
+ onSubmit === null || onSubmit === void 0 || onSubmit(model.schema);
407
49
  }, []);
50
+ react.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__*/jsxRuntime.jsxs(Form.Form, {
409
57
  formRef: formRef,
410
- onSubmit: handleChange,
411
- children: [/*#__PURE__*/jsxRuntime.jsx(core.Content, {
412
- className: "divide-y",
413
- children: schemaFields.map(function (field) {
414
- return /*#__PURE__*/jsxRuntime.jsx(SchemaField, {
415
- field: field,
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(schema.DATA_TYPES)
429
- }, field);
430
- })
58
+ onChange: handleChange,
59
+ onSubmit: handleSubmit,
60
+ children: [/*#__PURE__*/jsxRuntime.jsx(SchemaArrayForm.SchemaArrayForm, {
61
+ dynamic: dynamic,
62
+ optionLists: optionLists,
63
+ acceptableTypes: acceptableTypes !== null && acceptableTypes !== void 0 ? acceptableTypes : Object.values(schema.DATA_TYPES)
431
64
  }), children && /*#__PURE__*/jsxRuntime.jsx(core.Footer, {
432
65
  flex: true,
433
66
  className: "border-t bg-base px-6 py-2",
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
+ var core = require('@react-fabric/core');
5
+ var utilities = require('@react-fabric/utilities');
6
+ var react = require('react');
7
+ var Controller = require('../../form/Controller.js');
8
+ var Form = require('../../form/Form.js');
9
+ var ArrayInput = require('../../input/ArrayInput.js');
10
+ var Input = require('../../input/Input.js');
11
+ var jsxRuntime = require('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 = utilities.yup.object({
16
+ fields: utilities.yup.array(utilities.yup.string().trim().required()).required().min(1)
17
+ });
18
+ var AddSchemaFields = function AddSchemaFields(props) {
19
+ var handleSubmit = react.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__*/jsxRuntime.jsx(core.Modal, _objectSpread(_objectSpread({}, props), {}, {
27
+ children: /*#__PURE__*/jsxRuntime.jsxs(Form.Form, {
28
+ schema: fieldSchema,
29
+ defaultValues: {
30
+ fields: [" "]
31
+ },
32
+ onSubmit: handleSubmit,
33
+ children: [/*#__PURE__*/jsxRuntime.jsxs(core.Content, {
34
+ children: [/*#__PURE__*/jsxRuntime.jsx(ArrayInput.ArrayInput, {
35
+ minItems: 1,
36
+ name: "fields",
37
+ onAdd: function onAdd() {
38
+ return String("");
39
+ },
40
+ children: /*#__PURE__*/jsxRuntime.jsx(Controller.Controller, {
41
+ children: /*#__PURE__*/jsxRuntime.jsx(Input.Input, {
42
+ autoFocus: true
43
+ })
44
+ })
45
+ }), /*#__PURE__*/jsxRuntime.jsx("p", {
46
+ className: "text-sm text-muted",
47
+ children: "Provide list of fields or comma-separated value"
48
+ })]
49
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Footer, {
50
+ flex: true,
51
+ justify: "end",
52
+ className: "px-2 py-1",
53
+ children: /*#__PURE__*/jsxRuntime.jsx(core.Button, {
54
+ type: "submit",
55
+ children: "Add"
56
+ })
57
+ })]
58
+ })
59
+ }));
60
+ };
61
+
62
+ exports.AddSchemaFields = AddSchemaFields;
@@ -0,0 +1,143 @@
1
+ 'use strict';
2
+
3
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
+ var core$1 = require('@dnd-kit/core');
5
+ var modifiers = require('@dnd-kit/modifiers');
6
+ var sortable = require('@dnd-kit/sortable');
7
+ var core = require('@react-fabric/core');
8
+ var utilities = require('@react-fabric/utilities');
9
+ var react = require('react');
10
+ var reactHookForm = require('react-hook-form');
11
+ var schema = require('../../types/schema.js');
12
+ var AddSchemaFields = require('./AddSchemaFields.js');
13
+ var SchemaField = require('./SchemaField.js');
14
+ var jsxRuntime = require('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 = reactHookForm.useFormContext();
21
+ var _useState = react.useState(true),
22
+ _useState2 = _slicedToArray(_useState, 2),
23
+ defaultOpen = _useState2[0],
24
+ setDefaultOpen = _useState2[1];
25
+ var _useFieldArray = reactHookForm.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 = react.useCallback(function (index, val) {
34
+ form.setValue("schema.".concat(index), val);
35
+ }, []);
36
+ var handleRemove = react.useCallback(function (index) {
37
+ remove(index);
38
+ }, []);
39
+ var _useOverlayService = core.useOverlayService(AddSchemaFields.AddSchemaFields),
40
+ _useOverlayService2 = _slicedToArray(_useOverlayService, 2),
41
+ AddModal = _useOverlayService2[0],
42
+ openAddfields = _useOverlayService2[1];
43
+ var handleAdd = react.useCallback(function () {
44
+ void openAddfields().then(function (fields) {
45
+ if (utilities.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: schema.DATA_TYPES.STRING,
54
+ label: ""
55
+ });
56
+ }
57
+ });
58
+ }
59
+ });
60
+ }, []);
61
+ var Wrapper = react.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__*/jsxRuntime.jsx(core$1.DndContext, {
77
+ onDragEnd: handleDragEnd,
78
+ modifiers: [modifiers.restrictToVerticalAxis],
79
+ children: /*#__PURE__*/jsxRuntime.jsx(sortable.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__*/jsxRuntime.jsxs(Wrapper, {
90
+ children: [/*#__PURE__*/jsxRuntime.jsxs(core.Header, {
91
+ flex: true,
92
+ justify: "end",
93
+ className: "gap-1 px-2",
94
+ children: [dynamic && /*#__PURE__*/jsxRuntime.jsx(core.Button, {
95
+ size: "sm",
96
+ variant: "link",
97
+ icon: core.CoreIcons.insert,
98
+ "aria-label": "add field",
99
+ onClick: handleAdd
100
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
101
+ size: "sm",
102
+ variant: "link",
103
+ icon: core.CoreIcons.colapseAllVar,
104
+ "aria-label": "collapse all",
105
+ onClick: function onClick() {
106
+ return setDefaultOpen(false);
107
+ }
108
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
109
+ size: "sm",
110
+ variant: "link",
111
+ icon: core.CoreIcons.expandAllVar,
112
+ "aria-label": "expand all",
113
+ onClick: function onClick() {
114
+ return setDefaultOpen(true);
115
+ }
116
+ })]
117
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Content, {
118
+ className: "divide-y",
119
+ children: fields.map(function (field, index) {
120
+ return /*#__PURE__*/jsxRuntime.jsx(SchemaField.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(schema.DATA_TYPES)
137
+ }, index);
138
+ })
139
+ }), AddModal]
140
+ });
141
+ };
142
+
143
+ exports.SchemaArrayForm = SchemaArrayForm;