Skip to content

Commit

Permalink
Add instantValidation prop for TextArea (#2355)
Browse files Browse the repository at this point in the history
## Summary:
- Adds instantValidation prop so there is an option to validate on blur. If instantValidation is false, the error is also cleared on change
- If textarea is required and instantValidation is false, validate whenever the user tabs away from the field

Issue: WB-1781

Next: I'll work on refactoring TextField to a function component first and then will add the same `instantValidation` prop to TextField

## Test plan:
- Instant Validation docs are reviewed (`/?path=/docs/packages-form-textarea--docs#instant%20validation`)
- Instant Validation works as expected (see docs for expected behaviour) (`/?path=/story/packages-form-textarea--instant-validation`)

Author: beaesguerra

Reviewers: beaesguerra, jandrade, marcysutton

Required Reviewers:

Approved By: jandrade

Checks: ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test / Test (ubuntu-latest, 20.x, 2/2), ✅ Test / Test (ubuntu-latest, 20.x, 1/2), ✅ Lint / Lint (ubuntu-latest, 20.x), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ⏭️  Chromatic - Skip on Release PR (changesets), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️  dependabot

Pull Request URL: #2355
  • Loading branch information
beaesguerra authored Nov 13, 2024
1 parent cdcfe1b commit 9ed7bd5
Show file tree
Hide file tree
Showing 4 changed files with 585 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-moles-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-form": minor
---

Adds `instantValidation` prop for TextArea
93 changes: 93 additions & 0 deletions __docs__/wonder-blocks-form/text-area.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,99 @@ ErrorFromPropAndValidation.parameters = {
},
};

/**
* The `instantValidation` prop controls when validation is triggered. Validation
* is triggered if the `validate` or `required` props are set.
*
* It is preferred to set `instantValidation` to `false` so that the user isn't
* shown an error until they are done with a field. Note: if `instantValidation`
* is not explicitly set, it defaults to `true` since this is the current
* behaviour of existing usage. Validation on blur needs to be opted in.
*
* Validation is triggered:
* - On mount if the `value` prop is not empty
* - If `instantValidation` is `true`, validation occurs `onChange` (default)
* - If `instantValidation` is `false`, validation occurs `onBlur`
*
* When `required` is set to `true`:
* - If `instantValidation` is `true`, the required error message is shown after
* a value is cleared
* - If `instantValidation` is `false`, the required error message is shown
* whenever the user tabs away from the required field
*/
export const InstantValidation: StoryComponentType = {
args: {
validate(value: string) {
const emailRegex = /^[^@\s]+@[^@\s.]+\.[^@.\s]+$/;
if (!emailRegex.test(value)) {
return "Please enter a valid email";
}
},
},
render: (args) => {
return (
<View style={{gap: spacing.small_12}}>
<LabelSmall htmlFor="instant-validation-true-not-required">
Validation on mount if there is a value
</LabelSmall>
<ControlledTextArea
{...args}
id="instant-validation-true-not-required"
value="invalid"
/>
<LabelSmall htmlFor="instant-validation-true-not-required">
Error shown immediately (instantValidation: true, required:
false)
</LabelSmall>
<ControlledTextArea
{...args}
id="instant-validation-true-not-required"
instantValidation={true}
/>
<LabelSmall htmlFor="instant-validation-false-not-required">
Error shown onBlur (instantValidation: false, required:
false)
</LabelSmall>
<ControlledTextArea
{...args}
id="instant-validation-false-not-required"
instantValidation={false}
/>

<LabelSmall htmlFor="instant-validation-true-required">
Error shown immediately after clearing the value
(instantValidation: true, required: true)
</LabelSmall>
<ControlledTextArea
{...args}
validate={undefined}
value="T"
id="instant-validation-true-required"
instantValidation={true}
required="Required"
/>
<LabelSmall htmlFor="instant-validation-false-required">
Error shown on blur if it is empty (instantValidation:
false, required: true)
</LabelSmall>
<ControlledTextArea
{...args}
validate={undefined}
id="instant-validation-false-required"
instantValidation={false}
required="Required"
/>
</View>
);
},
parameters: {
chromatic: {
// Disabling because this doesn't test anything visual.
disableSnapshot: true,
},
},
};

/**
* A required field will have error styling if the field is left blank. To
* observe this, type something into the field, backspace all the way,
Expand Down
Loading

0 comments on commit 9ed7bd5

Please sign in to comment.