@insticc/genericform 2.0.7 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -3,6 +3,96 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "CustomAccordion", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _CustomAccordion["default"];
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "CustomAction", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _CustomAction["default"];
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "CustomCheckbox", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _CustomCheckbox["default"];
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "CustomCheckboxGroup", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _CustomCheckboxGroup["default"];
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "CustomDatePicker", {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _CustomDatePicker["default"];
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "CustomDropdown", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _CustomDropdown["default"];
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "CustomFileInput", {
43
+ enumerable: true,
44
+ get: function get() {
45
+ return _CustomFileInput["default"];
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "CustomInput", {
49
+ enumerable: true,
50
+ get: function get() {
51
+ return _CustomInput["default"];
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "CustomJson", {
55
+ enumerable: true,
56
+ get: function get() {
57
+ return _CustomJson["default"];
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "CustomMobilePhone", {
61
+ enumerable: true,
62
+ get: function get() {
63
+ return _CustomMobilePhone["default"];
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "CustomMultiDropdown", {
67
+ enumerable: true,
68
+ get: function get() {
69
+ return _CustomMultiDropdown["default"];
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "CustomNumberInput", {
73
+ enumerable: true,
74
+ get: function get() {
75
+ return _CustomNumberInput["default"];
76
+ }
77
+ });
78
+ Object.defineProperty(exports, "CustomRadioGroup", {
79
+ enumerable: true,
80
+ get: function get() {
81
+ return _CustomRadioGroup["default"];
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "CustomTextArea", {
85
+ enumerable: true,
86
+ get: function get() {
87
+ return _CustomTextArea["default"];
88
+ }
89
+ });
90
+ Object.defineProperty(exports, "CustomToggle", {
91
+ enumerable: true,
92
+ get: function get() {
93
+ return _CustomToggle["default"];
94
+ }
95
+ });
6
96
  Object.defineProperty(exports, "Demo", {
7
97
  enumerable: true,
8
98
  get: function get() {
@@ -19,14 +109,17 @@ exports["default"] = void 0;
19
109
  var _react = require("react");
20
110
  var _reactBootstrap = require("react-bootstrap");
21
111
  var _CustomAccordion = _interopRequireDefault(require("./FormFields/CustomAccordion"));
112
+ var _CustomAction = _interopRequireDefault(require("./FormFields/CustomAction"));
22
113
  var _CustomCheckbox = _interopRequireDefault(require("./FormFields/CustomCheckbox"));
23
114
  var _CustomCheckboxGroup = _interopRequireDefault(require("./FormFields/CustomCheckboxGroup"));
24
115
  var _CustomComponent = _interopRequireDefault(require("./FormFields/CustomComponent"));
25
116
  var _CustomDatePicker = _interopRequireDefault(require("./FormFields/CustomDatePicker"));
26
117
  var _CustomDropdown = _interopRequireDefault(require("./FormFields/CustomDropdown"));
118
+ var _CustomFileInput = _interopRequireDefault(require("./FormFields/CustomFileInput"));
27
119
  var _CustomInput = _interopRequireDefault(require("./FormFields/CustomInput"));
28
120
  var _CustomJson = _interopRequireDefault(require("./FormFields/CustomJson"));
29
121
  var _CustomLabel = _interopRequireDefault(require("./FormFields/CustomLabel"));
122
+ var _CustomMobilePhone = _interopRequireDefault(require("./FormFields/CustomMobilePhone"));
30
123
  var _CustomMultiDropdown = _interopRequireDefault(require("./FormFields/CustomMultiDropdown"));
31
124
  var _CustomNumberInput = _interopRequireDefault(require("./FormFields/CustomNumberInput"));
32
125
  var _CustomRadioGroup = _interopRequireDefault(require("./FormFields/CustomRadioGroup"));
@@ -116,10 +209,14 @@ var GenericForm = function GenericForm(_ref) {
116
209
  fieldsStyle = _ref.fieldsStyle,
117
210
  actionsDivStyle = _ref.actionsDivStyle,
118
211
  actionsStyle = _ref.actionsStyle,
212
+ inlineStyle = _ref.inlineStyle,
119
213
  _ref$splitForm = _ref.splitForm,
120
214
  splitForm = _ref$splitForm === void 0 ? false : _ref$splitForm,
121
- splitField = _ref.splitField,
215
+ _ref$splitFields = _ref.splitFields,
216
+ splitFields = _ref$splitFields === void 0 ? [] : _ref$splitFields,
122
217
  splitGroup = _ref.splitGroup,
218
+ _ref$splitColumns = _ref.splitColumns,
219
+ splitColumns = _ref$splitColumns === void 0 ? 2 : _ref$splitColumns,
123
220
  _ref$disableDefaultCo = _ref.disableDefaultContainer,
124
221
  disableDefaultContainer = _ref$disableDefaultCo === void 0 ? false : _ref$disableDefaultCo,
125
222
  handleSubmit = _ref.handleSubmit,
@@ -155,7 +252,9 @@ var GenericForm = function GenericForm(_ref) {
155
252
  _ref$modalShow = _ref.modalShow,
156
253
  modalShow = _ref$modalShow === void 0 ? false : _ref$modalShow,
157
254
  _ref$modalSize = _ref.modalSize,
158
- modalSize = _ref$modalSize === void 0 ? 'lg' : _ref$modalSize;
255
+ modalSize = _ref$modalSize === void 0 ? 'lg' : _ref$modalSize,
256
+ _ref$disableFormTag = _ref.disableFormTag,
257
+ disableFormTag = _ref$disableFormTag === void 0 ? false : _ref$disableFormTag;
159
258
  var _useState = (0, _react.useState)([]),
160
259
  _useState2 = _slicedToArray(_useState, 2),
161
260
  sortedFields = _useState2[0],
@@ -224,7 +323,7 @@ var GenericForm = function GenericForm(_ref) {
224
323
  var nextErrors = _objectSpread({}, prevErrors);
225
324
  Object.keys(patch).forEach(function (name) {
226
325
  var field = fields.find(function (f) {
227
- return f.name === name;
326
+ return (f === null || f === void 0 ? void 0 : f.name) === name;
228
327
  });
229
328
  if (field) nextErrors[name] = validateField(field, patch[name], merged) || undefined;
230
329
  });
@@ -243,14 +342,14 @@ var GenericForm = function GenericForm(_ref) {
243
342
  var errs = [];
244
343
  if (field.required) {
245
344
  var empty = value === undefined || value === null || value === '' || Array.isArray(value) && value.length === 0;
246
- if (empty) errs.push(field.errorMessage || "".concat(typeof field.label === 'string' && field.label || field.displayName || field.name, " is required"));
345
+ if (empty) errs.push(field.errorMessage || "".concat(typeof field.label === 'string' && field.label || field.name, " is required"));
247
346
  }
248
347
  if (field.type === 'email' && value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) errs.push(field.errorMessage || 'Invalid email format');
249
348
  if (field.minLength && value && value.length < field.minLength) errs.push(field.errorMessage || "Minimum ".concat(field.minLength, " characters required"));
250
349
  if (field.maxLength && value && value.length > field.maxLength) errs.push(field.errorMessage || "Maximum ".concat(field.maxLength, " characters allowed"));
251
350
  if (field.min !== undefined && value !== '' && Number(value) < field.min) errs.push(field.errorMessage || "Minimum value is ".concat(field.min));
252
351
  if (field.max !== undefined && value !== '' && Number(value) > field.max) errs.push(field.errorMessage || "Maximum value is ".concat(field.max));
253
- if (typeof field.validate === 'function') {
352
+ if (!['date', 'datetime', 'year'].includes(field.type) && typeof field.validate === 'function') {
254
353
  var custom = field.validate(value, currentFormData || internalFormData);
255
354
  if (custom) errs.push(custom);
256
355
  }
@@ -454,9 +553,10 @@ var GenericForm = function GenericForm(_ref) {
454
553
  // Props shared by all interactive fields
455
554
  var cp = {
456
555
  name: field.name,
457
- displayName: field.label || field.displayName,
556
+ label: field.label,
458
557
  disabled: field.disabled,
459
- required: field.required
558
+ required: field.required,
559
+ placeholder: field.placeholder
460
560
  };
461
561
  var sp = {
462
562
  mainDivStyle: field.style,
@@ -500,7 +600,6 @@ var GenericForm = function GenericForm(_ref) {
500
600
  case 'number':
501
601
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomNumberInput["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
502
602
  value: value,
503
- placeholder: field.placeholder,
504
603
  min: field.min,
505
604
  max: field.max,
506
605
  step: field.step,
@@ -509,11 +608,17 @@ var GenericForm = function GenericForm(_ref) {
509
608
  case 'textArea':
510
609
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomTextArea["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
511
610
  value: value,
512
- placeholder: field.placeholder,
513
611
  rows: field.rows,
514
612
  height: field.height,
515
613
  onChange: fieldHandler(field)
516
614
  }), field.name);
615
+ case 'tel':
616
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomMobilePhone["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
617
+ value: value,
618
+ onChange: fieldHandler,
619
+ countryCodes: field.countryCodes,
620
+ defaultCountryCode: field.defaultCountryCode
621
+ }), field.name);
517
622
 
518
623
  // Date
519
624
  case 'date':
@@ -522,8 +627,8 @@ var GenericForm = function GenericForm(_ref) {
522
627
  case 'year':
523
628
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomDatePicker["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
524
629
  value: value,
525
- placeholder: field.placeholder,
526
630
  onChange: dateFieldHandler(field),
631
+ isValidDate: field.validate,
527
632
  dateFormat: field.type === 'time' ? false : field.type === 'year' ? 'YYYY' : 'DD/MM/YYYY',
528
633
  timeFormat: field.type === 'datetime' || field.type === 'time' ? 'HH:mm' : false
529
634
  }), field.name);
@@ -588,10 +693,14 @@ var GenericForm = function GenericForm(_ref) {
588
693
  case 'checkboxGroup':
589
694
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomCheckboxGroup["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
590
695
  checked: value !== null && value !== void 0 ? value : [],
696
+ onChange: fieldHandler(field),
697
+ onOptionChange: field.onOptionChange,
698
+ groupInputStyle: field.groupInputStyle,
591
699
  options: field.options,
592
700
  grouped: field.grouped,
593
- onChange: fieldHandler(field),
594
- onOptionChange: field.onOptionChange
701
+ collapsible: field.collapsible,
702
+ defaultCollapsed: field.defaultCollapsed,
703
+ enableParent: field.enableParent
595
704
  }), field.name);
596
705
  case 'radioGroup':
597
706
  case 'radioGroupInline':
@@ -629,11 +738,17 @@ var GenericForm = function GenericForm(_ref) {
629
738
  return validateJsonField(name, raw, isMulti, false, field);
630
739
  }
631
740
  }), field.name);
741
+ case 'file':
742
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomFileInput["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
743
+ value: value,
744
+ accept: field.accept,
745
+ onChange: fieldHandler(field)
746
+ }), field.name);
632
747
  default:
633
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomInput["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
748
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomInput["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({
749
+ type: field.type
750
+ }, cp), sp), fp), {}, {
634
751
  value: value,
635
- type: "text",
636
- placeholder: field.placeholder,
637
752
  onChange: fieldHandler(field),
638
753
  className: field.className
639
754
  }), field.name);
@@ -656,6 +771,7 @@ var GenericForm = function GenericForm(_ref) {
656
771
  return groupByInline(fieldList).map(function (group, idx) {
657
772
  return group.type === 'inline' ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
658
773
  className: "ufg-field-row",
774
+ style: inlineStyle,
659
775
  children: group.fields.map(function (_ref9) {
660
776
  var field = _ref9.field;
661
777
  return renderField(field);
@@ -672,63 +788,91 @@ var GenericForm = function GenericForm(_ref) {
672
788
  }), renderFields(fieldList)]
673
789
  }, g);
674
790
  };
675
- var splitByField = function splitByField(fieldList) {
676
- var idx = fieldList.findIndex(function (f) {
677
- return f.name === splitField;
791
+
792
+ // ── Replace splitByField + buildContent entirely ─────────────────────────────
793
+
794
+ var splitFieldsSet = (0, _react.useMemo)(function () {
795
+ return new Set(splitFields);
796
+ }, [splitFields]);
797
+
798
+ /**
799
+ * Returns the index of the last field (in `list`) whose name is in splitFields.
800
+ * Returns -1 when none match.
801
+ */
802
+ var lastSplitIdx = (0, _react.useCallback)(function (list) {
803
+ var idx = -1;
804
+ list.forEach(function (f, i) {
805
+ if (splitFieldsSet.has(f.name)) idx = i;
678
806
  });
679
- if (idx === -1) return {
680
- left: fieldList,
681
- right: []
682
- };
683
- return {
684
- left: fieldList.slice(0, idx + 1),
685
- right: fieldList.slice(idx + 1)
686
- };
687
- };
688
- var buildContent = function buildContent() {
807
+ return idx;
808
+ }, [splitFieldsSet]);
809
+ var buildContent = (0, _react.useCallback)(function () {
689
810
  var groups = Object.keys(groupedFields);
690
- if (splitForm && splitField && splitGroup) {
691
- var splitIdx = groups.findIndex(function (g) {
692
- return groupedFields[g].some(function (f) {
693
- return f.name === splitField;
694
- });
811
+ if (!splitForm || splitFields.length === 0) return groups.map(function (g) {
812
+ return renderGroupSection(g, groupedFields[g]);
813
+ });
814
+
815
+ // ── Group-level split ────────────────────────────────────────────────────
816
+ if (splitGroup) {
817
+ // Find the rightmost group that contains any splitField
818
+ var pivotGroupIdx = -1;
819
+ groups.forEach(function (g, gi) {
820
+ if (groupedFields[g].some(function (f) {
821
+ return splitFieldsSet.has(f.name);
822
+ })) pivotGroupIdx = gi;
695
823
  });
696
- if (splitIdx === -1) return groups.map(function (g) {
824
+ if (pivotGroupIdx === -1) return groups.map(function (g) {
697
825
  return renderGroupSection(g, groupedFields[g]);
698
826
  });
699
827
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
700
828
  className: "ufg-form-columns",
829
+ style: {
830
+ gridTemplateColumns: "repeat(".concat(splitColumns, ", 1fr)")
831
+ },
701
832
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
702
- children: groups.slice(0, splitIdx + 1).map(function (g) {
833
+ children: groups.slice(0, pivotGroupIdx + 1).map(function (g) {
703
834
  return renderGroupSection(g, groupedFields[g]);
704
835
  })
705
836
  }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
706
- children: groups.slice(splitIdx + 1).map(function (g) {
837
+ children: groups.slice(pivotGroupIdx + 1).map(function (g) {
707
838
  return renderGroupSection(g, groupedFields[g]);
708
839
  })
709
840
  })]
710
841
  });
711
842
  }
712
- if (splitForm && splitField) {
713
- return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
714
- className: "ufg-form-columns",
715
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
716
- children: groups.map(function (g) {
717
- return renderGroupSection(g, splitByField(groupedFields[g]).left);
718
- })
719
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
720
- children: groups.map(function (g) {
721
- var _splitByField = splitByField(groupedFields[g]),
722
- right = _splitByField.right;
723
- return right.length ? renderGroupSection(g, right) : null;
724
- })
725
- })]
726
- });
727
- }
728
- return groups.map(function (g) {
843
+
844
+ // ── Field-level split (cross-group aware) ────────────────────────────────
845
+ // Flatten preserving order, find pivot, then re-group each side.
846
+ var allFields = groups.flatMap(function (g) {
847
+ return groupedFields[g];
848
+ });
849
+ var pivot = lastSplitIdx(allFields);
850
+ if (pivot === -1) return groups.map(function (g) {
729
851
  return renderGroupSection(g, groupedFields[g]);
730
852
  });
731
- };
853
+ var regroup = function regroup(slice) {
854
+ var map = {};
855
+ slice.forEach(function (f) {
856
+ var g = f.group || 'ungrouped';
857
+ if (!map[g]) map[g] = [];
858
+ map[g].push(f);
859
+ });
860
+ return Object.keys(map).map(function (g) {
861
+ return renderGroupSection(g, map[g]);
862
+ });
863
+ };
864
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
865
+ className: "ufg-form-columns",
866
+ style: {
867
+ gridTemplateColumns: "repeat(".concat(splitColumns, ", 1fr)")
868
+ },
869
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
870
+ children: regroup(allFields.slice(0, pivot + 1))
871
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
872
+ children: regroup(allFields.slice(pivot + 1))
873
+ })]
874
+ });
875
+ }, [groupedFields, splitForm, splitFields, splitGroup, splitFieldsSet, lastSplitIdx, renderGroupSection]); // eslint-disable-line react-hooks/exhaustive-deps
732
876
 
733
877
  // #endregion
734
878
  // #region Actions bar
@@ -737,14 +881,11 @@ var GenericForm = function GenericForm(_ref) {
737
881
  className: "ufg-actions",
738
882
  style: actionsDivStyle,
739
883
  children: [customActions.map(function (action, idx) {
740
- return /*#__PURE__*/(0, _jsxRuntime.jsxs)("button", {
741
- type: "button",
742
- className: "ufg-button ".concat(action.className || 'ufg-button-secondary'),
743
- onClick: action.onClick,
744
- disabled: !!action.disabled,
745
- style: _objectSpread(_objectSpread({}, actionsStyle), action.style),
746
- children: [action.icon, action.label || action.name]
747
- }, idx);
884
+ return (0, _CustomAction["default"])(_objectSpread(_objectSpread({}, action), {}, {
885
+ name: "".concat(action.name, " ").concat(idx),
886
+ className: action.className || "ufg-button-secondary",
887
+ style: _objectSpread(_objectSpread({}, actionsStyle), action.style)
888
+ }));
748
889
  }), showSubmit && haveSaveButton && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
749
890
  type: "submit",
750
891
  className: "ufg-button ".concat(submitButton.className || 'ufg-button-primary'),
@@ -769,7 +910,14 @@ var GenericForm = function GenericForm(_ref) {
769
910
 
770
911
  // #endregion
771
912
  // #region Form JSX
772
- var formJSX = /*#__PURE__*/(0, _jsxRuntime.jsxs)("form", {
913
+ var formJSX = disableFormTag ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
914
+ className: "ufg-form",
915
+ style: formStyle || {
916
+ margin: 'auto'
917
+ },
918
+ onSubmit: onFormSubmit,
919
+ children: [buildContent(), actionsBar]
920
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsxs)("form", {
773
921
  className: "ufg-form",
774
922
  style: formStyle || {
775
923
  margin: 'auto'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@insticc/genericform",
3
- "version": "2.0.7",
3
+ "version": "2.1.0",
4
4
  "description": "A generic form with a comprehensive set of field types, validation, and external state management",
5
5
  "main": "build/index.js",
6
6
  "scripts": {