-
Notifications
You must be signed in to change notification settings - Fork 166
LG-6160: Add personal key validation behavior #6222
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d3a2b14
TypeScript-ify ValidatedField
aduth baf7a18
Rename ValidatedField to ValidatedFieldElement
aduth fc5dd00
Move ValidatedFieldElement out of index
aduth 7c530bf
Auto-register ValidatedFieldElement custom element
aduth 656a322
Add ValidatedField React component
aduth 36ba583
Enhance FormSteps to work with ValidatedFieldElement
aduth fbbea96
LG-6160: Add personal key validation behavior
aduth 44a9918
Type narrowing via typeof function
aduth 6d8e8e2
Use native checkValidity for setting custom validity
aduth 4f32ae0
Add specs for ValidatedField React component
aduth 8a8927b
Combine checkValidity + reportValidity
aduth a0818f6
Update spec stubbing
aduth ac0fbc3
Add spec for PersonalKeyInput validation
aduth a5b03ce
Add spec for PersonalKeyConfirmStep validation
aduth 0efc340
Submit FormSteps using context
aduth c49a383
Add failing spec for missing "Enter"-to-submit behavior
aduth b1ae97c
Add missing value to fake FormStepsContext
aduth 51336e7
Shim fake form for modal input submission-by-enter
aduth 5a8df6c
Remove unused import
aduth 532154e
Add (failling) regression spec for expected initial errors behavior
aduth d053069
Restore initial active error handling, field-level error clearing
aduth e38860d
Handle submit bubbling via empty form tag
aduth 4dd1263
Restore FormStepsContext default value
aduth 4110e4e
Update specs for new version of testing-library/user-event
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 +1 @@ | ||
| import { ValidatedField } from '@18f/identity-validated-field'; | ||
|
|
||
| customElements.define('lg-validated-field', ValidatedField); | ||
| import '@18f/identity-validated-field'; |
This file contains hidden or 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 hidden or 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,5 +1,5 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import type { RefCallback, FormEventHandler, FC } from 'react'; | ||
| import type { FormEventHandler, RefCallback, FC } from 'react'; | ||
| import { Alert } from '@18f/identity-components'; | ||
| import { useDidUpdateEffect, useIfStillMounted } from '@18f/identity-react-hooks'; | ||
| import RequiredValueMissingError from './required-value-missing-error'; | ||
|
|
@@ -30,7 +30,7 @@ interface FormStepRegisterFieldOptions { | |
| export type RegisterFieldCallback = ( | ||
| field: string, | ||
| options?: Partial<FormStepRegisterFieldOptions>, | ||
| ) => undefined | RefCallback<HTMLElement>; | ||
| ) => undefined | RefCallback<HTMLInputElement>; | ||
|
|
||
| export type OnErrorCallback = (error: Error, options?: { field?: string | null }) => void; | ||
|
|
||
|
|
@@ -87,7 +87,7 @@ interface FieldsRefEntry { | |
| /** | ||
| * Ref callback. | ||
| */ | ||
| refCallback: RefCallback<HTMLElement>; | ||
| refCallback: RefCallback<HTMLInputElement>; | ||
|
|
||
| /** | ||
| * Whether field is required. | ||
|
|
@@ -97,7 +97,7 @@ interface FieldsRefEntry { | |
| /** | ||
| * Element assigned by ref callback. | ||
| */ | ||
| element: HTMLElement | null; | ||
| element: HTMLInputElement | null; | ||
| } | ||
|
|
||
| interface FormStepsProps { | ||
|
|
@@ -195,7 +195,11 @@ function FormSteps({ | |
| const ifStillMounted = useIfStillMounted(); | ||
| useEffect(() => { | ||
| if (activeErrors.length && didSubmitWithErrors.current) { | ||
| getFieldActiveErrorFieldElement(activeErrors, fields.current)?.focus(); | ||
| const activeErrorFieldElement = getFieldActiveErrorFieldElement(activeErrors, fields.current); | ||
| if (activeErrorFieldElement) { | ||
| activeErrorFieldElement.reportValidity(); | ||
| activeErrorFieldElement.focus(); | ||
| } | ||
| } | ||
|
|
||
| didSubmitWithErrors.current = false; | ||
|
|
@@ -242,8 +246,19 @@ function FormSteps({ | |
| const { element, isRequired } = fields.current[key]; | ||
| const isActive = !!element; | ||
|
|
||
| if (isActive && isRequired && !values[key]) { | ||
| result = result.concat({ field: key, error: new RequiredValueMissingError() }); | ||
| let error: Error | undefined; | ||
| if (isActive) { | ||
| element.checkValidity(); | ||
|
|
||
| if (element.validationMessage) { | ||
| error = new Error(element.validationMessage); | ||
| } else if (isRequired && !values[key]) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future refactoring could see about eliminating this custom idea of |
||
| error = new RequiredValueMissingError(); | ||
| } | ||
| } | ||
|
|
||
| if (error) { | ||
| result = result.concat({ field: key, error }); | ||
| } | ||
|
|
||
| return result; | ||
|
|
@@ -275,8 +290,8 @@ function FormSteps({ | |
|
|
||
| const nextActiveErrors = getValidationErrors(); | ||
| setActiveErrors(nextActiveErrors); | ||
| didSubmitWithErrors.current = true; | ||
| if (nextActiveErrors.length) { | ||
| didSubmitWithErrors.current = true; | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -300,7 +315,7 @@ function FormSteps({ | |
| const isLastStep = stepIndex + 1 === steps.length; | ||
|
|
||
| return ( | ||
| <form ref={formRef} onSubmit={toNextStep}> | ||
| <form ref={formRef} onSubmit={toNextStep} noValidate> | ||
| {promptOnNavigate && Object.keys(values).length > 0 && <PromptOnNavigate />} | ||
| {stepErrors.map((error) => ( | ||
| <Alert key={error.message} type="error" className="margin-bottom-4"> | ||
|
|
||
This file contains hidden or 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 @@ | ||
| import './validated-field-element'; | ||
|
|
||
| export { default as ValidatedField } from './validated-field'; | ||
|
|
||
| export type { ValidatedFieldValidator } from './validated-field'; |
This file contains hidden or 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,5 +1,13 @@ | ||
| { | ||
| "name": "@18f/identity-validated-field", | ||
| "private": true, | ||
| "version": "1.0.0" | ||
| "version": "1.0.0", | ||
| "peerDependencies": { | ||
| "react": "^17.0.2" | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "react": { | ||
| "optional": true | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.