From d9bc865b924aea41f1effd7f88c6e6da0cb8185f Mon Sep 17 00:00:00 2001 From: Bea Esguerra Date: Thu, 19 Dec 2024 14:43:16 -0700 Subject: [PATCH] LabeledField: integrate with field components and fixes (#2399) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: Putting LabeledField work together with field updates and addressing some issues. Fixes include: TextField and TextArea: - Set `aria-required` based on `required` prop - Always clear error state onChange if instantValidation is false so that external error messages set using the `error` prop can still be cleared (This is useful for clearing any backend errors when the input value changes) LabeledField updates: - Let `required` prop be a boolean or string so it can be passed down to the field prop - Conditionally set spacing above the error message depending on if there is an error message - Set `required`, `error` and `light` props for LabeledField and field component if it is set on either LabeledField or field component - Rename `error` prop to `errorMessage` (This technically isn't a breaking change since the batch of labeled field work hasn't been released yet, labeled field work is currently in the `feature/labeled-field` branch) Stories/docs: - Add docs on best practices for forms and fields - Add docs around LabeledTextField deprecation in favour of LabeledField + TextField - Update LabeledField examples and docs. Includes examples for validation and moving focus to the first field that has an error message after form submission Issue: WB-1783 ## Test plan: 1. Review new docs: - LabeledField (`?path=/docs/packages-labeledfield--docs`) - LabeledTextField (`/?path=/docs/packages-form-labeledtextfield-deprecated--docs`) - Form Best Practices (`?path=/docs/packages-form-overview--docs`) 2. Test LabeledField with Field Validation: Confirm validation works as expected - LabeledField Required story (`?path=/story/packages-labeledfield--required`) - Tabbing through fields without entering anything - Submitting without filling in fields - Inputting/selecting a value and removing it - LabeledField Validation story (`?path=/story/packages-labeledfield--validation`) - Triggering validation errors - Clearing validation errors when selected value changes - Submitting the form to see an example for backend errors Note: Did some preliminary testing with LabeledField and new validation features. Different screen reader and browser combinations had different behaviours around announcing errors so I created WB-1842 for more testing + documentation. Author: beaesguerra Reviewers: beaesguerra, jandrade Required Reviewers: Approved By: jandrade Checks: ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Lint / Lint (ubuntu-latest, 20.x), ✅ Test / Test (ubuntu-latest, 20.x, 2/2), ✅ Test / Test (ubuntu-latest, 20.x, 1/2), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ⏭️ Chromatic - Skip on Release PR (changesets), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️ dependabot Pull Request URL: https://github.com/Khan/wonder-blocks/pull/2399 --- .changeset/chilly-mirrors-add.md | 5 + .changeset/metal-maps-move.md | 5 + .changeset/slow-otters-crash.md | 5 + .changeset/smart-grapes-serve.md | 5 + .changeset/spicy-rivers-marry.md | 5 + __docs__/wonder-blocks-form/_overview_.mdx | 52 ++- __docs__/wonder-blocks-form/accessibility.mdx | 34 -- .../accessibility.stories.tsx | 2 +- .../labeled-text-field.stories.tsx | 74 +++- .../labeled-field.stories.tsx | 334 ++++++++++++++++-- .../components/__tests__/text-area.test.tsx | 72 +++- .../components/__tests__/text-field.test.tsx | 68 +++- .../src/components/text-area.tsx | 1 + .../src/components/text-field.tsx | 1 + .../src/hooks/use-field-validation.ts | 14 +- .../__tests__/labeled-field.test.tsx | 219 ++++++++---- .../src/components/labeled-field.tsx | 89 +++-- .../tsconfig-build.json | 1 - 18 files changed, 791 insertions(+), 195 deletions(-) create mode 100644 .changeset/chilly-mirrors-add.md create mode 100644 .changeset/metal-maps-move.md create mode 100644 .changeset/slow-otters-crash.md create mode 100644 .changeset/smart-grapes-serve.md create mode 100644 .changeset/spicy-rivers-marry.md delete mode 100644 __docs__/wonder-blocks-form/accessibility.mdx diff --git a/.changeset/chilly-mirrors-add.md b/.changeset/chilly-mirrors-add.md new file mode 100644 index 000000000..4d82d7b9d --- /dev/null +++ b/.changeset/chilly-mirrors-add.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-blocks-form": patch +--- + +TextField and TextArea: Set `aria-required` if it is required diff --git a/.changeset/metal-maps-move.md b/.changeset/metal-maps-move.md new file mode 100644 index 000000000..f4a0999de --- /dev/null +++ b/.changeset/metal-maps-move.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-blocks-form": patch +--- + +TextField and TextArea validation: Always clear error message onChange if instantValidation=false so externally set error state can still be cleared diff --git a/.changeset/slow-otters-crash.md b/.changeset/slow-otters-crash.md new file mode 100644 index 000000000..0888206d7 --- /dev/null +++ b/.changeset/slow-otters-crash.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-blocks-labeled-field": patch +--- + +Set required, error and light props for LabeledField and field component if it is set on either LabeledField or field component diff --git a/.changeset/smart-grapes-serve.md b/.changeset/smart-grapes-serve.md new file mode 100644 index 000000000..5835a09e5 --- /dev/null +++ b/.changeset/smart-grapes-serve.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-blocks-labeled-field": patch +--- + +Use `errorMessage` prop instead of `error` prop for consistency (`error` prop is used for boolean props in form field components). diff --git a/.changeset/spicy-rivers-marry.md b/.changeset/spicy-rivers-marry.md new file mode 100644 index 000000000..be94ec3c4 --- /dev/null +++ b/.changeset/spicy-rivers-marry.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-blocks-labeled-field": patch +--- + +LabeledField: Let `required` prop be a boolean or string so it can be passed down to the field prop diff --git a/__docs__/wonder-blocks-form/_overview_.mdx b/__docs__/wonder-blocks-form/_overview_.mdx index 78c7e733d..cdece2c84 100644 --- a/__docs__/wonder-blocks-form/_overview_.mdx +++ b/__docs__/wonder-blocks-form/_overview_.mdx @@ -1,4 +1,6 @@ -import {Meta} from "@storybook/blocks"; +import {Meta, Story, Canvas} from "@storybook/blocks"; +import * as AccessibilityStories from './accessibility.stories'; +import * as LabeledFieldStories from '../wonder-blocks-labeled-field/labeled-field.stories'; ` and +`` elements instead of a `