-
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.
Add
instantValidation
prop for TextField & share logic with TextArea (
#2357) ## Summary: - Adds `instantValidation` prop to `TextField` component - Create `useFieldValidation` hook to share validation logic between `TextField` and `TextArea` - `TextArea` is updated to use `useFieldValidation` hook. - Both `TextArea` and `TextField` will now validate on initialization if `value` is not empty and it is not disabled - Stories: Use utility functions for validate functions Issue: WB-1781 ## Test plan: - TextField - Instant Validation docs are reviewed (`/?path=/docs/packages-form-textfield--docs#instant%20validation`) - Instant Validation works as expected (see docs for expected behaviour) (`/?path=/story/packages-form-textfield--instant-validation`) - TextArea - 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 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: #2357
- Loading branch information
1 parent
21f6779
commit 486c6a8
Showing
11 changed files
with
1,982 additions
and
187 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,9 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-form": minor | ||
--- | ||
|
||
- `TextField` | ||
- Add `instantValidation` prop | ||
- No longer calls `validate` prop if the field is disabled during initialization and on change | ||
- `TextArea` | ||
- Validate the value during initialization if the field is not disabled |
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,23 @@ | ||
/** | ||
* Checks if a value is a valid email and returns an error message if it is invalid. | ||
* @param value the email to validate | ||
* @returns An error message if there is a validation error | ||
*/ | ||
export const validateEmail = (value: string) => { | ||
const emailRegex = /^[^@\s]+@[^@\s.]+\.[^@.\s]+$/; | ||
if (!emailRegex.test(value)) { | ||
return "Please enter a valid email"; | ||
} | ||
}; | ||
|
||
/** | ||
* Checks if a value is a valid phone number and returns an error message if it is invalid. | ||
* @param value the phone number to validate | ||
* @returns An error message if there is a validation error | ||
*/ | ||
export const validatePhoneNumber = (value: string) => { | ||
const telRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; | ||
if (!telRegex.test(value)) { | ||
return "Invalid US telephone number"; | ||
} | ||
}; |
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
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.