-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LabeledField: integrate with field components and fixes (#2399)
## 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: #2399
- Loading branch information
1 parent
5badeed
commit d9bc865
Showing
18 changed files
with
791 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-form": patch | ||
--- | ||
|
||
TextField and TextArea: Set `aria-required` if it is required |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
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'; | ||
|
||
<Meta | ||
title="Packages / Form / Overview" | ||
/> | ||
|
||
# Form | ||
|
||
`wonder-blocks-form` provides building blocks form Form components, including | ||
`wonder-blocks-form` provides building blocks for Form components, including | ||
TextField, TextArea, Choice, Checkbox, RadioButton, etc. | ||
|
||
|
||
## Best Practices | ||
|
||
### Form Field Labels | ||
|
||
- Use form field components with the `LabeledField` component. `LabeledField` | ||
provides a consistent pattern for things like the label, | ||
description, required indicator, and error message for a field. It will also | ||
provide unique ids automatically and wire up the proper attributes for | ||
accessibility. Use `LabeledField` for the following components: | ||
- `TextField` | ||
- `TextArea` | ||
- `SingleSelect` | ||
- `MultiSelect` | ||
- `SearchField` | ||
- If you are using the `CheckboxGroup` or `RadioGroup` components, use the | ||
`label` prop for an accessible field label. This uses `<fieldset>` and | ||
`<legend>` elements instead of a `<label>` since the label is for a group of | ||
related controls. See [Grouping Controls](https://www.w3.org/WAI/tutorials/forms/grouping/) | ||
for more details! | ||
- For custom implementations of field labels: | ||
- Make sure the id of the form field element is unique to the page | ||
- When using Wonder Blocks typography components for the form field label, | ||
set the `tag` prop to `label` to change the underlying element to render and | ||
use the `htmlFor` prop. Here is an example of a form field label using Wonder | ||
Blocks components `LabelMedium` and `TextArea`: | ||
|
||
<Canvas of={AccessibilityStories.FormLabelExample} /> | ||
|
||
### Error Validation | ||
|
||
- For fields like `TextField`, `TextArea`, and `SearchField` prefer setting | ||
`instantValidation=false`. This makes it so validation occurs on blur after a | ||
user is done interacting with a field. | ||
- Avoid disabling form submission buttons. There could be exceptions if the | ||
button is for one field. | ||
- If there are errors after a form is submitted, programatically move the user's | ||
focus to the first field with an error. | ||
|
||
Here is an example of validation behaviour in a form. It validates when a | ||
user is done filling out a field and also shows a validation error once | ||
the form is submitted (this simulates a backend validation error). When the form | ||
is submitted, the focus is also moved to the first field with an error. | ||
|
||
<Canvas of={LabeledFieldStories.Validation} /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.