@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
|
@@ -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;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
import { QuestionsContext } from '@digigov/form/Questions/QuestionsContext';
|
|
3
|
+
import PageTitle, { PageTitleHeading } from '@digigov/ui/app/PageTitle';
|
|
4
|
+
import Button from '@digigov/ui/core/Button';
|
|
5
|
+
import { FormBuilder } from '@digigov/form/FormBuilder';
|
|
6
|
+
import { Field } from '@digigov/form/Field';
|
|
7
|
+
import { Fieldset } from '@digigov/form/Fieldset';
|
|
8
|
+
import Label from '@digigov/form/inputs/Label';
|
|
9
|
+
import { SummaryList, SummaryListItem, SummaryListItemKey, SummaryListItemValue, SummaryListItemAction } from '@digigov/ui/core/SummaryList';
|
|
10
|
+
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
11
|
+
import { getAddMoreFields } from '@digigov/form/Questions/Step/getAddMoreFields';
|
|
12
|
+
export var StepArrayReview = function StepArrayReview(props) {
|
|
13
|
+
var _currentStep$review2, _currentStep$review3, _currentStep$review4;
|
|
14
|
+
|
|
15
|
+
var _useTranslation = useTranslation(),
|
|
16
|
+
t = _useTranslation.t;
|
|
17
|
+
|
|
18
|
+
var _useContext = useContext(QuestionsContext),
|
|
19
|
+
currentStep = _useContext.currentStep;
|
|
20
|
+
|
|
21
|
+
var fields = getAddMoreFields(currentStep);
|
|
22
|
+
var primaryField = currentStep.fields.find(function (field) {
|
|
23
|
+
var _currentStep$review;
|
|
24
|
+
|
|
25
|
+
return field.key === ((_currentStep$review = currentStep.review) === null || _currentStep$review === void 0 ? void 0 : _currentStep$review.primaryFieldKey);
|
|
26
|
+
}) || {};
|
|
27
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PageTitle, null, /*#__PURE__*/React.createElement(PageTitleHeading, null, ((_currentStep$review2 = currentStep.review) === null || _currentStep$review2 === void 0 ? void 0 : _currentStep$review2.title) && t((_currentStep$review3 = currentStep.review) === null || _currentStep$review3 === void 0 ? void 0 : _currentStep$review3.title))), /*#__PURE__*/React.createElement(SummaryList, null, props.array.map(function (item, idx) {
|
|
28
|
+
return /*#__PURE__*/React.createElement(SummaryListItem, {
|
|
29
|
+
key: idx
|
|
30
|
+
}, /*#__PURE__*/React.createElement(SummaryListItemKey, null, t(primaryField.label.primary)), /*#__PURE__*/React.createElement(SummaryListItemValue, null, item[primaryField.key]), /*#__PURE__*/React.createElement(SummaryListItemAction, {
|
|
31
|
+
onClick: function onClick() {
|
|
32
|
+
return props.handleDelete(idx);
|
|
33
|
+
}
|
|
34
|
+
}, t('button.delete')));
|
|
35
|
+
})), /*#__PURE__*/React.createElement(FormBuilder, {
|
|
36
|
+
key: "addmore",
|
|
37
|
+
fields: fields,
|
|
38
|
+
onSubmit: props.handleSubmit
|
|
39
|
+
}, /*#__PURE__*/React.createElement(Fieldset, null, /*#__PURE__*/React.createElement(Label, {
|
|
40
|
+
label: (_currentStep$review4 = currentStep.review) === null || _currentStep$review4 === void 0 ? void 0 : _currentStep$review4.addMore.title
|
|
41
|
+
}), fields.map(function (field) {
|
|
42
|
+
return /*#__PURE__*/React.createElement(Field, {
|
|
43
|
+
key: field.key,
|
|
44
|
+
name: field.key
|
|
45
|
+
});
|
|
46
|
+
})), /*#__PURE__*/React.createElement(Button, {
|
|
47
|
+
type: "submit"
|
|
48
|
+
}, t('button.submit'))));
|
|
49
|
+
};
|
|
50
|
+
export default StepArrayReview;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
export var StepContext = /*#__PURE__*/createContext({
|
|
3
|
+
name: '',
|
|
4
|
+
fields: [],
|
|
5
|
+
initial: [],
|
|
6
|
+
errorLabels: [],
|
|
7
|
+
submitStep: '',
|
|
8
|
+
review: {
|
|
9
|
+
title: '',
|
|
10
|
+
primaryFieldKey: '',
|
|
11
|
+
addMore: {
|
|
12
|
+
title: {
|
|
13
|
+
primary: ''
|
|
14
|
+
},
|
|
15
|
+
answers: {
|
|
16
|
+
positive: {
|
|
17
|
+
primary: 'Yes'
|
|
18
|
+
},
|
|
19
|
+
negative: {
|
|
20
|
+
primary: 'No'
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
type: 'object'
|
|
26
|
+
});
|
|
27
|
+
export default StepContext;
|
|
@@ -2,7 +2,7 @@ import _extends from "@babel/runtime/helpers/extends";
|
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
3
|
var _excluded = ["children", "description"];
|
|
4
4
|
import React, { useContext } from 'react';
|
|
5
|
-
import { StepContext } from '@digigov/form/Questions/Step';
|
|
5
|
+
import { StepContext } from '@digigov/form/Questions/Step/StepContext';
|
|
6
6
|
import Paragraph from '@digigov/ui/typography/Paragraph';
|
|
7
7
|
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
8
8
|
export var StepDescriptionBase = function StepDescriptionBase(_ref) {
|
|
@@ -2,8 +2,10 @@ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProper
|
|
|
2
2
|
var _excluded = ["children", "submitButton"],
|
|
3
3
|
_excluded2 = ["name", "submitStep"];
|
|
4
4
|
import React, { useContext } from 'react';
|
|
5
|
-
import { StepContext } from '@digigov/form/Questions/Step';
|
|
6
|
-
import FormBuilder
|
|
5
|
+
import { StepContext } from '@digigov/form/Questions/Step/StepContext';
|
|
6
|
+
import FormBuilder from '@digigov/form/FormBuilder';
|
|
7
|
+
import Fieldset from '@digigov/form/Fieldset';
|
|
8
|
+
import Field from '@digigov/form/Field';
|
|
7
9
|
import Button from '@digigov/ui/core/Button';
|
|
8
10
|
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
9
11
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useContext } from 'react';
|
|
2
|
-
import { StepContext } from '@digigov/form/Questions/Step';
|
|
2
|
+
import { StepContext } from '@digigov/form/Questions/Step/StepContext';
|
|
3
3
|
import PageTitle, { PageTitleHeading, PageTitleSection, PageTitleCaption } from '@digigov/ui/app/PageTitle';
|
|
4
4
|
import BackButton from '@digigov/ui/core/Button/BackButton';
|
|
5
5
|
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export var getAddMoreFields = function getAddMoreFields(currentStep) {
|
|
2
|
+
var _currentStep$review, _currentStep$review2;
|
|
3
|
+
|
|
4
|
+
return [{
|
|
5
|
+
key: 'addMore',
|
|
6
|
+
type: 'choice:single',
|
|
7
|
+
required: true,
|
|
8
|
+
extra: {
|
|
9
|
+
options: [{
|
|
10
|
+
label: ((_currentStep$review = currentStep.review) === null || _currentStep$review === void 0 ? void 0 : _currentStep$review.addMore.answers.positive) || {
|
|
11
|
+
primary: 'Yes'
|
|
12
|
+
},
|
|
13
|
+
value: 'yes'
|
|
14
|
+
}, {
|
|
15
|
+
label: ((_currentStep$review2 = currentStep.review) === null || _currentStep$review2 === void 0 ? void 0 : _currentStep$review2.addMore.answers.negative) || {
|
|
16
|
+
primary: 'No'
|
|
17
|
+
},
|
|
18
|
+
value: 'no'
|
|
19
|
+
}]
|
|
20
|
+
}
|
|
21
|
+
}];
|
|
22
|
+
};
|
|
@@ -1,165 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
export var StepContext = /*#__PURE__*/createContext({
|
|
15
|
-
name: '',
|
|
16
|
-
fields: [],
|
|
17
|
-
initial: [],
|
|
18
|
-
errorLabels: [],
|
|
19
|
-
submitStep: '',
|
|
20
|
-
review: {
|
|
21
|
-
title: '',
|
|
22
|
-
primaryFieldKey: '',
|
|
23
|
-
addMore: {
|
|
24
|
-
title: {
|
|
25
|
-
primary: ''
|
|
26
|
-
},
|
|
27
|
-
answers: {
|
|
28
|
-
positive: {
|
|
29
|
-
primary: 'Yes'
|
|
30
|
-
},
|
|
31
|
-
negative: {
|
|
32
|
-
primary: 'No'
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
type: 'object'
|
|
38
|
-
});
|
|
39
|
-
/**
|
|
40
|
-
* The Step component accepts Step data as props
|
|
41
|
-
* uses composable components to provide a wholesome UX
|
|
42
|
-
*
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
export var Step = function Step(props) {
|
|
46
|
-
// or return all Questions and currentStepName
|
|
47
|
-
// or return a specific Step object
|
|
48
|
-
var _useContext = useContext(QuestionsContext),
|
|
49
|
-
currentStep = _useContext.currentStep,
|
|
50
|
-
submitStep = _useContext.submitStep,
|
|
51
|
-
data = _useContext.data;
|
|
52
|
-
|
|
53
|
-
var _useState = useState([]),
|
|
54
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
55
|
-
stepData = _useState2[0],
|
|
56
|
-
setStepData = _useState2[1];
|
|
57
|
-
|
|
58
|
-
var _useState3 = useState(false),
|
|
59
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
60
|
-
reviewActive = _useState4[0],
|
|
61
|
-
setReviewActive = _useState4[1];
|
|
62
|
-
|
|
63
|
-
var handleArraySubmit = function handleArraySubmit(_name, data) {
|
|
64
|
-
setStepData([].concat(_toConsumableArray(stepData), [data]));
|
|
65
|
-
setReviewActive(true);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
var handleArrayDeleteItem = function handleArrayDeleteItem(deleteIndex) {
|
|
69
|
-
var nextStepData = stepData.filter(function (_item, stepIndex) {
|
|
70
|
-
return stepIndex !== deleteIndex;
|
|
71
|
-
});
|
|
72
|
-
setStepData(nextStepData);
|
|
73
|
-
|
|
74
|
-
if (nextStepData.length === 0) {
|
|
75
|
-
setReviewActive(false);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
var handleArrayReviewStep = function handleArrayReviewStep(data) {
|
|
80
|
-
if (data.addMore === 'yes') {
|
|
81
|
-
setReviewActive(false);
|
|
82
|
-
} else if (data.addMore === 'no') {
|
|
83
|
-
submitStep(currentStep.name, stepData);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
if (!currentStep || props.name !== currentStep.name) return null; // then provide the currentStep object
|
|
88
|
-
|
|
89
|
-
return /*#__PURE__*/React.createElement(StepContext.Provider, {
|
|
90
|
-
value: _extends({}, currentStep, {
|
|
91
|
-
submitStep: currentStep.type === 'array' ? handleArraySubmit : submitStep,
|
|
92
|
-
initial: data[props.name]
|
|
93
|
-
})
|
|
94
|
-
}, reviewActive ? /*#__PURE__*/React.createElement(StepArrayReview, {
|
|
95
|
-
array: stepData,
|
|
96
|
-
handleSubmit: handleArrayReviewStep,
|
|
97
|
-
handleDelete: handleArrayDeleteItem
|
|
98
|
-
}) : props.children);
|
|
99
|
-
};
|
|
100
|
-
export var getAddMoreFields = function getAddMoreFields(currentStep) {
|
|
101
|
-
var _currentStep$review, _currentStep$review2;
|
|
102
|
-
|
|
103
|
-
return [{
|
|
104
|
-
key: 'addMore',
|
|
105
|
-
type: 'choice:single',
|
|
106
|
-
required: true,
|
|
107
|
-
extra: {
|
|
108
|
-
options: [{
|
|
109
|
-
label: ((_currentStep$review = currentStep.review) === null || _currentStep$review === void 0 ? void 0 : _currentStep$review.addMore.answers.positive) || {
|
|
110
|
-
primary: 'Yes'
|
|
111
|
-
},
|
|
112
|
-
value: 'yes'
|
|
113
|
-
}, {
|
|
114
|
-
label: ((_currentStep$review2 = currentStep.review) === null || _currentStep$review2 === void 0 ? void 0 : _currentStep$review2.addMore.answers.negative) || {
|
|
115
|
-
primary: 'No'
|
|
116
|
-
},
|
|
117
|
-
value: 'no'
|
|
118
|
-
}]
|
|
119
|
-
}
|
|
120
|
-
}];
|
|
121
|
-
};
|
|
122
|
-
export var StepArrayReview = function StepArrayReview(props) {
|
|
123
|
-
var _currentStep$review4, _currentStep$review5, _currentStep$review6;
|
|
124
|
-
|
|
125
|
-
var _useTranslation = useTranslation(),
|
|
126
|
-
t = _useTranslation.t;
|
|
127
|
-
|
|
128
|
-
var _useContext2 = useContext(QuestionsContext),
|
|
129
|
-
currentStep = _useContext2.currentStep;
|
|
130
|
-
|
|
131
|
-
var fields = getAddMoreFields(currentStep);
|
|
132
|
-
var primaryField = currentStep.fields.find(function (field) {
|
|
133
|
-
var _currentStep$review3;
|
|
134
|
-
|
|
135
|
-
return field.key === ((_currentStep$review3 = currentStep.review) === null || _currentStep$review3 === void 0 ? void 0 : _currentStep$review3.primaryFieldKey);
|
|
136
|
-
}) || {};
|
|
137
|
-
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PageTitle, null, /*#__PURE__*/React.createElement(PageTitleHeading, null, ((_currentStep$review4 = currentStep.review) === null || _currentStep$review4 === void 0 ? void 0 : _currentStep$review4.title) && t((_currentStep$review5 = currentStep.review) === null || _currentStep$review5 === void 0 ? void 0 : _currentStep$review5.title))), /*#__PURE__*/React.createElement(SummaryList, null, props.array.map(function (item, idx) {
|
|
138
|
-
return /*#__PURE__*/React.createElement(SummaryListItem, {
|
|
139
|
-
key: idx
|
|
140
|
-
}, /*#__PURE__*/React.createElement(SummaryListItemKey, null, t(primaryField.label.primary)), /*#__PURE__*/React.createElement(SummaryListItemValue, null, item[primaryField.key]), /*#__PURE__*/React.createElement(SummaryListItemAction, {
|
|
141
|
-
onClick: function onClick() {
|
|
142
|
-
return props.handleDelete(idx);
|
|
143
|
-
}
|
|
144
|
-
}, t('button.delete')));
|
|
145
|
-
})), /*#__PURE__*/React.createElement(FormBuilder, {
|
|
146
|
-
key: "addmore",
|
|
147
|
-
fields: fields,
|
|
148
|
-
onSubmit: props.handleSubmit
|
|
149
|
-
}, /*#__PURE__*/React.createElement(Fieldset, null, /*#__PURE__*/React.createElement(Label, {
|
|
150
|
-
label: (_currentStep$review6 = currentStep.review) === null || _currentStep$review6 === void 0 ? void 0 : _currentStep$review6.addMore.title
|
|
151
|
-
}), fields.map(function (field) {
|
|
152
|
-
return /*#__PURE__*/React.createElement(Field, {
|
|
153
|
-
key: field.key,
|
|
154
|
-
name: field.key
|
|
155
|
-
});
|
|
156
|
-
})), /*#__PURE__*/React.createElement(Button, {
|
|
157
|
-
type: "submit"
|
|
158
|
-
}, t('button.submit'))));
|
|
159
|
-
};
|
|
160
|
-
export default Step;
|
|
161
|
-
export * from '@digigov/form/Questions/Step/StepDescription';
|
|
162
|
-
export * from '@digigov/form/Questions/Step/StepForm';
|
|
163
|
-
export * from '@digigov/form/Questions/Step/StepQuote';
|
|
164
|
-
export * from '@digigov/form/Questions/Step/StepTitle';
|
|
165
|
-
export * from '@digigov/form/Questions/Step/ReviewStep';
|
|
1
|
+
export * from '@digigov/form/Questions/Step/ReviewStep';
|
|
2
|
+
export * from '@digigov/form/Questions/Step/Step';
|
|
3
|
+
export * from '@digigov/form/Questions/Step/StepArrayReview';
|
|
4
|
+
export * from '@digigov/form/Questions/Step/StepContext';
|
|
5
|
+
export { default as StepDescription } from '@digigov/form/Questions/Step/StepDescription';
|
|
6
|
+
export { default as StepForm } from '@digigov/form/Questions/Step/StepForm';
|
|
7
|
+
export { default as StepQuote } from '@digigov/form/Questions/Step/StepQuote';
|
|
8
|
+
export { default as StepTitle } from '@digigov/form/Questions/Step/StepTitle';
|
|
9
|
+
export * from '@digigov/form/Questions/Step/getAddMoreFields';
|
|
10
|
+
export * from '@digigov/form/Questions/Step/';
|
|
11
|
+
export * from '@digigov/form/Questions/Step/types';
|
|
12
|
+
import Step from '@digigov/form/Questions/Step/Step';
|
|
13
|
+
export default Step;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function getNextStep(currentStep, steps, data) {
|
|
2
|
+
if (currentStep.nextStep) {
|
|
3
|
+
var nextStepName = currentStep.nextStep(data);
|
|
4
|
+
|
|
5
|
+
if (nextStepName) {
|
|
6
|
+
return steps.find(function (step) {
|
|
7
|
+
return step.name === nextStepName;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var currentStepIndex = steps.findIndex(function (step) {
|
|
13
|
+
return step.name === currentStep.name;
|
|
14
|
+
});
|
|
15
|
+
var nextStep = steps.slice(currentStepIndex + 1).find(function (step) {
|
|
16
|
+
if (step.show) {
|
|
17
|
+
return step.show(data);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return true;
|
|
21
|
+
});
|
|
22
|
+
return nextStep;
|
|
23
|
+
}
|
package/esm/Questions/index.js
CHANGED
|
@@ -1,120 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import React, { useState, createContext, useEffect } from 'react';
|
|
3
|
-
var isBrowser = typeof window !== 'undefined';
|
|
4
|
-
var defaultStep = {
|
|
5
|
-
name: ''
|
|
6
|
-
};
|
|
7
|
-
export var QuestionsContext = /*#__PURE__*/createContext({
|
|
8
|
-
currentStep: defaultStep,
|
|
9
|
-
steps: [],
|
|
10
|
-
submitStep: function submitStep() {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
function getNextStep(currentStep, steps, data) {
|
|
16
|
-
if (currentStep.nextStep) {
|
|
17
|
-
var nextStepName = currentStep.nextStep(data);
|
|
18
|
-
|
|
19
|
-
if (nextStepName) {
|
|
20
|
-
return steps.find(function (step) {
|
|
21
|
-
return step.name === nextStepName;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
var currentStepIndex = steps.findIndex(function (step) {
|
|
27
|
-
return step.name === currentStep.name;
|
|
28
|
-
});
|
|
29
|
-
var nextStep = steps.slice(currentStepIndex + 1).find(function (step) {
|
|
30
|
-
if (step.show) {
|
|
31
|
-
return step.show(data);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return true;
|
|
35
|
-
});
|
|
36
|
-
return nextStep;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* The Question component accepts question data as props
|
|
40
|
-
* uses composable components to provide a wholesome UX
|
|
41
|
-
*
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export var Questions = function Questions(_ref) {
|
|
46
|
-
var name = _ref.name,
|
|
47
|
-
steps = _ref.steps,
|
|
48
|
-
_ref$initialData = _ref.initialData,
|
|
49
|
-
initialData = _ref$initialData === void 0 ? {} : _ref$initialData,
|
|
50
|
-
onChange = _ref.onChange,
|
|
51
|
-
onSubmit = _ref.onSubmit,
|
|
52
|
-
onActiveStep = _ref.onActiveStep,
|
|
53
|
-
forceStepName = _ref.forceStepName,
|
|
54
|
-
_ref$localDraft = _ref.localDraft,
|
|
55
|
-
localDraft = _ref$localDraft === void 0 ? false : _ref$localDraft,
|
|
56
|
-
children = _ref.children;
|
|
57
|
-
|
|
58
|
-
var _useState = useState({
|
|
59
|
-
name: ''
|
|
60
|
-
}),
|
|
61
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
62
|
-
currentStep = _useState2[0],
|
|
63
|
-
setCurrentStep = _useState2[1];
|
|
64
|
-
|
|
65
|
-
useEffect(function () {
|
|
66
|
-
if (!forceStepName) {
|
|
67
|
-
setCurrentStep(steps[0]);
|
|
68
|
-
}
|
|
69
|
-
}, [forceStepName, steps]);
|
|
70
|
-
useEffect(function () {
|
|
71
|
-
if (forceStepName !== currentStep.name) {
|
|
72
|
-
var forceStep = steps.find(function (_ref2) {
|
|
73
|
-
var name = _ref2.name;
|
|
74
|
-
return name === forceStepName;
|
|
75
|
-
});
|
|
76
|
-
forceStep && setCurrentStep(forceStep);
|
|
77
|
-
}
|
|
78
|
-
}, [forceStepName]);
|
|
79
|
-
var localData = isBrowser && localDraft && window.localStorage.getItem("questions-".concat(name));
|
|
80
|
-
|
|
81
|
-
var _useState3 = useState(localData && JSON.parse(localData) || initialData),
|
|
82
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
83
|
-
data = _useState4[0],
|
|
84
|
-
setData = _useState4[1];
|
|
85
|
-
|
|
86
|
-
var submitStep = function submitStep(stepName, stepData) {
|
|
87
|
-
data[stepName] = stepData;
|
|
88
|
-
|
|
89
|
-
if (localDraft) {
|
|
90
|
-
isBrowser && window.localStorage.setItem("questions-".concat(name), JSON.stringify(data));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (onChange) {
|
|
94
|
-
onChange && onChange(data);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
setData(data);
|
|
98
|
-
var nextStep = getNextStep(currentStep, steps, data);
|
|
99
|
-
|
|
100
|
-
if (nextStep) {
|
|
101
|
-
onActiveStep && onActiveStep(nextStep);
|
|
102
|
-
setCurrentStep(nextStep);
|
|
103
|
-
} else {
|
|
104
|
-
onSubmit(data);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
return /*#__PURE__*/React.createElement(QuestionsContext.Provider, {
|
|
109
|
-
value: {
|
|
110
|
-
steps: steps,
|
|
111
|
-
currentStep: currentStep,
|
|
112
|
-
submitStep: submitStep,
|
|
113
|
-
data: data,
|
|
114
|
-
onActiveStep: onActiveStep
|
|
115
|
-
}
|
|
116
|
-
}, children);
|
|
117
|
-
};
|
|
1
|
+
import { Questions } from '@digigov/form/Questions/Questions';
|
|
118
2
|
export default Questions;
|
|
119
|
-
export * from '@digigov/form/Questions/Step';
|
|
120
|
-
export * from '@digigov/form';
|
|
3
|
+
export * from '@digigov/form/Questions/Step';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { mount } from 'enzyme';
|
|
3
|
-
import
|
|
3
|
+
import { Step, StepForm, StepQuote, StepTitle } from '@digigov/form/Questions';
|
|
4
|
+
import Questions from '@digigov/form/Questions';
|
|
4
5
|
import { Field } from '@digigov/form';
|
|
5
6
|
var steps = [{
|
|
6
7
|
name: 'intro',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|