Skip to content

Commit 9f0919d

Browse files
committed
feat: add showErrorOnTouched prop to the all fields
1 parent c965ecb commit 9f0919d

File tree

9 files changed

+70
-24
lines changed

9 files changed

+70
-24
lines changed

src/components/CheckboxField/CheckboxField.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type CheckboxFieldProps = {
2222
nowrap?: boolean,
2323
/** when true then stretch to the maximal width */
2424
stretch?: boolean,
25+
/** show error only for touched fields */
26+
showErrorOnTouched?: boolean,
2527
};
2628

2729
const CheckboxField = ({
@@ -32,13 +34,14 @@ const CheckboxField = ({
3234
disabled,
3335
color,
3436
nowrap,
37+
showErrorOnTouched,
3538
...rest
3639
}: CheckboxFieldProps) => {
3740
const { name, value, onChange, onFocus, onBlur } = input;
38-
const hasError = formUtils.hasError(meta);
41+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
3942

4043
return (
41-
<FormField { ...rest } input={ input } meta={ meta } stretch={ stretch }>
44+
<FormField { ...rest } input={ input } meta={ meta } stretch={ stretch } showErrorOnTouched={ showErrorOnTouched }>
4245
<Checkbox
4346
label={ label }
4447
name={ name }
@@ -56,4 +59,8 @@ const CheckboxField = ({
5659
);
5760
};
5861

62+
CheckboxField.defaultProps = {
63+
showErrorOnTouched: true,
64+
};
65+
5966
export { CheckboxField };

src/components/Form/FormField.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type FormFieldProps = {
1515
hideErrorLabel?: boolean,
1616
direction?: 'row' | 'column',
1717
meta?: MetaType,
18+
showErrorOnTouched?: boolean,
1819
};
1920

2021
const name = 'formField';
@@ -101,10 +102,11 @@ const FormField = ({
101102
note,
102103
children,
103104
hideErrorLabel,
105+
showErrorOnTouched,
104106
...rest
105107
}: FormFieldProps) => {
106-
const hasError = formUtils.hasError(meta);
107-
let error: any = formUtils.getError(meta);
108+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
109+
let error: any = formUtils.getError(meta, showErrorOnTouched);
108110

109111
const hasLabel = !!label;
110112

@@ -142,6 +144,7 @@ FormField.defaultProps = {
142144
hideErrorLabel: false,
143145
stretch: true,
144146
direction: 'column',
147+
showErrorOnTouched: true,
145148
};
146149

147150
export { FormField, theme, FormLabel, ControlErrorTag };

src/components/InputField/InputField.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type InputFieldProps = {
4545
min?: string | number,
4646
max?: string | number,
4747
mask?: string,
48+
/** show error only for touched fields */
49+
showErrorOnTouched?: boolean,
4850
};
4951

5052
const InputField = ({
@@ -74,11 +76,12 @@ const InputField = ({
7476
min,
7577
max,
7678
mask,
79+
showErrorOnTouched,
7780
...rest
7881
}: InputFieldProps) => {
7982
const { name, value, onChange, onFocus, onBlur } = input;
8083

81-
const hasError = formUtils.hasError(meta);
84+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
8285

8386
return (
8487
<FormField
@@ -89,6 +92,7 @@ const InputField = ({
8992
hideErrorLabel={ hideErrorLabel }
9093
input={ input }
9194
meta={ meta }
95+
showErrorOnTouched={ showErrorOnTouched }
9296
>
9397
<Input
9498
align={ align }
@@ -128,6 +132,7 @@ InputField.defaultProps = {
128132
type: 'text',
129133
input: {},
130134
meta: {},
135+
showErrorOnTouched: true,
131136
};
132137

133138
export { InputField };

src/components/RadioGroupField/RadioGroupField.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type RadioGroupFieldProps = {
3030
alignContent?: PropLayout,
3131
alignItems?: PropLayoutStretch,
3232
flexWrap?: 'wrap' | 'nowrap' | 'wrap-reverse',
33+
/** show error only for touched fields */
34+
showErrorOnTouched?: boolean,
3335
};
3436

3537
const RadioGroupField = ({
@@ -46,14 +48,15 @@ const RadioGroupField = ({
4648
alignContent,
4749
alignItems,
4850
flexWrap,
51+
showErrorOnTouched,
4952
...rest
5053
}: RadioGroupFieldProps) => {
5154
const { name, value, onChange } = input;
5255

53-
const hasError = formUtils.hasError(meta);
56+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
5457

5558
return (
56-
<FormField { ...rest } hideErrorLabel={ hideErrorLabel } input={ input } meta={ meta }>
59+
<FormField { ...rest } hideErrorLabel={ hideErrorLabel } input={ input } meta={ meta } showErrorOnTouched={ showErrorOnTouched }>
5760
<Radio.Group
5861
direction={ direction }
5962
gap={ gap }
@@ -75,4 +78,8 @@ const RadioGroupField = ({
7578
);
7679
};
7780

81+
RadioGroupField.defaultProps = {
82+
showErrorOnTouched: true,
83+
};
84+
7885
export { RadioGroupField };

src/components/SelectField/SelectField.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,27 @@ type SelectFieldProps = {
2323
inputValue?: string,
2424
/** callback to control search value */
2525
onInputChange?: (value: string, event?: SyntheticInputEvent<HTMLInputElement>) => void,
26+
/** show error only for touched fields */
27+
showErrorOnTouched?: boolean,
2628
};
2729

2830
class SelectField extends React.Component<SelectFieldProps> {
31+
static defaultProps = {
32+
showErrorOnTouched: true,
33+
};
34+
2935
collectFormFieldProps() {
30-
const { meta, input, stretch, label } = this.props;
36+
const { meta, input, stretch, label, showErrorOnTouched } = this.props;
3137

32-
return { meta, input, stretch, label };
38+
return { meta, input, stretch, label, showErrorOnTouched };
3339
}
3440

3541
collectSelectProps() {
3642
const {
37-
input = {}, meta, placeholder, options, multiple, isMulti, creatable, stretch, filterOption, getOptionValue, getOptionLabel,
43+
input = {}, meta, placeholder, options, multiple, isMulti, creatable, stretch, filterOption, getOptionValue, getOptionLabel, showErrorOnTouched,
3844
} = this.props;
3945

40-
const hasError = formUtils.hasError(meta);
46+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
4147

4248
return {
4349
...this.props,

src/components/SwitchField/SwitchField.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ type SwitchFieldProps = {
1111
label?: string,
1212
input: InputType,
1313
meta?: MetaType,
14+
/** show error only for touched fields */
15+
showErrorOnTouched?: boolean,
1416
};
1517

16-
const SwitchField = ({ label, input, meta, ...rest }: SwitchFieldProps) => {
18+
const SwitchField = ({ label, input, meta, showErrorOnTouched, ...rest }: SwitchFieldProps) => {
1719
const {
1820
name,
1921
value,
2022
onChange,
2123
onFocus,
2224
onBlur,
2325
} = input;
24-
const hasError = formUtils.hasError(meta);
26+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
2527

2628
return (
27-
<FormField { ...rest } input={ input } meta={ meta }>
29+
<FormField { ...rest } input={ input } meta={ meta } showErrorOnTouched={ showErrorOnTouched }>
2830
<Switch
2931
name={ name }
3032
label={ label }
@@ -38,5 +40,9 @@ const SwitchField = ({ label, input, meta, ...rest }: SwitchFieldProps) => {
3840
);
3941
};
4042

43+
SwitchField.defaultProps = {
44+
showErrorOnTouched: true,
45+
};
46+
4147
export { SwitchField };
4248

src/components/TextAreaField/TextAreaField.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type TextAreaFieldProps = {
2121
input: Object,
2222
/** form meta object */
2323
meta?: Object,
24+
/** show error only for touched fields */
25+
showErrorOnTouched?: boolean,
2426
};
2527

2628

@@ -32,14 +34,15 @@ function TextAreaField({
3234
placeholder,
3335
rows,
3436
stretch,
37+
showErrorOnTouched,
3538
...rest
3639
}: TextAreaFieldProps) {
3740
const { name, value, onChange } = input;
3841

39-
const hasError = formUtils.hasError(meta);
42+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
4043

4144
return (
42-
<FormField label={ label } stretch={ stretch } input={ input } meta={ meta }>
45+
<FormField label={ label } stretch={ stretch } input={ input } meta={ meta } showErrorOnTouched={ showErrorOnTouched }>
4346
<TextArea
4447
{ ...rest }
4548
hasError={ hasError }
@@ -55,4 +58,8 @@ function TextAreaField({
5558
);
5659
}
5760

61+
TextAreaField.defaultProps = {
62+
showErrorOnTouched: true,
63+
};
64+
5865
export { TextAreaField };

src/components/TreeSelectField/TreeSelectField.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ type TreeSelectFieldProps = {
2020
input: InputType,
2121
/** form meta object */
2222
meta?: MetaType,
23+
/** show error only for touched fields */
24+
showErrorOnTouched?: boolean,
2325
};
2426

2527
class TreeSelectField extends React.Component<TreeSelectFieldProps> {
28+
static defaultProps = {
29+
showErrorOnTouched: true,
30+
};
2631

2732
onChange = (currenNode: Object, selectedNodes: Object[]) => {
2833
const { input } = this.props;
@@ -32,15 +37,15 @@ class TreeSelectField extends React.Component<TreeSelectFieldProps> {
3237
};
3338

3439
collectFormFieldProps() {
35-
const { meta, input, stretch, label } = this.props;
40+
const { meta, input, stretch, label, showErrorOnTouched } = this.props;
3641

37-
return { meta, input, stretch, label };
42+
return { meta, input, stretch, label, showErrorOnTouched };
3843
}
3944

4045
collectSelectProps() {
41-
const { input, meta, placeholder, options, stretch } = this.props;
46+
const { input, meta, placeholder, options, stretch, showErrorOnTouched } = this.props;
4247

43-
const hasError = formUtils.hasError(meta);
48+
const hasError = formUtils.hasError(meta, showErrorOnTouched);
4449

4550
return {
4651
...this.props,

src/utils/forms.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export const hasError = (meta: Object = {}) => {
1+
export const hasError = (meta: Object = {}, showErrorOnTouched: boolean = true) => {
22
const { submitError, dirtySinceLastSubmit, error, touched } = meta;
33

4-
return (!!error || (!!submitError && !dirtySinceLastSubmit)) && !!touched;
4+
return (!!error || (!!submitError && !dirtySinceLastSubmit)) && (!!showErrorOnTouched ? !!touched : true);
55
};
66

7-
export const getError = (meta: Object) => {
8-
if (hasError(meta)) {
7+
export const getError = (meta: Object, showErrorOnTouched: boolean = true) => {
8+
if (hasError(meta, showErrorOnTouched)) {
99
const { submitError, error } = meta;
1010

1111
return error || submitError;

0 commit comments

Comments
 (0)