@digigov/form 0.6.9 → 0.6.10
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/CHANGELOG.md +8 -1
- package/Field/index.js +107 -34
- package/Field/types.js +5 -0
- package/Fieldset/FieldsetWithContext.js +63 -0
- package/Fieldset/index.js +4 -63
- package/Fieldset/types.js +5 -0
- package/FormBuilder.js +178 -0
- package/FormContext.js +14 -0
- package/Questions/Questions.js +105 -0
- package/Questions/QuestionsContext.js +20 -0
- package/Questions/Step/ReviewStep.js +11 -7
- package/Questions/Step/Step.js +93 -0
- package/Questions/Step/StepArrayReview.js +79 -0
- package/Questions/Step/StepContext.js +37 -0
- package/Questions/Step/StepDescription.js +2 -2
- package/Questions/Step/StepForm.js +10 -6
- package/Questions/Step/StepTitle.js +2 -2
- package/Questions/Step/getAddMoreFields.js +31 -0
- package/Questions/Step/index.js +81 -200
- package/Questions/Step/types.js +5 -0
- package/Questions/getNextStep.js +30 -0
- package/Questions/index.js +4 -150
- package/Questions/types.js +5 -0
- package/es/Field/index.js +102 -30
- package/es/Field/types.js +1 -0
- package/es/Fieldset/FieldsetWithContext.js +41 -0
- package/es/Fieldset/index.js +3 -51
- package/es/Fieldset/types.js +1 -0
- package/es/FormBuilder.js +148 -0
- package/es/FormContext.js +5 -0
- package/es/Questions/Questions.js +84 -0
- package/es/Questions/QuestionsContext.js +11 -0
- package/es/Questions/Step/ReviewStep.js +5 -3
- package/es/Questions/Step/Step.js +69 -0
- package/es/Questions/Step/StepArrayReview.js +50 -0
- package/es/Questions/Step/StepContext.js +27 -0
- package/es/Questions/Step/StepDescription.js +1 -1
- package/es/Questions/Step/StepForm.js +4 -2
- package/es/Questions/Step/StepTitle.js +1 -1
- package/es/Questions/Step/getAddMoreFields.js +22 -0
- package/es/Questions/Step/index.js +13 -165
- package/es/Questions/Step/types.js +1 -0
- package/es/Questions/getNextStep.js +23 -0
- package/es/Questions/index.js +2 -119
- package/es/Questions/index.spec.js +2 -1
- package/es/Questions/types.js +1 -0
- package/es/index.js +2 -153
- package/es/inputs/index.js +5 -4
- package/es/internal.js +29 -0
- package/es/types.js +1 -0
- package/esm/Field/index.js +102 -30
- package/esm/Field/types.js +1 -0
- package/esm/Fieldset/FieldsetWithContext.js +41 -0
- package/esm/Fieldset/index.js +3 -51
- package/esm/Fieldset/types.js +1 -0
- package/esm/FormBuilder.js +148 -0
- package/esm/FormContext.js +5 -0
- package/esm/Questions/Questions.js +84 -0
- package/esm/Questions/QuestionsContext.js +11 -0
- package/esm/Questions/Step/ReviewStep.js +5 -3
- package/esm/Questions/Step/Step.js +69 -0
- package/esm/Questions/Step/StepArrayReview.js +50 -0
- package/esm/Questions/Step/StepContext.js +27 -0
- package/esm/Questions/Step/StepDescription.js +1 -1
- package/esm/Questions/Step/StepForm.js +4 -2
- package/esm/Questions/Step/StepTitle.js +1 -1
- package/esm/Questions/Step/getAddMoreFields.js +22 -0
- package/esm/Questions/Step/index.js +13 -165
- package/esm/Questions/Step/types.js +1 -0
- package/esm/Questions/getNextStep.js +23 -0
- package/esm/Questions/index.js +2 -119
- package/esm/Questions/index.spec.js +2 -1
- package/esm/Questions/types.js +1 -0
- package/esm/index.js +3 -154
- package/esm/inputs/index.js +5 -4
- package/esm/internal.js +29 -0
- package/esm/types.js +1 -0
- package/index.js +8 -191
- package/inputs/index.js +13 -5
- package/internal.js +335 -0
- package/libs/form/src/Field/index.d.ts +2 -61
- package/libs/form/src/Field/types.d.ts +75 -0
- package/libs/form/src/Fieldset/FieldsetWithContext.d.ts +4 -0
- package/libs/form/src/Fieldset/index.d.ts +2 -12
- package/libs/form/src/Fieldset/types.d.ts +6 -0
- package/libs/form/src/FormBuilder.d.ts +5 -0
- package/libs/form/src/FormContext.d.ts +3 -0
- package/libs/form/src/Questions/Questions.d.ts +8 -0
- package/libs/form/src/Questions/QuestionsContext.d.ts +3 -0
- package/libs/form/src/Questions/Step/ReviewStep.d.ts +1 -0
- package/libs/form/src/Questions/Step/Step.d.ts +9 -0
- package/libs/form/src/Questions/Step/StepArrayReview.d.ts +4 -0
- package/libs/form/src/Questions/Step/StepContext.d.ts +4 -0
- package/libs/form/src/Questions/Step/StepForm.d.ts +2 -1
- package/libs/form/src/Questions/Step/getAddMoreFields.d.ts +3 -0
- package/libs/form/src/Questions/Step/index.d.ts +12 -47
- package/libs/form/src/Questions/Step/types.d.ts +31 -0
- package/libs/form/src/Questions/getNextStep.d.ts +2 -0
- package/libs/form/src/Questions/index.d.ts +1 -28
- package/libs/form/src/Questions/types.d.ts +19 -0
- package/libs/form/src/index.d.ts +2 -73
- package/libs/form/src/inputs/Checkboxes/index.d.ts +1 -1
- package/libs/form/src/inputs/DateInput/index.d.ts +1 -1
- package/libs/form/src/inputs/FileInput/index.d.ts +1 -1
- package/libs/form/src/inputs/Input/index.d.ts +1 -1
- package/libs/form/src/inputs/Label/index.d.ts +2 -1
- package/libs/form/src/inputs/Radio/index.d.ts +1 -1
- package/libs/form/src/inputs/Select/index.d.ts +1 -1
- package/libs/form/src/inputs/index.d.ts +4 -3
- package/libs/form/src/internal.d.ts +29 -0
- package/libs/form/src/types.d.ts +69 -0
- package/libs/form/src/validators.d.ts +1 -1
- package/libs/ui/src/core/Button/index.d.ts +1 -1
- package/libs/ui/src/core/NavList/NavList.d.ts +1 -14
- package/libs/ui/src/core/NavList/NavListBase.d.ts +4 -0
- package/libs/ui/src/core/NavList/NavListSubMenu.d.ts +1 -1
- package/libs/ui/src/core/NavList/types.d.ts +13 -0
- package/libs/ui/src/core/Table/TableFloatingScroll.d.ts +3 -0
- package/libs/ui/src/core/Table/index.d.ts +1 -0
- package/libs/ui/src/layouts/Grid/index.d.ts +1 -0
- package/libs-ui/react-core/src/Button/index.d.ts +1 -1
- package/libs-ui/react-core/src/CheckboxItem/index.d.ts +1 -1
- package/libs-ui/react-core/src/DateInputItem/index.d.ts +1 -1
- package/libs-ui/react-core/src/Fieldset/index.d.ts +1 -1
- package/libs-ui/react-core/src/FileUpload/index.d.ts +1 -1
- package/libs-ui/react-core/src/RadioItem/index.d.ts +1 -1
- package/libs-ui/react-core/src/Select/index.d.ts +1 -1
- package/libs-ui/react-core/src/SvgIcon/index.d.ts +1 -1
- package/libs-ui/react-core/src/Table/index.d.ts +1 -1
- package/libs-ui/react-core/src/TextArea/index.d.ts +1 -1
- package/libs-ui/react-core/src/TextInput/index.d.ts +1 -1
- package/package.json +2 -2
- package/types.js +5 -0
- package/Field/ConditionalField.js +0 -92
- package/es/Field/ConditionalField.js +0 -77
- package/esm/Field/ConditionalField.js +0 -77
- package/libs/form/src/Field/ConditionalField.d.ts +0 -17
package/es/Field/index.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import _extends from "@babel/runtime/helpers/extends";
|
|
2
1
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
2
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
3
3
|
var _excluded = ["required", "name", "component", "wrapper", "control", "type", "controlled", "enabled", "editable", "defaultValue", "label", "extra", "layout", "error", "register"],
|
|
4
4
|
_excluded2 = ["name", "children"];
|
|
5
5
|
import React, { useContext, useMemo } from 'react';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { FormContext } from '@digigov/form';
|
|
6
|
+
import { Controller, useWatch } from 'react-hook-form';
|
|
7
|
+
import { FormContext } from '@digigov/form/FormContext';
|
|
9
8
|
import CoreField from '@digigov/react-core/Field';
|
|
10
9
|
import CoreLabel from '@digigov/react-core/Label';
|
|
11
10
|
import CoreFieldset from '@digigov/react-core/Fieldset';
|
|
@@ -20,6 +19,79 @@ import FileInput from '@digigov/form/inputs/FileInput';
|
|
|
20
19
|
import DateInput from '@digigov/form/inputs/DateInput';
|
|
21
20
|
import Label from '@digigov/form/inputs/Label';
|
|
22
21
|
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
22
|
+
|
|
23
|
+
function evaluateFieldWithConditions(field, variables) {
|
|
24
|
+
var newField = _extends({}, field);
|
|
25
|
+
|
|
26
|
+
if (variables) {
|
|
27
|
+
for (var key in variables) {
|
|
28
|
+
if (field.condition[key] && field.condition[key].is === variables[key]) {
|
|
29
|
+
var then = field.condition[key].then || {};
|
|
30
|
+
|
|
31
|
+
for (var attr in then) {
|
|
32
|
+
newField[attr] = then[attr];
|
|
33
|
+
}
|
|
34
|
+
} else if (field.condition[key]) {
|
|
35
|
+
var otherwise = field.condition[key]["else"] || {};
|
|
36
|
+
|
|
37
|
+
for (var _attr in otherwise) {
|
|
38
|
+
newField[_attr] = otherwise[_attr];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return newField;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
var ChildFieldMemo = /*#__PURE__*/React.memo(function ChildField(_ref) {
|
|
48
|
+
var dependencies = _ref.dependencies,
|
|
49
|
+
field = _ref.field,
|
|
50
|
+
control = _ref.control,
|
|
51
|
+
register = _ref.register,
|
|
52
|
+
error = _ref.error;
|
|
53
|
+
var newField = evaluateFieldWithConditions(field, dependencies);
|
|
54
|
+
return /*#__PURE__*/React.createElement(FieldBase, _extends({}, newField, {
|
|
55
|
+
name: newField.key,
|
|
56
|
+
control: control,
|
|
57
|
+
register: register,
|
|
58
|
+
error: error
|
|
59
|
+
}));
|
|
60
|
+
}, function (prev, next) {
|
|
61
|
+
if (!prev || !prev.dependencies) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (prev.error !== next.error) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (var dep in next.dependencies) {
|
|
70
|
+
if (next.dependencies[dep] !== prev.dependencies[dep]) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return true;
|
|
76
|
+
});
|
|
77
|
+
export var ConditionalField = function ConditionalField(_ref2) {
|
|
78
|
+
var control = _ref2.control,
|
|
79
|
+
field = _ref2.field,
|
|
80
|
+
register = _ref2.register,
|
|
81
|
+
error = _ref2.error;
|
|
82
|
+
var dependencyKeys = Object.keys(field.condition);
|
|
83
|
+
var variables = useWatch({
|
|
84
|
+
name: dependencyKeys,
|
|
85
|
+
control: control
|
|
86
|
+
});
|
|
87
|
+
return /*#__PURE__*/React.createElement(ChildFieldMemo, {
|
|
88
|
+
dependencies: variables,
|
|
89
|
+
field: field,
|
|
90
|
+
control: control,
|
|
91
|
+
register: register,
|
|
92
|
+
error: error
|
|
93
|
+
});
|
|
94
|
+
};
|
|
23
95
|
var FIELD_COMPONENTS = {
|
|
24
96
|
text: {
|
|
25
97
|
component: Input
|
|
@@ -54,12 +126,12 @@ var ALTERNATIVE_COMPONENTS = {
|
|
|
54
126
|
}
|
|
55
127
|
};
|
|
56
128
|
|
|
57
|
-
var FieldContainer = function FieldContainer(
|
|
58
|
-
var name =
|
|
59
|
-
wrapper =
|
|
60
|
-
label =
|
|
61
|
-
children =
|
|
62
|
-
error =
|
|
129
|
+
var FieldContainer = function FieldContainer(_ref3) {
|
|
130
|
+
var name = _ref3.name,
|
|
131
|
+
wrapper = _ref3.wrapper,
|
|
132
|
+
label = _ref3.label,
|
|
133
|
+
children = _ref3.children,
|
|
134
|
+
error = _ref3.error;
|
|
63
135
|
|
|
64
136
|
var _useTranslation = useTranslation(),
|
|
65
137
|
t = _useTranslation.t;
|
|
@@ -118,10 +190,10 @@ export var FieldBase = function FieldBase(props) {
|
|
|
118
190
|
}, /*#__PURE__*/React.createElement(Controller, {
|
|
119
191
|
control: control,
|
|
120
192
|
name: name,
|
|
121
|
-
render: function render(
|
|
122
|
-
var onChange =
|
|
123
|
-
onBlur =
|
|
124
|
-
value =
|
|
193
|
+
render: function render(_ref4) {
|
|
194
|
+
var onChange = _ref4.onChange,
|
|
195
|
+
onBlur = _ref4.onBlur,
|
|
196
|
+
value = _ref4.value;
|
|
125
197
|
return /*#__PURE__*/React.createElement(Component, _extends({
|
|
126
198
|
name: name,
|
|
127
199
|
onChange: onChange,
|
|
@@ -199,41 +271,41 @@ function calculateField(children, field, componentRegistry) {
|
|
|
199
271
|
|
|
200
272
|
var calculatedField = _extends({}, field);
|
|
201
273
|
|
|
202
|
-
var
|
|
274
|
+
var fieldComponentRegistry = _extends({}, FIELD_COMPONENTS, componentRegistry);
|
|
203
275
|
|
|
204
276
|
if (children) {
|
|
205
277
|
calculatedField.component = children;
|
|
206
278
|
} else if (typeof field.component === 'function') {// leave as is
|
|
207
279
|
} else if (!field.component && !field.type) {
|
|
208
|
-
var
|
|
280
|
+
var _fieldComponentRegist;
|
|
209
281
|
|
|
210
|
-
calculatedField.component =
|
|
211
|
-
calculatedField.controlled = ((
|
|
282
|
+
calculatedField.component = fieldComponentRegistry.string.component;
|
|
283
|
+
calculatedField.controlled = ((_fieldComponentRegist = fieldComponentRegistry.string) === null || _fieldComponentRegist === void 0 ? void 0 : _fieldComponentRegist.controlled) || false;
|
|
212
284
|
} else if (typeof (field === null || field === void 0 ? void 0 : (_field$extra = field.extra) === null || _field$extra === void 0 ? void 0 : _field$extra.component) === 'string' && ALTERNATIVE_COMPONENTS[field.extra.component]) {
|
|
213
285
|
var _ALTERNATIVE_COMPONEN;
|
|
214
286
|
|
|
215
287
|
calculatedField.controlled = ((_ALTERNATIVE_COMPONEN = ALTERNATIVE_COMPONENTS[field.extra.component]) === null || _ALTERNATIVE_COMPONEN === void 0 ? void 0 : _ALTERNATIVE_COMPONEN.controlled) || false;
|
|
216
288
|
calculatedField.component = ALTERNATIVE_COMPONENTS[field.extra.component].component;
|
|
217
|
-
} else if (!field.component && field.type &&
|
|
218
|
-
var
|
|
289
|
+
} else if (!field.component && field.type && fieldComponentRegistry[field.type]) {
|
|
290
|
+
var _fieldComponentRegist2;
|
|
219
291
|
|
|
220
|
-
calculatedField.component =
|
|
221
|
-
calculatedField.wrapper =
|
|
222
|
-
calculatedField.controlled = ((
|
|
292
|
+
calculatedField.component = fieldComponentRegistry[field.type].component;
|
|
293
|
+
calculatedField.wrapper = fieldComponentRegistry[field.type].wrapper;
|
|
294
|
+
calculatedField.controlled = ((_fieldComponentRegist2 = fieldComponentRegistry[field.type]) === null || _fieldComponentRegist2 === void 0 ? void 0 : _fieldComponentRegist2.controlled) || false;
|
|
223
295
|
} else {
|
|
224
|
-
var
|
|
296
|
+
var _fieldComponentRegist3;
|
|
225
297
|
|
|
226
|
-
calculatedField.component =
|
|
227
|
-
calculatedField.controlled = ((
|
|
298
|
+
calculatedField.component = fieldComponentRegistry.string.component;
|
|
299
|
+
calculatedField.controlled = ((_fieldComponentRegist3 = fieldComponentRegistry.string) === null || _fieldComponentRegist3 === void 0 ? void 0 : _fieldComponentRegist3.controlled) || false;
|
|
228
300
|
}
|
|
229
301
|
|
|
230
302
|
return calculatedField;
|
|
231
303
|
}
|
|
232
304
|
|
|
233
|
-
export var Field = function Field(
|
|
234
|
-
var name =
|
|
235
|
-
children =
|
|
236
|
-
customField = _objectWithoutProperties(
|
|
305
|
+
export var Field = function Field(_ref5) {
|
|
306
|
+
var name = _ref5.name,
|
|
307
|
+
children = _ref5.children,
|
|
308
|
+
customField = _objectWithoutProperties(_ref5, _excluded2);
|
|
237
309
|
|
|
238
310
|
var _useField = useField(name, customField !== null && customField !== void 0 && customField.type ? customField : null),
|
|
239
311
|
field = _useField.field,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import { FormContext } from '@digigov/form/FormContext';
|
|
3
|
+
import Field from '@digigov/form/Field';
|
|
4
|
+
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
5
|
+
import { Fieldset, FieldsetLabel, FieldsetCaption, FieldsetBody } from '@digigov/form/Fieldset/';
|
|
6
|
+
export var FieldsetWithContext = function FieldsetWithContext(_ref) {
|
|
7
|
+
var name = _ref.name;
|
|
8
|
+
|
|
9
|
+
var _useFieldset = useFieldset(name),
|
|
10
|
+
label = _useFieldset.label,
|
|
11
|
+
body = _useFieldset.body,
|
|
12
|
+
fields = _useFieldset.fields;
|
|
13
|
+
|
|
14
|
+
var _useTranslation = useTranslation(),
|
|
15
|
+
t = _useTranslation.t;
|
|
16
|
+
|
|
17
|
+
var _ref2 = /*#__PURE__*/React.createElement(FieldsetBody, null, body);
|
|
18
|
+
|
|
19
|
+
return useMemo(function () {
|
|
20
|
+
return /*#__PURE__*/React.createElement(Fieldset, null, (label === null || label === void 0 ? void 0 : label.primary) && /*#__PURE__*/React.createElement(FieldsetLabel, null, t(label.primary)), (label === null || label === void 0 ? void 0 : label.secondary) && /*#__PURE__*/React.createElement(FieldsetCaption, null, t(label.secondary)), body && _ref2, fields.map(function (field) {
|
|
21
|
+
return /*#__PURE__*/React.createElement(Field, {
|
|
22
|
+
key: field,
|
|
23
|
+
name: field
|
|
24
|
+
});
|
|
25
|
+
}));
|
|
26
|
+
}, [label === null || label === void 0 ? void 0 : label.primary, label === null || label === void 0 ? void 0 : label.secondary, body, fields]);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
var useFieldset = function useFieldset(name) {
|
|
30
|
+
var _useContext = useContext(FormContext),
|
|
31
|
+
fieldsetsMap = _useContext.fieldsetsMap;
|
|
32
|
+
|
|
33
|
+
if (fieldsetsMap && fieldsetsMap[name]) {
|
|
34
|
+
return useMemo(function () {
|
|
35
|
+
return fieldsetsMap[name];
|
|
36
|
+
}, [name]);
|
|
37
|
+
} else {
|
|
38
|
+
if (!fieldsetsMap) throw new Error("Fieldsets is not defined");
|
|
39
|
+
throw new Error("No fieldset with name ".concat(name, " found"));
|
|
40
|
+
}
|
|
41
|
+
};
|
package/es/Fieldset/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import React
|
|
1
|
+
import React from 'react';
|
|
2
2
|
import Grid from '@digigov/ui/layouts/Grid';
|
|
3
3
|
import CoreFieldset from '@digigov/react-core/Fieldset';
|
|
4
4
|
import FieldsetLegend from '@digigov/react-core/FieldsetLegend';
|
|
5
5
|
import Hint from '@digigov/react-core/Hint';
|
|
6
|
-
import { FormContext } from '@digigov/form';
|
|
7
|
-
import Field from '@digigov/form/Field';
|
|
8
6
|
import NormalText from '@digigov/ui/typography/NormalText';
|
|
9
|
-
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
10
7
|
export var FieldsetLabel = function FieldsetLabel(_ref) {
|
|
11
8
|
var children = _ref.children;
|
|
12
9
|
return /*#__PURE__*/React.createElement(FieldsetLegend, {
|
|
@@ -25,53 +22,8 @@ export var FieldsetBody = function FieldsetBody(_ref3) {
|
|
|
25
22
|
xs: 12
|
|
26
23
|
}, /*#__PURE__*/React.createElement(NormalText, null, children));
|
|
27
24
|
};
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
var _useContext = useContext(FormContext),
|
|
31
|
-
fieldsetsMap = _useContext.fieldsetsMap;
|
|
32
|
-
|
|
33
|
-
if (fieldsetsMap && fieldsetsMap[name]) {
|
|
34
|
-
return useMemo(function () {
|
|
35
|
-
return fieldsetsMap[name];
|
|
36
|
-
}, [name]);
|
|
37
|
-
} else {
|
|
38
|
-
if (!fieldsetsMap) throw new Error("Fieldsets is not defined");
|
|
39
|
-
throw new Error("No fieldset with name ".concat(name, " found"));
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export var FieldsetWithContext = function FieldsetWithContext(_ref4) {
|
|
44
|
-
var name = _ref4.name;
|
|
45
|
-
|
|
46
|
-
var _useFieldset = useFieldset(name),
|
|
47
|
-
label = _useFieldset.label,
|
|
48
|
-
body = _useFieldset.body,
|
|
49
|
-
fields = _useFieldset.fields;
|
|
50
|
-
|
|
51
|
-
var _useTranslation = useTranslation(),
|
|
52
|
-
t = _useTranslation.t;
|
|
53
|
-
|
|
54
|
-
var _ref5 = /*#__PURE__*/React.createElement(FieldsetBody, null, body);
|
|
55
|
-
|
|
56
|
-
return useMemo(function () {
|
|
57
|
-
return /*#__PURE__*/React.createElement(Fieldset, null, (label === null || label === void 0 ? void 0 : label.primary) && /*#__PURE__*/React.createElement(FieldsetLabel, null, t(label.primary)), (label === null || label === void 0 ? void 0 : label.secondary) && /*#__PURE__*/React.createElement(FieldsetCaption, null, t(label.secondary)), body && _ref5, fields.map(function (field) {
|
|
58
|
-
return /*#__PURE__*/React.createElement(Field, {
|
|
59
|
-
key: field,
|
|
60
|
-
name: field
|
|
61
|
-
});
|
|
62
|
-
}));
|
|
63
|
-
}, [label === null || label === void 0 ? void 0 : label.primary, label === null || label === void 0 ? void 0 : label.secondary, body, fields]);
|
|
64
|
-
};
|
|
65
|
-
export var Fieldset = function Fieldset(_ref6) {
|
|
66
|
-
var name = _ref6.name,
|
|
67
|
-
children = _ref6.children;
|
|
68
|
-
|
|
69
|
-
if (name && !children) {
|
|
70
|
-
return /*#__PURE__*/React.createElement(FieldsetWithContext, {
|
|
71
|
-
name: name
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
25
|
+
export var Fieldset = function Fieldset(_ref4) {
|
|
26
|
+
var children = _ref4.children;
|
|
75
27
|
return /*#__PURE__*/React.createElement(CoreFieldset, null, children);
|
|
76
28
|
};
|
|
77
29
|
export default Fieldset;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
3
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
4
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
5
|
+
var _excluded = ["onSubmit", "children", "registerField", "fieldsMap", "fieldsetsMap", "resolver", "mode", "initial", "reValidateMode", "shouldFocusError", "criteriaMode", "componentRegistry"],
|
|
6
|
+
_excluded2 = ["fields", "fieldsets", "initial", "onSubmit", "children", "reValidateMode", "mode", "shouldFocusError", "criteriaMode", "auto", "validatorRegistry", "componentRegistry"];
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
import React, { useCallback, useRef } from 'react';
|
|
10
|
+
import { useForm } from 'react-hook-form';
|
|
11
|
+
import { yupResolver } from '@digigov/form/utils';
|
|
12
|
+
import Grid from '@digigov/ui/layouts/Grid';
|
|
13
|
+
import { useValidationSchema } from '@digigov/form/validators';
|
|
14
|
+
import Fieldset from '@digigov/form/Fieldset';
|
|
15
|
+
import Field from '@digigov/form/Field';
|
|
16
|
+
import { FormContext } from '@digigov/form/FormContext';
|
|
17
|
+
export var FormBase = /*#__PURE__*/React.forwardRef(function FormBase(_ref, ref) {
|
|
18
|
+
var onSubmit = _ref.onSubmit,
|
|
19
|
+
children = _ref.children,
|
|
20
|
+
registerField = _ref.registerField,
|
|
21
|
+
fieldsMap = _ref.fieldsMap,
|
|
22
|
+
fieldsetsMap = _ref.fieldsetsMap,
|
|
23
|
+
resolver = _ref.resolver,
|
|
24
|
+
mode = _ref.mode,
|
|
25
|
+
initial = _ref.initial,
|
|
26
|
+
reValidateMode = _ref.reValidateMode,
|
|
27
|
+
shouldFocusError = _ref.shouldFocusError,
|
|
28
|
+
criteriaMode = _ref.criteriaMode,
|
|
29
|
+
componentRegistry = _ref.componentRegistry,
|
|
30
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
31
|
+
|
|
32
|
+
var form = useForm({
|
|
33
|
+
resolver: resolver,
|
|
34
|
+
mode: mode,
|
|
35
|
+
defaultValues: initial,
|
|
36
|
+
reValidateMode: reValidateMode,
|
|
37
|
+
shouldFocusError: shouldFocusError,
|
|
38
|
+
criteriaMode: criteriaMode
|
|
39
|
+
});
|
|
40
|
+
var handleSubmit = useCallback(function (data) {
|
|
41
|
+
var errors = onSubmit && onSubmit(data);
|
|
42
|
+
|
|
43
|
+
if (errors) {
|
|
44
|
+
for (var key in errors) {
|
|
45
|
+
form.setError(key, errors[key]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, [onSubmit]);
|
|
49
|
+
var ctx = {
|
|
50
|
+
fieldsMap: fieldsMap,
|
|
51
|
+
fieldsetsMap: fieldsetsMap,
|
|
52
|
+
control: form.control,
|
|
53
|
+
register: form.register,
|
|
54
|
+
registerField: registerField,
|
|
55
|
+
errors: form.errors,
|
|
56
|
+
reset: form.reset,
|
|
57
|
+
componentRegistry: componentRegistry
|
|
58
|
+
};
|
|
59
|
+
return /*#__PURE__*/React.createElement(FormContext.Provider, {
|
|
60
|
+
value: ctx
|
|
61
|
+
}, /*#__PURE__*/React.createElement("form", _extends({
|
|
62
|
+
onSubmit: form.handleSubmit(handleSubmit),
|
|
63
|
+
ref: ref
|
|
64
|
+
}, props), /*#__PURE__*/React.createElement(Grid, {
|
|
65
|
+
container: true
|
|
66
|
+
}, /*#__PURE__*/React.createElement(Grid, {
|
|
67
|
+
item: true,
|
|
68
|
+
xs: 12
|
|
69
|
+
}, children))));
|
|
70
|
+
});
|
|
71
|
+
export var FormBuilder = /*#__PURE__*/React.forwardRef(function FormBuilder(_ref2, ref) {
|
|
72
|
+
var _ref2$fields = _ref2.fields,
|
|
73
|
+
fields = _ref2$fields === void 0 ? [] : _ref2$fields,
|
|
74
|
+
fieldsets = _ref2.fieldsets,
|
|
75
|
+
_ref2$initial = _ref2.initial,
|
|
76
|
+
initial = _ref2$initial === void 0 ? {} : _ref2$initial,
|
|
77
|
+
onSubmit = _ref2.onSubmit,
|
|
78
|
+
children = _ref2.children,
|
|
79
|
+
_ref2$reValidateMode = _ref2.reValidateMode,
|
|
80
|
+
reValidateMode = _ref2$reValidateMode === void 0 ? 'onSubmit' : _ref2$reValidateMode,
|
|
81
|
+
_ref2$mode = _ref2.mode,
|
|
82
|
+
mode = _ref2$mode === void 0 ? 'onSubmit' : _ref2$mode,
|
|
83
|
+
_ref2$shouldFocusErro = _ref2.shouldFocusError,
|
|
84
|
+
shouldFocusError = _ref2$shouldFocusErro === void 0 ? true : _ref2$shouldFocusErro,
|
|
85
|
+
_ref2$criteriaMode = _ref2.criteriaMode,
|
|
86
|
+
criteriaMode = _ref2$criteriaMode === void 0 ? 'firstError' : _ref2$criteriaMode,
|
|
87
|
+
_ref2$auto = _ref2.auto,
|
|
88
|
+
auto = _ref2$auto === void 0 ? false : _ref2$auto,
|
|
89
|
+
validatorRegistry = _ref2.validatorRegistry,
|
|
90
|
+
componentRegistry = _ref2.componentRegistry,
|
|
91
|
+
props = _objectWithoutProperties(_ref2, _excluded2);
|
|
92
|
+
|
|
93
|
+
var fieldsState = useRef(fields);
|
|
94
|
+
var setFieldsState = useCallback(function (newFields) {
|
|
95
|
+
fieldsState.current = newFields;
|
|
96
|
+
}, []);
|
|
97
|
+
var schema = useValidationSchema(fieldsState, validatorRegistry);
|
|
98
|
+
var registerField = useCallback(function (field) {
|
|
99
|
+
setFieldsState([].concat(_toConsumableArray(fieldsState.current), [field]));
|
|
100
|
+
}, []);
|
|
101
|
+
var resolver;
|
|
102
|
+
var fieldsMap;
|
|
103
|
+
var fieldsetsMap;
|
|
104
|
+
var fieldChildren;
|
|
105
|
+
|
|
106
|
+
if (schema) {
|
|
107
|
+
resolver = yupResolver(schema);
|
|
108
|
+
fieldsMap = fields ? fields.reduce(function (map, field) {
|
|
109
|
+
return _extends({}, map, _defineProperty({}, field.key, field));
|
|
110
|
+
}, {}) : {};
|
|
111
|
+
fieldsetsMap = fieldsets && fieldsets.reduce(function (map, fieldset) {
|
|
112
|
+
return _extends({}, map, _defineProperty({}, fieldset.key, fieldset));
|
|
113
|
+
}, {});
|
|
114
|
+
|
|
115
|
+
if (auto) {
|
|
116
|
+
if (fieldsets) {
|
|
117
|
+
fieldChildren = fieldsets.map(function (fieldset) {
|
|
118
|
+
return /*#__PURE__*/React.createElement(Fieldset, {
|
|
119
|
+
key: fieldset.key
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
} else if (fields) {
|
|
123
|
+
fieldChildren = fields.map(function (field) {
|
|
124
|
+
return /*#__PURE__*/React.createElement(Field, {
|
|
125
|
+
key: field.key,
|
|
126
|
+
name: field.key
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return /*#__PURE__*/React.createElement(FormBase, _extends({
|
|
134
|
+
resolver: resolver,
|
|
135
|
+
fieldsetsMap: fieldsetsMap,
|
|
136
|
+
fieldsMap: fieldsMap,
|
|
137
|
+
registerField: registerField,
|
|
138
|
+
initial: initial,
|
|
139
|
+
reValidateMode: reValidateMode,
|
|
140
|
+
mode: mode,
|
|
141
|
+
shouldFocusError: shouldFocusError,
|
|
142
|
+
criteriaMode: criteriaMode,
|
|
143
|
+
onSubmit: onSubmit,
|
|
144
|
+
componentRegistry: componentRegistry,
|
|
145
|
+
ref: ref
|
|
146
|
+
}, props), fieldChildren, children);
|
|
147
|
+
});
|
|
148
|
+
export default FormBuilder;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import React, { useState, useEffect } from 'react';
|
|
3
|
+
import { QuestionsContext } from '@digigov/form/Questions/QuestionsContext';
|
|
4
|
+
import { getNextStep } from '@digigov/form/Questions/getNextStep';
|
|
5
|
+
var isBrowser = typeof window !== 'undefined';
|
|
6
|
+
/**
|
|
7
|
+
* The Question component accepts question data as props
|
|
8
|
+
* uses composable components to provide a wholesome UX
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export var Questions = function Questions(_ref) {
|
|
13
|
+
var name = _ref.name,
|
|
14
|
+
steps = _ref.steps,
|
|
15
|
+
_ref$initialData = _ref.initialData,
|
|
16
|
+
initialData = _ref$initialData === void 0 ? {} : _ref$initialData,
|
|
17
|
+
onChange = _ref.onChange,
|
|
18
|
+
onSubmit = _ref.onSubmit,
|
|
19
|
+
onActiveStep = _ref.onActiveStep,
|
|
20
|
+
forceStepName = _ref.forceStepName,
|
|
21
|
+
_ref$localDraft = _ref.localDraft,
|
|
22
|
+
localDraft = _ref$localDraft === void 0 ? false : _ref$localDraft,
|
|
23
|
+
children = _ref.children;
|
|
24
|
+
|
|
25
|
+
var _useState = useState({
|
|
26
|
+
name: ''
|
|
27
|
+
}),
|
|
28
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
29
|
+
currentStep = _useState2[0],
|
|
30
|
+
setCurrentStep = _useState2[1];
|
|
31
|
+
|
|
32
|
+
useEffect(function () {
|
|
33
|
+
if (!forceStepName) {
|
|
34
|
+
setCurrentStep(steps[0]);
|
|
35
|
+
}
|
|
36
|
+
}, [forceStepName, steps]);
|
|
37
|
+
useEffect(function () {
|
|
38
|
+
if (forceStepName !== currentStep.name) {
|
|
39
|
+
var forceStep = steps.find(function (_ref2) {
|
|
40
|
+
var name = _ref2.name;
|
|
41
|
+
return name === forceStepName;
|
|
42
|
+
});
|
|
43
|
+
forceStep && setCurrentStep(forceStep);
|
|
44
|
+
}
|
|
45
|
+
}, [forceStepName]);
|
|
46
|
+
var localData = isBrowser && localDraft && window.localStorage.getItem("questions-".concat(name));
|
|
47
|
+
|
|
48
|
+
var _useState3 = useState(localData && JSON.parse(localData) || initialData),
|
|
49
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
50
|
+
data = _useState4[0],
|
|
51
|
+
setData = _useState4[1];
|
|
52
|
+
|
|
53
|
+
var submitStep = function submitStep(stepName, stepData) {
|
|
54
|
+
data[stepName] = stepData;
|
|
55
|
+
|
|
56
|
+
if (localDraft) {
|
|
57
|
+
isBrowser && window.localStorage.setItem("questions-".concat(name), JSON.stringify(data));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (onChange) {
|
|
61
|
+
onChange && onChange(data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
setData(data);
|
|
65
|
+
var nextStep = getNextStep(currentStep, steps, data);
|
|
66
|
+
|
|
67
|
+
if (nextStep) {
|
|
68
|
+
onActiveStep && onActiveStep(nextStep);
|
|
69
|
+
setCurrentStep(nextStep);
|
|
70
|
+
} else {
|
|
71
|
+
onSubmit(data);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return /*#__PURE__*/React.createElement(QuestionsContext.Provider, {
|
|
76
|
+
value: {
|
|
77
|
+
steps: steps,
|
|
78
|
+
currentStep: currentStep,
|
|
79
|
+
submitStep: submitStep,
|
|
80
|
+
data: data,
|
|
81
|
+
onActiveStep: onActiveStep
|
|
82
|
+
}
|
|
83
|
+
}, children);
|
|
84
|
+
};
|
|
@@ -2,8 +2,9 @@ import React, { useContext } from 'react';
|
|
|
2
2
|
import { SummaryList, SummaryListItem, SummaryListItemKey, SummaryListItemValue, SummaryListItemAction } from '@digigov/ui/core/SummaryList';
|
|
3
3
|
import Title from '@digigov/ui/typography/Title';
|
|
4
4
|
import { Button } from '@digigov/ui/core';
|
|
5
|
-
import { QuestionsContext } from '@digigov/form/Questions/';
|
|
6
|
-
import { StepTitleBase
|
|
5
|
+
import { QuestionsContext } from '@digigov/form/Questions/QuestionsContext';
|
|
6
|
+
import { StepTitleBase } from '@digigov/form/Questions/Step/StepTitle';
|
|
7
|
+
import { StepContext } from '@digigov/form/Questions/Step/StepContext';
|
|
7
8
|
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
8
9
|
export var ReviewStep = function ReviewStep() {
|
|
9
10
|
var _useTranslation = useTranslation(),
|
|
@@ -48,4 +49,5 @@ export var ReviewStep = function ReviewStep() {
|
|
|
48
49
|
return submitStep && submitStep();
|
|
49
50
|
}
|
|
50
51
|
}, t('button.submit')));
|
|
51
|
-
};
|
|
52
|
+
};
|
|
53
|
+
export default ReviewStep;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
3
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
4
|
+
import React, { useContext, useState } from 'react';
|
|
5
|
+
import { QuestionsContext } from '@digigov/form/Questions/QuestionsContext';
|
|
6
|
+
import { StepContext } from '@digigov/form/Questions/Step/StepContext';
|
|
7
|
+
import { StepArrayReview } from '@digigov/form/Questions/Step/StepArrayReview';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Step component accepts Step data as props
|
|
11
|
+
* uses composable components to provide a wholesome UX
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export var Step = function Step(props) {
|
|
15
|
+
// or return all Questions and currentStepName
|
|
16
|
+
// or return a specific Step object
|
|
17
|
+
var _useContext = useContext(QuestionsContext),
|
|
18
|
+
currentStep = _useContext.currentStep,
|
|
19
|
+
submitStep = _useContext.submitStep,
|
|
20
|
+
data = _useContext.data;
|
|
21
|
+
|
|
22
|
+
var _useState = useState([]),
|
|
23
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
24
|
+
stepData = _useState2[0],
|
|
25
|
+
setStepData = _useState2[1];
|
|
26
|
+
|
|
27
|
+
var _useState3 = useState(false),
|
|
28
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
29
|
+
reviewActive = _useState4[0],
|
|
30
|
+
setReviewActive = _useState4[1];
|
|
31
|
+
|
|
32
|
+
var handleArraySubmit = function handleArraySubmit(_name, data) {
|
|
33
|
+
setStepData([].concat(_toConsumableArray(stepData), [data]));
|
|
34
|
+
setReviewActive(true);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
var handleArrayDeleteItem = function handleArrayDeleteItem(deleteIndex) {
|
|
38
|
+
var nextStepData = stepData.filter(function (_item, stepIndex) {
|
|
39
|
+
return stepIndex !== deleteIndex;
|
|
40
|
+
});
|
|
41
|
+
setStepData(nextStepData);
|
|
42
|
+
|
|
43
|
+
if (nextStepData.length === 0) {
|
|
44
|
+
setReviewActive(false);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
var handleArrayReviewStep = function handleArrayReviewStep(data) {
|
|
49
|
+
if (data.addMore === 'yes') {
|
|
50
|
+
setReviewActive(false);
|
|
51
|
+
} else if (data.addMore === 'no') {
|
|
52
|
+
submitStep(currentStep.name, stepData);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (!currentStep || props.name !== currentStep.name) return null; // then provide the currentStep object
|
|
57
|
+
|
|
58
|
+
return /*#__PURE__*/React.createElement(StepContext.Provider, {
|
|
59
|
+
value: _extends({}, currentStep, {
|
|
60
|
+
submitStep: currentStep.type === 'array' ? handleArraySubmit : submitStep,
|
|
61
|
+
initial: data[props.name]
|
|
62
|
+
})
|
|
63
|
+
}, reviewActive ? /*#__PURE__*/React.createElement(StepArrayReview, {
|
|
64
|
+
array: stepData,
|
|
65
|
+
handleSubmit: handleArrayReviewStep,
|
|
66
|
+
handleDelete: handleArrayDeleteItem
|
|
67
|
+
}) : props.children);
|
|
68
|
+
};
|
|
69
|
+
export default Step;
|