Skip to content

Commit ef6d9e5

Browse files
authored
Merge pull request #956 from fhlavac/validate-on-mount
Add validateOnMount option
2 parents 0c833c7 + 65a3403 commit ef6d9e5

File tree

15 files changed

+223
-28
lines changed

15 files changed

+223
-28
lines changed

packages/pf4-component-mapper/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ Data Driven Forms supports all kinds of component, basic set is consisted of:
9696
- [Plain text](https://data-driven-forms.org/mappers/plain-text?mapper=pf4)
9797
- [Wizard](https://data-driven-forms.org/mappers/wizard?mapper=pf4)
9898

99+
100+
### ValidateOnMount
101+
102+
PF4 mapper provides an option to validate a field when the component is mounted. Just set `validateOnMount` to `true`.
103+
104+
```jsx
105+
{
106+
component: 'text-field',
107+
name: 'required-field',
108+
validate: [{type: 'required'}],
109+
validateOnMount: true
110+
}
111+
```
112+
113+
This field will show the error immediately.
114+
115+
99116
### Useful links
100117

101118
- [Data Driven Forms documentation](https://data-driven-forms.org/)

packages/pf4-component-mapper/src/common/form-group.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import PropTypes from 'prop-types';
44

55
import showError from './show-error';
66

7-
const FormGroup = ({ label, isRequired, helperText, meta, description, hideLabel, children, id, FormGroupProps }) => (
7+
const FormGroup = ({ label, isRequired, helperText, meta, validateOnMount, description, hideLabel, children, id, FormGroupProps }) => (
88
<Pf4FormGroup
99
isRequired={isRequired}
1010
label={!hideLabel && label}
1111
fieldId={id}
1212
helperText={(meta.touched && meta.warning) || helperText}
1313
helperTextInvalid={meta.error || meta.submitError}
14-
{...showError(meta)}
14+
{...showError(meta, validateOnMount)}
1515
{...FormGroupProps}
1616
>
1717
{description && (
@@ -30,6 +30,7 @@ FormGroup.propTypes = {
3030
meta: PropTypes.object.isRequired,
3131
description: PropTypes.node,
3232
hideLabel: PropTypes.bool,
33+
validateOnMount: PropTypes.bool,
3334
id: PropTypes.string.isRequired,
3435
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]).isRequired,
3536
FormGroupProps: PropTypes.object

packages/pf4-component-mapper/src/common/show-error.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const showError = ({ error, touched, warning, submitError }) => {
2-
if (touched && error) {
1+
const showError = ({ error, touched, warning, submitError }, validateOnMount) => {
2+
if ((touched || validateOnMount) && error) {
33
return { validated: 'error' };
44
}
55

6-
if (touched && submitError) {
6+
if ((touched || validateOnMount) && submitError) {
77
return { validated: 'error' };
88
}
99

10-
if (touched && warning) {
10+
if ((touched || validateOnMount) && warning) {
1111
return { validated: 'warning' };
1212
}
1313

packages/pf4-component-mapper/src/files/checkbox.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ import { Checkbox as Pf4Checkbox } from '@patternfly/react-core';
77
import IsRequired from '../common/is-required';
88

99
const SingleCheckbox = (props) => {
10-
const { label, isRequired, helperText, meta, description, input, isReadOnly, isDisabled, id, FormGroupProps, ...rest } = useFieldApi(props);
10+
const {
11+
label,
12+
isRequired,
13+
helperText,
14+
meta,
15+
validateOnMount,
16+
description,
17+
input,
18+
isReadOnly,
19+
isDisabled,
20+
id,
21+
FormGroupProps,
22+
...rest
23+
} = useFieldApi(props);
1124
return (
1225
<FormGroup
1326
isRequired={isRequired}
1427
helperText={helperText}
1528
meta={meta}
29+
validateOnMount={validateOnMount}
1630
description={description}
1731
hideLabel
1832
id={id || input.name}
@@ -33,6 +47,7 @@ const SingleCheckbox = (props) => {
3347

3448
SingleCheckbox.propTypes = {
3549
label: PropTypes.node,
50+
validateOnMount: PropTypes.bool,
3651
isReadOnly: PropTypes.bool,
3752
isRequired: PropTypes.bool,
3853
helperText: PropTypes.node,

packages/pf4-component-mapper/src/files/date-picker.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,49 @@ import FormGroup from '../common/form-group';
66
import showError from '../common/show-error';
77

88
const DatePicker = (props) => {
9-
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, FormGroupProps, ...rest } = useFieldApi(
10-
props
11-
);
9+
const {
10+
label,
11+
isRequired,
12+
helperText,
13+
meta,
14+
validateOnMount,
15+
description,
16+
hideLabel,
17+
input,
18+
isReadOnly,
19+
isDisabled,
20+
id,
21+
FormGroupProps,
22+
...rest
23+
} = useFieldApi(props);
1224
return (
1325
<FormGroup
1426
label={label}
1527
isRequired={isRequired}
1628
helperText={helperText}
1729
meta={meta}
30+
validateOnMount={validateOnMount}
1831
description={description}
1932
hideLabel={hideLabel}
2033
id={id || input.name}
2134
FormGroupProps={FormGroupProps}
2235
>
23-
<TextInput {...input} {...showError(meta)} {...rest} type="date" id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} />
36+
<TextInput
37+
{...input}
38+
{...showError(meta, validateOnMount)}
39+
{...rest}
40+
type="date"
41+
id={id || input.name}
42+
isReadOnly={isReadOnly}
43+
isDisabled={isDisabled}
44+
/>
2445
</FormGroup>
2546
);
2647
};
2748

2849
DatePicker.propTypes = {
2950
label: PropTypes.node,
51+
validateOnMount: PropTypes.bool,
3052
isReadOnly: PropTypes.bool,
3153
isRequired: PropTypes.bool,
3254
helperText: PropTypes.node,

packages/pf4-component-mapper/src/files/dual-list-select.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const DualList = (props) => {
1313
isRequired,
1414
helperText,
1515
meta,
16+
validateOnMount,
1617
description,
1718
hideLabel,
1819
id,
@@ -81,6 +82,7 @@ const DualList = (props) => {
8182
isRequired={isRequired}
8283
helperText={helperText}
8384
meta={meta}
85+
validateOnMount={validateOnMount}
8486
description={description}
8587
hideLabel={hideLabel}
8688
id={id || input.name}
@@ -109,6 +111,7 @@ const DualList = (props) => {
109111

110112
DualList.propTypes = {
111113
label: PropTypes.node,
114+
validateOnMount: PropTypes.bool,
112115
isRequired: PropTypes.bool,
113116
helperText: PropTypes.node,
114117
description: PropTypes.node,

packages/pf4-component-mapper/src/files/radio.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,20 @@ const Radio = ({ name, options, type, ...props }) => {
3535
* You cannot assign type radio to PF4 radio buttons input. It will break and will not set input value, only checked property
3636
* It has to be reqular input and we have change the radio value manully to the option value
3737
*/
38-
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, FormGroupProps } = useFieldApi({
38+
const {
39+
label,
40+
isRequired,
41+
helperText,
42+
meta,
43+
validateOnMount,
44+
description,
45+
hideLabel,
46+
input,
47+
isReadOnly,
48+
isDisabled,
49+
id,
50+
FormGroupProps
51+
} = useFieldApi({
3952
name,
4053
...props
4154
});
@@ -45,6 +58,7 @@ const Radio = ({ name, options, type, ...props }) => {
4558
isRequired={isRequired}
4659
helperText={helperText}
4760
meta={meta}
61+
validateOnMount={validateOnMount}
4862
description={description}
4963
hideLabel={hideLabel}
5064
id={id || input.name}
@@ -59,6 +73,7 @@ const Radio = ({ name, options, type, ...props }) => {
5973

6074
Radio.propTypes = {
6175
label: PropTypes.node,
76+
validateOnMount: PropTypes.bool,
6277
isReadOnly: PropTypes.bool,
6378
isRequired: PropTypes.bool,
6479
helperText: PropTypes.node,

packages/pf4-component-mapper/src/files/select.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@ import FormGroup from '../common/form-group';
55
import DataDrivenSelect from '../common/select/select';
66

77
const Select = (props) => {
8-
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, FormGroupProps, ...rest } = useFieldApi(
9-
props
10-
);
8+
const {
9+
label,
10+
isRequired,
11+
helperText,
12+
meta,
13+
validateOnMount,
14+
description,
15+
hideLabel,
16+
input,
17+
isReadOnly,
18+
isDisabled,
19+
id,
20+
FormGroupProps,
21+
...rest
22+
} = useFieldApi(props);
1123
return (
1224
<FormGroup
1325
label={label}
1426
isRequired={isRequired}
1527
helperText={helperText}
1628
meta={meta}
29+
validateOnMount={validateOnMount}
1730
description={description}
1831
hideLabel={hideLabel}
1932
id={id || input.name}
@@ -26,6 +39,7 @@ const Select = (props) => {
2639

2740
Select.propTypes = {
2841
label: PropTypes.node,
42+
validateOnMount: PropTypes.bool,
2943
isReadOnly: PropTypes.bool,
3044
isRequired: PropTypes.bool,
3145
helperText: PropTypes.node,

packages/pf4-component-mapper/src/files/slider.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@ import FormGroup from '../common/form-group';
55
import { Slider as PF4Slider } from '@patternfly/react-core';
66

77
const Slider = (props) => {
8-
const { label, isRequired, helperText, meta, description, input, isReadOnly, isDisabled, id, FormGroupProps, ...rest } = useFieldApi(props);
8+
const {
9+
label,
10+
isRequired,
11+
helperText,
12+
meta,
13+
validateOnMount,
14+
description,
15+
input,
16+
isReadOnly,
17+
isDisabled,
18+
id,
19+
FormGroupProps,
20+
...rest
21+
} = useFieldApi(props);
922

1023
return (
1124
<FormGroup
1225
label={label}
1326
isRequired={isRequired}
1427
helperText={helperText}
1528
meta={meta}
29+
validateOnMount={validateOnMount}
1630
description={description}
1731
id={id || input.name}
1832
FormGroupProps={FormGroupProps}
@@ -24,6 +38,7 @@ const Slider = (props) => {
2438

2539
Slider.propTypes = {
2640
label: PropTypes.node,
41+
validateOnMount: PropTypes.bool,
2742
isReadOnly: PropTypes.bool,
2843
isRequired: PropTypes.bool,
2944
helperText: PropTypes.node,

packages/pf4-component-mapper/src/files/switch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Switch = (props) => {
1313
isRequired,
1414
helperText,
1515
meta,
16+
validateOnMount,
1617
description,
1718
input,
1819
isReadOnly,
@@ -29,6 +30,7 @@ const Switch = (props) => {
2930
isRequired={isRequired}
3031
helperText={helperText}
3132
meta={meta}
33+
validateOnMount={validateOnMount}
3234
description={description}
3335
hideLabel
3436
id={id || input.name}
@@ -48,6 +50,7 @@ const Switch = (props) => {
4850

4951
Switch.propTypes = {
5052
label: PropTypes.node,
53+
validateOnMount: PropTypes.bool,
5154
isReadOnly: PropTypes.bool,
5255
isRequired: PropTypes.bool,
5356
helperText: PropTypes.node,

0 commit comments

Comments
 (0)