Skip to content

Fix The accessibility attribute aria-invalid has an error message instead of a 'true' value #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
validateFormWithHighPriority(initialValues.current);
}
}
}
}}
}, [
enableReinitialize,
props.initialValues,
Expand Down Expand Up @@ -915,6 +915,13 @@ export function useFormik<Values extends FormikValues = FormikValues>({
onChange: handleChange,
onBlur: handleBlur,
};

// Add aria-invalid attribute with value 'true' when field has an error
const error = getIn(state.errors, name);
if (error && getIn(state.touched, name)) {
field['aria-invalid'] = 'true';
}

if (isAnObject) {
const {
type,
Expand Down Expand Up @@ -942,7 +949,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
}
return field;
},
[handleBlur, handleChange, state.values]
[handleBlur, handleChange, state.values, state.errors, state.touched]
);

const dirty = React.useMemo(
Expand Down Expand Up @@ -1216,4 +1223,4 @@ function useEventCallback<T extends (...args: any[]) => any>(fn: T): T {
(...args: any[]) => ref.current.apply(void 0, args),
[]
) as T;
}
}