@dhis2/ui-forms 10.16.2 → 10.16.3
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/package.json +12 -11
- package/src/CheckboxFieldFF/CheckboxFieldFF.e2e.stories.js +52 -0
- package/src/CheckboxFieldFF/CheckboxFieldFF.js +52 -0
- package/src/CheckboxFieldFF/CheckboxFieldFF.prod.stories.js +142 -0
- package/src/CheckboxFieldFF/features/can_toggle_a_boolean/index.js +19 -0
- package/src/CheckboxFieldFF/features/can_toggle_a_boolean.feature +11 -0
- package/src/CheckboxFieldFF/features/can_toggle_a_value/index.js +21 -0
- package/src/CheckboxFieldFF/features/can_toggle_a_value.feature +11 -0
- package/src/CheckboxFieldFF/features/common/index.js +5 -0
- package/src/CheckboxFieldFF/features/displays_error/index.js +18 -0
- package/src/CheckboxFieldFF/features/displays_error.feature +6 -0
- package/src/FieldGroupFF/FieldGroupFF.js +42 -0
- package/src/FieldGroupFF/FieldGroupFF.prod.stories.js +49 -0
- package/src/FileInputFieldFF/FileInputFieldFF.e2e.stories.js +272 -0
- package/src/FileInputFieldFF/FileInputFieldFF.js +100 -0
- package/src/FileInputFieldFF/FileInputFieldFF.prod.stories.js +95 -0
- package/src/FileInputFieldFF/features/accepts_file/index.js +40 -0
- package/src/FileInputFieldFF/features/accepts_file.feature +13 -0
- package/src/FileInputFieldFF/features/common/index.js +9 -0
- package/src/FileInputFieldFF/features/displays_error/index.js +18 -0
- package/src/FileInputFieldFF/features/displays_error.feature +8 -0
- package/src/InputFieldFF/InputFieldFF.e2e.stories.js +19 -0
- package/src/InputFieldFF/InputFieldFF.js +58 -0
- package/src/InputFieldFF/InputFieldFF.prod.stories.js +102 -0
- package/src/InputFieldFF/features/can_set_a_value/index.js +14 -0
- package/src/InputFieldFF/features/can_set_a_value.feature +6 -0
- package/src/InputFieldFF/features/displays_error/index.js +15 -0
- package/src/InputFieldFF/features/displays_error.feature +6 -0
- package/src/MultiSelectFieldFF/MultiSelectFieldFF.e2e.stories.js +27 -0
- package/src/MultiSelectFieldFF/MultiSelectFieldFF.js +72 -0
- package/src/MultiSelectFieldFF/MultiSelectFieldFF.prod.stories.js +79 -0
- package/src/MultiSelectFieldFF/features/can_set_a_value/index.js +38 -0
- package/src/MultiSelectFieldFF/features/can_set_a_value.feature +14 -0
- package/src/MultiSelectFieldFF/features/common/index.js +7 -0
- package/src/MultiSelectFieldFF/features/displays_error/index.js +10 -0
- package/src/MultiSelectFieldFF/features/displays_error.feature +6 -0
- package/src/RadioFieldFF/RadioFieldFF.e2e.stories.js +39 -0
- package/src/RadioFieldFF/RadioFieldFF.js +52 -0
- package/src/RadioFieldFF/RadioFieldFF.prod.stories.js +104 -0
- package/src/RadioFieldFF/features/can_set_a_value/index.js +24 -0
- package/src/RadioFieldFF/features/can_set_a_value.feature +7 -0
- package/src/RadioFieldFF/features/common/index.js +9 -0
- package/src/RadioFieldFF/features/displays_error/index.js +13 -0
- package/src/RadioFieldFF/features/displays_error.feature +6 -0
- package/src/SimpleSingleSelectFieldFF/SimpleSingleSelectFieldFF.js +172 -0
- package/src/SimpleSingleSelectFieldFF/SimpleSingleSelectFieldFF.prod.stories.js +79 -0
- package/src/SimpleSingleSelectFieldFF/SimpleSingleSelectFieldFF.test.js +82 -0
- package/src/SingleSelectFieldFF/SingleSelectFieldFF.e2e.stories.js +23 -0
- package/src/SingleSelectFieldFF/SingleSelectFieldFF.js +70 -0
- package/src/SingleSelectFieldFF/SingleSelectFieldFF.prod.stories.js +77 -0
- package/src/SingleSelectFieldFF/features/can_set_a_value/index.js +22 -0
- package/src/SingleSelectFieldFF/features/can_set_a_value.feature +7 -0
- package/src/SingleSelectFieldFF/features/common/index.js +6 -0
- package/src/SingleSelectFieldFF/features/displays_error/index.js +10 -0
- package/src/SingleSelectFieldFF/features/displays_error.feature +6 -0
- package/src/SwitchFieldFF/SwitchFieldFF.e2e.stories.js +52 -0
- package/src/SwitchFieldFF/SwitchFieldFF.js +52 -0
- package/src/SwitchFieldFF/SwitchFieldFF.prod.stories.js +148 -0
- package/src/SwitchFieldFF/features/can_toggle_a_boolean/index.js +19 -0
- package/src/SwitchFieldFF/features/can_toggle_a_boolean.feature +11 -0
- package/src/SwitchFieldFF/features/can_toggle_a_value/index.js +21 -0
- package/src/SwitchFieldFF/features/can_toggle_a_value.feature +11 -0
- package/src/SwitchFieldFF/features/common/index.js +5 -0
- package/src/SwitchFieldFF/features/displays_error/index.js +18 -0
- package/src/SwitchFieldFF/features/displays_error.feature +6 -0
- package/src/TextAreaFieldFF/TextAreaFieldFF.e2e.stories.js +23 -0
- package/src/TextAreaFieldFF/TextAreaFieldFF.js +57 -0
- package/src/TextAreaFieldFF/TextAreaFieldFF.prod.stories.js +111 -0
- package/src/TextAreaFieldFF/features/can_set_a_value/index.js +14 -0
- package/src/TextAreaFieldFF/features/can_set_a_value.feature +6 -0
- package/src/TextAreaFieldFF/features/displays_error/index.js +15 -0
- package/src/TextAreaFieldFF/features/displays_error.feature +6 -0
- package/src/__tests__/__snapshots__/index.test.js.snap +65 -0
- package/src/__tests__/index.test.js +37 -0
- package/src/formDecorator.js +80 -0
- package/src/index.js +28 -0
- package/src/locales/ar/translations.json +30 -0
- package/src/locales/ar_IQ/translations.json +30 -0
- package/src/locales/ckb/translations.json +30 -0
- package/src/locales/cs/translations.json +30 -0
- package/src/locales/da/translations.json +30 -0
- package/src/locales/en/translations.json +30 -0
- package/src/locales/es/translations.json +30 -0
- package/src/locales/es_419/translations.json +30 -0
- package/src/locales/fr/translations.json +30 -0
- package/src/locales/hi_IN/translations.json +30 -0
- package/src/locales/id/translations.json +30 -0
- package/src/locales/index.js +84 -0
- package/src/locales/km/translations.json +30 -0
- package/src/locales/ko_KR/translations.json +30 -0
- package/src/locales/lo/translations.json +30 -0
- package/src/locales/my/translations.json +30 -0
- package/src/locales/nb/translations.json +30 -0
- package/src/locales/nl/translations.json +30 -0
- package/src/locales/prs/translations.json +30 -0
- package/src/locales/ps/translations.json +30 -0
- package/src/locales/pt/translations.json +30 -0
- package/src/locales/pt_BR/translations.json +30 -0
- package/src/locales/ro/translations.json +30 -0
- package/src/locales/ru/translations.json +30 -0
- package/src/locales/si/translations.json +30 -0
- package/src/locales/sv/translations.json +30 -0
- package/src/locales/tet/translations.json +30 -0
- package/src/locales/tg/translations.json +30 -0
- package/src/locales/uk/translations.json +30 -0
- package/src/locales/ur/translations.json +30 -0
- package/src/locales/uz_Latn/translations.json +30 -0
- package/src/locales/uz_UZ_Cyrl/translations.json +30 -0
- package/src/locales/uz_UZ_Latn/translations.json +30 -0
- package/src/locales/vi/translations.json +30 -0
- package/src/locales/zh/translations.json +30 -0
- package/src/locales/zh_CN/translations.json +30 -0
- package/src/shared/helpers/createBlurHandler.js +9 -0
- package/src/shared/helpers/createChangeHandler.js +21 -0
- package/src/shared/helpers/createFocusHandler.js +9 -0
- package/src/shared/helpers/createSelectChangeHandler.js +6 -0
- package/src/shared/helpers/createToggleChangeHandler.js +9 -0
- package/src/shared/helpers/getValidationText.js +21 -0
- package/src/shared/helpers/hasError.js +3 -0
- package/src/shared/helpers/isLoading.js +4 -0
- package/src/shared/helpers/isValid.js +4 -0
- package/src/shared/helpers.js +9 -0
- package/src/shared/propTypes.js +48 -0
- package/src/transformers/arrayWithIdObjects.js +8 -0
- package/src/transformers/index.js +1 -0
- package/src/validators/__tests__/alphaNumeric.test.js +29 -0
- package/src/validators/__tests__/boolean.test.js +23 -0
- package/src/validators/__tests__/composeValidators.test.js +23 -0
- package/src/validators/__tests__/createCharacterLengthRange.test.js +59 -0
- package/src/validators/__tests__/createEqualTo.test.js +43 -0
- package/src/validators/__tests__/createMaxCharacterLength.test.js +24 -0
- package/src/validators/__tests__/createMaxNumber.test.js +21 -0
- package/src/validators/__tests__/createMinCharacterLength.test.js +24 -0
- package/src/validators/__tests__/createMinNumber.test.js +21 -0
- package/src/validators/__tests__/createNumberRange.test.js +68 -0
- package/src/validators/__tests__/createPattern.test.js +40 -0
- package/src/validators/__tests__/dhis2Password.test.js +51 -0
- package/src/validators/__tests__/dhis2Username.test.js +75 -0
- package/src/validators/__tests__/email.test.js +83 -0
- package/src/validators/__tests__/hasValue.test.js +21 -0
- package/src/validators/__tests__/integer.test.js +48 -0
- package/src/validators/__tests__/internationalPhoneNumber.test.js +49 -0
- package/src/validators/__tests__/number.test.js +39 -0
- package/src/validators/__tests__/string.test.js +29 -0
- package/src/validators/__tests__/url.test.js +106 -0
- package/src/validators/alphaNumeric.js +15 -0
- package/src/validators/boolean.js +11 -0
- package/src/validators/composeValidators.js +8 -0
- package/src/validators/createCharacterLengthRange.js +27 -0
- package/src/validators/createEqualTo.js +16 -0
- package/src/validators/createMaxCharacterLength.js +13 -0
- package/src/validators/createMaxNumber.js +13 -0
- package/src/validators/createMinCharacterLength.js +13 -0
- package/src/validators/createMinNumber.js +13 -0
- package/src/validators/createNumberRange.js +28 -0
- package/src/validators/createPattern.js +22 -0
- package/src/validators/dhis2Password.js +81 -0
- package/src/validators/dhis2Username.js +16 -0
- package/src/validators/email.js +38 -0
- package/src/validators/hasValue.js +8 -0
- package/src/validators/helpers/index.js +23 -0
- package/src/validators/index.js +20 -0
- package/src/validators/integer.js +20 -0
- package/src/validators/internationalPhoneNumber.js +56 -0
- package/src/validators/number.js +9 -0
- package/src/validators/string.js +9 -0
- package/src/validators/test-helpers/index.js +21 -0
- package/src/validators/url.js +15 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dhis2/ui-forms",
|
|
3
|
-
"version": "10.16.
|
|
3
|
+
"version": "10.16.3",
|
|
4
4
|
"main": "./build/cjs/index.js",
|
|
5
5
|
"module": "./build/es/index.js",
|
|
6
6
|
"sideEffects": [
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@dhis2/prop-types": "^3.1.2",
|
|
38
|
-
"@dhis2-ui/button": "10.16.
|
|
39
|
-
"@dhis2-ui/checkbox": "10.16.
|
|
40
|
-
"@dhis2-ui/field": "10.16.
|
|
41
|
-
"@dhis2-ui/file-input": "10.16.
|
|
42
|
-
"@dhis2-ui/input": "10.16.
|
|
43
|
-
"@dhis2-ui/radio": "10.16.
|
|
44
|
-
"@dhis2-ui/select": "10.16.
|
|
45
|
-
"@dhis2-ui/switch": "10.16.
|
|
46
|
-
"@dhis2-ui/text-area": "10.16.
|
|
38
|
+
"@dhis2-ui/button": "10.16.3",
|
|
39
|
+
"@dhis2-ui/checkbox": "10.16.3",
|
|
40
|
+
"@dhis2-ui/field": "10.16.3",
|
|
41
|
+
"@dhis2-ui/file-input": "10.16.3",
|
|
42
|
+
"@dhis2-ui/input": "10.16.3",
|
|
43
|
+
"@dhis2-ui/radio": "10.16.3",
|
|
44
|
+
"@dhis2-ui/select": "10.16.3",
|
|
45
|
+
"@dhis2-ui/switch": "10.16.3",
|
|
46
|
+
"@dhis2-ui/text-area": "10.16.3",
|
|
47
47
|
"classnames": "^2.3.1",
|
|
48
48
|
"final-form": "^4.20.2",
|
|
49
49
|
"prop-types": "^15.7.2",
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
},
|
|
52
52
|
"files": [
|
|
53
53
|
"build",
|
|
54
|
-
"types"
|
|
54
|
+
"types",
|
|
55
|
+
"src"
|
|
55
56
|
],
|
|
56
57
|
"devDependencies": {
|
|
57
58
|
"react": "^18.3.1",
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Field } from 'react-final-form'
|
|
3
|
+
import { formDecorator } from '../formDecorator.js'
|
|
4
|
+
import { hasValue } from '../validators/index.js'
|
|
5
|
+
import { CheckboxFieldFF } from './CheckboxFieldFF.js'
|
|
6
|
+
|
|
7
|
+
// https://github.com/final-form/react-final-form-arrays/issues/111
|
|
8
|
+
const initialValue = ['yes']
|
|
9
|
+
|
|
10
|
+
export default { title: 'Testing:Checkbox', decorators: [formDecorator] }
|
|
11
|
+
export const Unchecked = () => (
|
|
12
|
+
<Field
|
|
13
|
+
type="checkbox"
|
|
14
|
+
component={CheckboxFieldFF}
|
|
15
|
+
className="checkbox"
|
|
16
|
+
name="checkbox"
|
|
17
|
+
label="Label text"
|
|
18
|
+
validate={hasValue}
|
|
19
|
+
required
|
|
20
|
+
/>
|
|
21
|
+
)
|
|
22
|
+
export const Checked = () => (
|
|
23
|
+
<Field
|
|
24
|
+
type="checkbox"
|
|
25
|
+
component={CheckboxFieldFF}
|
|
26
|
+
className="checkbox"
|
|
27
|
+
name="checkbox"
|
|
28
|
+
label="Label text"
|
|
29
|
+
initialValue={true}
|
|
30
|
+
/>
|
|
31
|
+
)
|
|
32
|
+
export const UncheckedWithValue = () => (
|
|
33
|
+
<Field
|
|
34
|
+
type="checkbox"
|
|
35
|
+
component={CheckboxFieldFF}
|
|
36
|
+
className="checkbox"
|
|
37
|
+
name="checkbox"
|
|
38
|
+
label="Label text"
|
|
39
|
+
value="yes"
|
|
40
|
+
/>
|
|
41
|
+
)
|
|
42
|
+
export const CheckedWithValue = () => (
|
|
43
|
+
<Field
|
|
44
|
+
type="checkbox"
|
|
45
|
+
component={CheckboxFieldFF}
|
|
46
|
+
className="checkbox"
|
|
47
|
+
name="checkbox"
|
|
48
|
+
label="Label text"
|
|
49
|
+
value="yes"
|
|
50
|
+
initialValue={initialValue}
|
|
51
|
+
/>
|
|
52
|
+
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CheckboxField } from '@dhis2-ui/checkbox'
|
|
2
|
+
import PropTypes from 'prop-types'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
import {
|
|
5
|
+
createToggleChangeHandler,
|
|
6
|
+
createFocusHandler,
|
|
7
|
+
createBlurHandler,
|
|
8
|
+
hasError,
|
|
9
|
+
isValid,
|
|
10
|
+
getValidationText,
|
|
11
|
+
} from '../shared/helpers.js'
|
|
12
|
+
import { metaPropType, inputPropType } from '../shared/propTypes.js'
|
|
13
|
+
|
|
14
|
+
export const CheckboxFieldFF = ({
|
|
15
|
+
error,
|
|
16
|
+
input,
|
|
17
|
+
meta,
|
|
18
|
+
showValidStatus,
|
|
19
|
+
valid,
|
|
20
|
+
validationText,
|
|
21
|
+
onBlur,
|
|
22
|
+
onFocus,
|
|
23
|
+
...rest
|
|
24
|
+
}) => (
|
|
25
|
+
<CheckboxField
|
|
26
|
+
{...rest}
|
|
27
|
+
name={input.name}
|
|
28
|
+
checked={input.checked}
|
|
29
|
+
value={input.value}
|
|
30
|
+
error={hasError(meta, error)}
|
|
31
|
+
valid={isValid(meta, valid, showValidStatus)}
|
|
32
|
+
validationText={getValidationText(meta, validationText, error)}
|
|
33
|
+
onFocus={createFocusHandler(input, onFocus)}
|
|
34
|
+
onChange={createToggleChangeHandler(input)}
|
|
35
|
+
onBlur={createBlurHandler(input, onBlur)}
|
|
36
|
+
/>
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
CheckboxFieldFF.propTypes = {
|
|
40
|
+
/** Provided by Final Form `Field` */
|
|
41
|
+
input: inputPropType.isRequired,
|
|
42
|
+
/** Provided by Final Form `Field` */
|
|
43
|
+
meta: metaPropType.isRequired,
|
|
44
|
+
|
|
45
|
+
error: PropTypes.bool,
|
|
46
|
+
showValidStatus: PropTypes.bool,
|
|
47
|
+
valid: PropTypes.bool,
|
|
48
|
+
validationText: PropTypes.string,
|
|
49
|
+
|
|
50
|
+
onBlur: PropTypes.func,
|
|
51
|
+
onFocus: PropTypes.func,
|
|
52
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Field } from 'react-final-form'
|
|
3
|
+
import { formDecorator } from '../formDecorator.js'
|
|
4
|
+
import { inputArgType, metaArgType } from '../shared/propTypes.js'
|
|
5
|
+
import { hasValue } from '../validators/index.js'
|
|
6
|
+
import { CheckboxFieldFF } from './CheckboxFieldFF.js'
|
|
7
|
+
|
|
8
|
+
const description = `
|
|
9
|
+
The \`CheckboxFieldFF\` is a wrapper around a \`CheckboxField\` that enables it to work with Final Form, the preferred library for form validation and utilities in DHIS 2 apps.
|
|
10
|
+
|
|
11
|
+
#### Final Form
|
|
12
|
+
|
|
13
|
+
See how to use Final Form at [Final Form - Getting Started](https://final-form.org/docs/react-final-form/getting-started).
|
|
14
|
+
|
|
15
|
+
Inside a Final Form \`<Form>\` component, these 'FF' UI components are intended to be used in the \`component\` prop of the [Final Form \`<Field>\` components](https://final-form.org/docs/react-final-form/api/Field) where they will receive some props from the Field, e.g. \`<Field component={CheckboxFieldFF} />\`. See the code samples below for examples.
|
|
16
|
+
|
|
17
|
+
#### Props
|
|
18
|
+
|
|
19
|
+
The props shown in the table below are generally provided to the \`CheckboxFieldFF\` wrapper by the Final Form \`Field\`.
|
|
20
|
+
|
|
21
|
+
Note that any props beyond the API of the \`Field\` component will be spread to the \`CheckboxFieldFF\`, which passes any extra props to the underlying \`CheckboxField\` using \`{...rest}\`.
|
|
22
|
+
|
|
23
|
+
Therefore, to add any props to the \`CheckboxFieldFF\` or \`CheckboxField\`, add those props to the parent Final Form \`Field\` component.
|
|
24
|
+
|
|
25
|
+
Also see \`Checkbox\` and \`CheckboxField\` for notes about props and implementation.
|
|
26
|
+
|
|
27
|
+
\`\`\`js
|
|
28
|
+
import { CheckboxFieldFF } from '@dhis2/ui'
|
|
29
|
+
\`\`\`
|
|
30
|
+
|
|
31
|
+
Press **Submit** to see the form values logged to the console.
|
|
32
|
+
`
|
|
33
|
+
|
|
34
|
+
export default {
|
|
35
|
+
title: 'Checkbox Field (Final Form)',
|
|
36
|
+
component: CheckboxFieldFF,
|
|
37
|
+
decorators: [formDecorator],
|
|
38
|
+
parameters: { docs: { description: { component: description } } },
|
|
39
|
+
argTypes: {
|
|
40
|
+
input: { ...inputArgType },
|
|
41
|
+
meta: { ...metaArgType },
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const Default = () => (
|
|
46
|
+
<Field
|
|
47
|
+
type="checkbox"
|
|
48
|
+
component={CheckboxFieldFF}
|
|
49
|
+
name="agree"
|
|
50
|
+
label="Do you agree?"
|
|
51
|
+
/>
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
export const RequiredWithValidate = () => (
|
|
55
|
+
<Field
|
|
56
|
+
type="checkbox"
|
|
57
|
+
name="agree"
|
|
58
|
+
component={CheckboxFieldFF}
|
|
59
|
+
required
|
|
60
|
+
validate={hasValue}
|
|
61
|
+
label="Do you agree?"
|
|
62
|
+
value="yes"
|
|
63
|
+
/>
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
export const Disabled = () => (
|
|
67
|
+
<Field
|
|
68
|
+
type="checkbox"
|
|
69
|
+
name="agree"
|
|
70
|
+
component={CheckboxFieldFF}
|
|
71
|
+
disabled
|
|
72
|
+
label="Do you agree?"
|
|
73
|
+
/>
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
export const HelpText = () => (
|
|
77
|
+
<Field
|
|
78
|
+
type="checkbox"
|
|
79
|
+
name="agree"
|
|
80
|
+
component={CheckboxFieldFF}
|
|
81
|
+
label="Do you agree?"
|
|
82
|
+
helpText="Click to agree"
|
|
83
|
+
/>
|
|
84
|
+
)
|
|
85
|
+
HelpText.storyName = 'Help text'
|
|
86
|
+
|
|
87
|
+
export const Statuses = () => (
|
|
88
|
+
<>
|
|
89
|
+
<Field
|
|
90
|
+
type="checkbox"
|
|
91
|
+
name="valid"
|
|
92
|
+
component={CheckboxFieldFF}
|
|
93
|
+
label="Valid"
|
|
94
|
+
valid
|
|
95
|
+
validationText="Validation text"
|
|
96
|
+
/>
|
|
97
|
+
<Field
|
|
98
|
+
type="checkbox"
|
|
99
|
+
name="warning"
|
|
100
|
+
component={CheckboxFieldFF}
|
|
101
|
+
label="Warning"
|
|
102
|
+
warning
|
|
103
|
+
validationText="Validation text"
|
|
104
|
+
/>
|
|
105
|
+
<Field
|
|
106
|
+
type="checkbox"
|
|
107
|
+
name="error"
|
|
108
|
+
component={CheckboxFieldFF}
|
|
109
|
+
label="Error"
|
|
110
|
+
error
|
|
111
|
+
validationText="Validation text"
|
|
112
|
+
/>
|
|
113
|
+
</>
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
export const ValueWhenChecked = () => (
|
|
117
|
+
<>
|
|
118
|
+
<Field
|
|
119
|
+
type="checkbox"
|
|
120
|
+
name="bool"
|
|
121
|
+
component={CheckboxFieldFF}
|
|
122
|
+
label="I produce boolean form values"
|
|
123
|
+
helpText="Click submit and check the console"
|
|
124
|
+
/>
|
|
125
|
+
<Field
|
|
126
|
+
type="checkbox"
|
|
127
|
+
name="string"
|
|
128
|
+
component={CheckboxFieldFF}
|
|
129
|
+
label="I produce string form values"
|
|
130
|
+
value="checked-value"
|
|
131
|
+
helpText="Click submit and check the console"
|
|
132
|
+
/>
|
|
133
|
+
<Field
|
|
134
|
+
type="checkbox"
|
|
135
|
+
name="string"
|
|
136
|
+
component={CheckboxFieldFF}
|
|
137
|
+
label="I also produce string form values"
|
|
138
|
+
value="another-checked-value"
|
|
139
|
+
helpText="Click submit and check the console"
|
|
140
|
+
/>
|
|
141
|
+
</>
|
|
142
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Given, Then } from '@badeball/cypress-cucumber-preprocessor'
|
|
2
|
+
|
|
3
|
+
Given('an unchecked Checkbox without value is rendered', () => {
|
|
4
|
+
cy.visitStory('Testing:Checkbox', 'Unchecked')
|
|
5
|
+
cy.verifyFormValue('checkbox', undefined)
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
Then('the form value that corresponds to the checkbox will be true', () => {
|
|
9
|
+
cy.verifyFormValue('checkbox', true)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
Given('a checked Checkbox without value is rendered', () => {
|
|
13
|
+
cy.visitStory('Testing:Checkbox', 'Checked')
|
|
14
|
+
cy.verifyFormValue('checkbox', true)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
Then('the form value that corresponds to the checkbox will be false', () => {
|
|
18
|
+
cy.verifyFormValue('checkbox', false)
|
|
19
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Feature: The Checkbox can toggle a boolean
|
|
2
|
+
|
|
3
|
+
Scenario: The user checks the Checkbox
|
|
4
|
+
Given an unchecked Checkbox without value is rendered
|
|
5
|
+
When the user clicks on the Checkbox
|
|
6
|
+
Then the form value that corresponds to the checkbox will be true
|
|
7
|
+
|
|
8
|
+
Scenario: The user unchecks the Checkbox
|
|
9
|
+
Given a checked Checkbox without value is rendered
|
|
10
|
+
When the user clicks on the Checkbox
|
|
11
|
+
Then the form value that corresponds to the checkbox will be false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Given, Then } from '@badeball/cypress-cucumber-preprocessor'
|
|
2
|
+
|
|
3
|
+
Given('an unchecked Checkbox with a value is rendered', () => {
|
|
4
|
+
cy.visitStory('Testing:Checkbox', 'Unchecked with value')
|
|
5
|
+
cy.verifyFormValue('checkbox', undefined)
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
Then('the form value that corresponds to the checkbox will be yes', () => {
|
|
9
|
+
cy.verifyFormArrayValue('checkbox', 'yes')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
Given('a checked Checkbox with a value is rendered', () => {
|
|
13
|
+
cy.visitStory('Testing:Checkbox', 'Checked with value')
|
|
14
|
+
cy.verifyFormArrayValue('checkbox', 'yes')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
Then('the form value that corresponds to the checkbox will be correct', () => {
|
|
18
|
+
cy.window().then((win) => {
|
|
19
|
+
expect(win.formValues.checkbox).to.deep.equal([])
|
|
20
|
+
})
|
|
21
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Feature: The Checkbox can toggle a value
|
|
2
|
+
|
|
3
|
+
Scenario: The user clicks on the Checkbox
|
|
4
|
+
Given an unchecked Checkbox with a value is rendered
|
|
5
|
+
When the user clicks on the Checkbox
|
|
6
|
+
Then the form value that corresponds to the checkbox will be yes
|
|
7
|
+
|
|
8
|
+
Scenario: The user clicks on the Checkbox
|
|
9
|
+
Given a checked Checkbox with a value is rendered
|
|
10
|
+
When the user clicks on the Checkbox
|
|
11
|
+
Then the form value that corresponds to the checkbox will be correct
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Given, When, Then } from '@badeball/cypress-cucumber-preprocessor'
|
|
2
|
+
import { hasValueMessage } from '../../../validators/hasValue.js'
|
|
3
|
+
|
|
4
|
+
Given('an unchecked Checkbox is rendered', () => {
|
|
5
|
+
cy.visitStory('Testing:Checkbox', 'Unchecked')
|
|
6
|
+
cy.verifyFormValue('checkbox', undefined)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
When('the user submits the form', () => {
|
|
10
|
+
cy.get('button[type="submit"]').click()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
Then('an error message is shown', () => {
|
|
14
|
+
cy.get('[data-test="dhis2-uiwidgets-checkboxfield-validation"]').should(
|
|
15
|
+
'contain',
|
|
16
|
+
hasValueMessage
|
|
17
|
+
)
|
|
18
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { FieldGroup } from '@dhis2-ui/field'
|
|
2
|
+
import PropTypes from 'prop-types'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
import { useField } from 'react-final-form'
|
|
5
|
+
|
|
6
|
+
export const FieldGroupFF = ({ name, label, children, required }) => {
|
|
7
|
+
const {
|
|
8
|
+
meta: { error, touched },
|
|
9
|
+
} = useField(name, { subscription: { error: true, touched: true } })
|
|
10
|
+
|
|
11
|
+
const isError = !!error && !!touched
|
|
12
|
+
let errorText
|
|
13
|
+
|
|
14
|
+
if (isError) {
|
|
15
|
+
if (typeof error === 'string') {
|
|
16
|
+
errorText = error
|
|
17
|
+
} else if (typeof error === 'object' && error[name]) {
|
|
18
|
+
errorText = error[name]
|
|
19
|
+
} else {
|
|
20
|
+
errorText = null
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<FieldGroup
|
|
26
|
+
label={label}
|
|
27
|
+
required={required}
|
|
28
|
+
error={isError}
|
|
29
|
+
name={name}
|
|
30
|
+
validationText={errorText}
|
|
31
|
+
>
|
|
32
|
+
{children}
|
|
33
|
+
</FieldGroup>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
FieldGroupFF.propTypes = {
|
|
38
|
+
children: PropTypes.node,
|
|
39
|
+
label: PropTypes.string,
|
|
40
|
+
name: PropTypes.string,
|
|
41
|
+
required: PropTypes.bool,
|
|
42
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Field } from 'react-final-form'
|
|
3
|
+
import { CheckboxFieldFF } from '../CheckboxFieldFF/CheckboxFieldFF.js'
|
|
4
|
+
import { formDecorator } from '../formDecorator.js'
|
|
5
|
+
import { hasValue } from '../validators/index.js'
|
|
6
|
+
import { FieldGroupFF } from './FieldGroupFF.js'
|
|
7
|
+
|
|
8
|
+
const description = `
|
|
9
|
+
This component is intended for use with [Final Form](https://final-form.org/docs/react-final-form/getting-started), the preferred library for form validation and utilities in DHIS 2 apps.
|
|
10
|
+
|
|
11
|
+
\`FieldGroupFF\` groups related fields (using the Final Form \`<Field>\`), like checkboxes, and adds a label and name.
|
|
12
|
+
|
|
13
|
+
\`\`\`js
|
|
14
|
+
import { FieldGroupFF } from '@dhis2/ui'
|
|
15
|
+
\`\`\`
|
|
16
|
+
|
|
17
|
+
Press **Submit** to see the form values logged to the console.
|
|
18
|
+
`
|
|
19
|
+
|
|
20
|
+
export default {
|
|
21
|
+
title: 'Field Group (Final Form)',
|
|
22
|
+
component: FieldGroupFF,
|
|
23
|
+
decorators: [formDecorator],
|
|
24
|
+
parameters: { docs: { description: { component: description } } },
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const Default = () => (
|
|
28
|
+
<FieldGroupFF label="Group label" required name="set">
|
|
29
|
+
<Field
|
|
30
|
+
type="checkbox"
|
|
31
|
+
component={CheckboxFieldFF}
|
|
32
|
+
name="set"
|
|
33
|
+
label="One"
|
|
34
|
+
value="one"
|
|
35
|
+
validate={hasValue}
|
|
36
|
+
validationText={null}
|
|
37
|
+
/>
|
|
38
|
+
|
|
39
|
+
<Field
|
|
40
|
+
type="checkbox"
|
|
41
|
+
component={CheckboxFieldFF}
|
|
42
|
+
name="set"
|
|
43
|
+
label="Two"
|
|
44
|
+
validate={hasValue}
|
|
45
|
+
value="two"
|
|
46
|
+
validationText={null}
|
|
47
|
+
/>
|
|
48
|
+
</FieldGroupFF>
|
|
49
|
+
)
|