Skip to content

Commit

Permalink
fix(Form): handle final form errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zouxuoz committed Oct 10, 2018
1 parent 3bdaddd commit b5c0f9a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/atoms/dataEntry/CheckboxField/CheckboxField.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React from 'react';

import type { InputType, MetaType } from '../formTypes';
import * as formUtils from '../../../utils/forms';
import { Checkbox } from '../Checkbox';
import { FormField } from '../Form/FormField';

Expand All @@ -22,9 +23,9 @@ const CheckboxField = ({
meta = {},
...rest
}: CheckboxFieldProps) => {
const { error, touched } = meta;
const { name, value, onChange, onBlur, onFocus } = input;
const hasError = !!error && !!touched;
const { name, value, onChange, onFocus, onBlur } = input;

const hasError = formUtils.hasError(meta);

return (
<FormField input={ input } meta={ meta }>
Expand Down
6 changes: 4 additions & 2 deletions src/atoms/dataEntry/Form/FormField.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React from 'react';

import { createStyledTag, createTheme } from '../../../utils';
import * as formUtils from '../../../utils/forms';
import type { InputType, MetaType } from '../formTypes';

type FormFieldProps = {
Expand Down Expand Up @@ -67,8 +68,9 @@ const FormField = ({
hideErrorLabel,
...rest
}: FormFieldProps) => {
const { error, touched } = meta;
const hasError = !!error && !!touched;
const hasError = formUtils.hasError(meta);
const error = formUtils.getError(meta);

const hasLabel = !!label;

return (
Expand Down
5 changes: 3 additions & 2 deletions src/atoms/dataEntry/InputField/InputField.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import { createTheme } from '../../../utils';
import * as formUtils from '../../../utils/forms';
import { Input } from '../Input';
import { FormField } from '../Form/FormField';

Expand Down Expand Up @@ -51,9 +52,9 @@ const InputField = ({
stretch,
...rest
}: InputFieldProps) => {
const { error, touched } = meta;
const { name, value, onChange, onFocus, onBlur } = input;
const hasError = !!error && !!touched;

const hasError = formUtils.hasError(meta);

return (
<FormField label={ label } stretch={ stretch } direction={ direction } hideErrorLabel={ hideErrorLabel } input={ input } meta={ meta }>
Expand Down
5 changes: 3 additions & 2 deletions src/atoms/dataEntry/RadioGroupField/RadioGroupField.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React from 'react';

import type { InputType, MetaType } from '../formTypes';
import * as formUtils from '../../../utils/forms';
import { Radio } from '../Radio';
import { FormField } from '../Form/FormField';

Expand All @@ -24,9 +25,9 @@ const RadioGroupField = ({
meta = {},
...rest
}: RadioGroupFieldProps) => {
const { error, touched } = meta;
const { name, value, onChange } = input;
const hasError = !!error && !!touched;

const hasError = formUtils.hasError(meta);

return (
<FormField hideErrorLabel={ hideErrorLabel } input={ input } meta={ meta }>
Expand Down
5 changes: 3 additions & 2 deletions src/atoms/dataEntry/SelectField/SelectField.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import { createTheme } from '../../../utils';
import * as formUtils from '../../../utils/forms';
import { Select } from '../Select';
import { FormField } from '../Form/FormField';

Expand Down Expand Up @@ -38,8 +39,8 @@ function SelectField({
...rest
}: SelectFieldProps) {
const { name, value, onChange } = input;
const { error, touched } = meta;
const hasError = !!error && !!touched;

const hasError = formUtils.hasError(meta);

const selectedOption = options.find((option) => option.value === value);

Expand Down
13 changes: 13 additions & 0 deletions src/utils/forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const hasError = (meta: Object) => {
const { submitError, dirtySinceLastSubmit, error, touched } = meta;

return (!!error || !!submitError) && !!touched && !dirtySinceLastSubmit;
};

export const getError = (meta: Object) => {
if (hasError(meta)) {
const { submitError, error } = meta;

return error || submitError;
}
};

0 comments on commit b5c0f9a

Please sign in to comment.