-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: create ssn input component tckt-364 #386
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
590a133
feat: cretae ssn pattern tckt-364
19d796c
feat: create ssn pattern edit form tckt-364
6d4bfa4
feat: create ssn icon and phone icons tckt-364
7818501
feat: add ssn input and schema validations tckt-364
036165a
test: add tests for ssn input and schema validations tckt-364
b397f71
feat: update ssn validation criteria based on USWDS recommendations
f02ec84
feat: address accessibility issues tckt-364
b8956ea
feat: improve SSN validation error messages for clarity tckt-364
a9f4e8b
feat: improve accessibility for ssn input tckt-364
c3d3745
feat: add input masking to guide correct entry of the Social Security…
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 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
76 changes: 76 additions & 0 deletions
76
packages/design/src/Form/components/SocialSecurityNumber/SocialSecurityNumber.stories.tsx
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,76 @@ | ||
import React from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { type Meta, type StoryObj } from '@storybook/react'; | ||
|
||
import { SocialSecurityNumberPattern } from './SocialSecurityNumber.js'; | ||
|
||
const meta: Meta<typeof SocialSecurityNumberPattern> = { | ||
title: 'patterns/SocialSecurityNumberPattern', | ||
component: SocialSecurityNumberPattern, | ||
decorators: [ | ||
(Story, args) => { | ||
const FormDecorator = () => { | ||
const formMethods = useForm(); | ||
return ( | ||
<FormProvider {...formMethods}> | ||
<Story {...args} /> | ||
</FormProvider> | ||
); | ||
}; | ||
return <FormDecorator />; | ||
}, | ||
], | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
|
||
export const Default: StoryObj<typeof SocialSecurityNumberPattern> = { | ||
args: { | ||
ssnId: 'ssn', | ||
label: 'Social Security Number', | ||
required: false, | ||
}, | ||
}; | ||
|
||
export const WithRequired: StoryObj<typeof SocialSecurityNumberPattern> = { | ||
args: { | ||
ssnId: 'ssn', | ||
label: 'Social Security Number', | ||
required: true, | ||
}, | ||
}; | ||
|
||
export const WithError: StoryObj<typeof SocialSecurityNumberPattern> = { | ||
args: { | ||
ssnId: 'ssn', | ||
label: 'Social Security Number with error', | ||
required: true, | ||
error: { | ||
type: 'custom', | ||
message: 'This field has an error', | ||
}, | ||
}, | ||
}; | ||
|
||
export const WithHint: StoryObj<typeof SocialSecurityNumberPattern> = { | ||
args: { | ||
ssnId: 'ssn', | ||
label: 'Social Security Number', | ||
hint: 'For example, 555-11-0000', | ||
required: true, | ||
}, | ||
}; | ||
|
||
export const WithHintAndError: StoryObj<typeof SocialSecurityNumberPattern> = { | ||
args: { | ||
ssnId: 'ssn', | ||
label: 'Social Security Number', | ||
hint: 'For example, 555-11-0000', | ||
required: true, | ||
error: { | ||
type: 'custom', | ||
message: 'This field has an error', | ||
}, | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/design/src/Form/components/SocialSecurityNumber/SocialSecurityNumber.test.tsx
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,7 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describeStories } from '../../../test-helper.js'; | ||
import meta, * as stories from './SocialSecurityNumber.stories.js'; | ||
|
||
describeStories(meta, stories); |
67 changes: 67 additions & 0 deletions
67
packages/design/src/Form/components/SocialSecurityNumber/SocialSecurityNumber.tsx
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,67 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import { type SocialSecurityNumberProps } from '@atj/forms'; | ||
|
||
import { type PatternComponent } from '../../index.js'; | ||
|
||
const formatSSN = (value: string) => { | ||
const rawValue = value.replace(/[^\d]/g, ''); | ||
if (rawValue.length <= 3) return rawValue; | ||
if (rawValue.length <= 5) | ||
return `${rawValue.slice(0, 3)}-${rawValue.slice(3)}`; | ||
return `${rawValue.slice(0, 3)}-${rawValue.slice(3, 5)}-${rawValue.slice(5, 9)}`; | ||
}; | ||
|
||
export const SocialSecurityNumberPattern: PatternComponent< | ||
SocialSecurityNumberProps | ||
> = ({ ssnId, hint, label, required, error, value }) => { | ||
const { register, setValue } = useFormContext(); | ||
const errorId = `input-error-message-${ssnId}`; | ||
const hintId = `hint-${ssnId}`; | ||
|
||
const handleSSNChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const formattedSSN = formatSSN(e.target.value); | ||
setValue(ssnId, formattedSSN, { shouldValidate: true }); | ||
}; | ||
|
||
return ( | ||
<fieldset className="usa-fieldset"> | ||
<div className={classNames('usa-form-group margin-top-2')}> | ||
<label | ||
className={classNames('usa-label', { | ||
'usa-label--error': error, | ||
})} | ||
htmlFor={ssnId} | ||
> | ||
{label || 'Social Security Number'} | ||
{required && <span className="required-indicator">*</span>} | ||
</label> | ||
{hint && ( | ||
<div className="usa-hint" id={hintId}> | ||
{hint} | ||
</div> | ||
)} | ||
{error && ( | ||
<div className="usa-error-message" id={errorId} role="alert"> | ||
{error.message} | ||
</div> | ||
)} | ||
<input | ||
className={classNames('usa-input usa-input--xl', { | ||
'usa-input--error': error, | ||
})} | ||
id={ssnId} | ||
type="text" | ||
defaultValue={value} | ||
{...register(ssnId, { required })} | ||
onChange={handleSSNChange} | ||
aria-describedby={ | ||
`${hint ? `${hintId}` : ''}${error ? ` ${errorId}` : ''}`.trim() || | ||
undefined | ||
} | ||
/> | ||
</div> | ||
</fieldset> | ||
); | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/design/src/Form/components/SocialSecurityNumber/index.tsx
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,3 @@ | ||
import { SocialSecurityNumberPattern } from './SocialSecurityNumber.js'; | ||
|
||
export default SocialSecurityNumberPattern; |
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
90 changes: 90 additions & 0 deletions
90
...es/design/src/FormManager/FormEdit/components/SocialSecurityNumberPatternEdit.stories.tsx
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,90 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { expect, userEvent } from '@storybook/test'; | ||
import { within } from '@testing-library/react'; | ||
|
||
import { type SocialSecurityNumberPattern } from '@atj/forms'; | ||
import { createPatternEditStoryMeta } from './common/story-helper.js'; | ||
import FormEdit from '../index.js'; | ||
import { enLocale as message } from '@atj/common'; | ||
|
||
const pattern: SocialSecurityNumberPattern = { | ||
id: 'social-security-number-1', | ||
type: 'social-security-number', | ||
data: { | ||
label: message.patterns.ssn.displayName, | ||
required: false, | ||
hint: undefined, | ||
}, | ||
}; | ||
|
||
const storyConfig: Meta = { | ||
title: 'Edit components/SocialSecurityNumberPattern', | ||
...createPatternEditStoryMeta({ | ||
pattern, | ||
}), | ||
} as Meta<typeof FormEdit>; | ||
|
||
export default storyConfig; | ||
|
||
export const Basic: StoryObj<typeof FormEdit> = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const updatedLabel = 'Social Security Number update'; | ||
const updatedHint = 'Updated hint for Social Security Number'; | ||
|
||
await userEvent.click(canvas.getByText(message.patterns.ssn.displayName)); | ||
|
||
const labelInput = canvas.getByLabelText(message.patterns.ssn.fieldLabel); | ||
await userEvent.clear(labelInput); | ||
await userEvent.type(labelInput, updatedLabel); | ||
|
||
const hintInput = canvas.getByLabelText(message.patterns.ssn.hintLabel); | ||
await userEvent.clear(hintInput); | ||
await userEvent.type(hintInput, updatedHint); | ||
|
||
const form = labelInput?.closest('form'); | ||
form?.requestSubmit(); | ||
|
||
await expect(await canvas.findByText(updatedLabel)).toBeInTheDocument(); | ||
await expect(await canvas.findByText(updatedHint)).toBeInTheDocument(); | ||
}, | ||
}; | ||
|
||
export const WithoutHint: StoryObj<typeof FormEdit> = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const updatedLabel = 'Social Security Number update'; | ||
|
||
await userEvent.click(canvas.getByText(message.patterns.ssn.displayName)); | ||
|
||
const labelInput = canvas.getByLabelText(message.patterns.ssn.fieldLabel); | ||
await userEvent.clear(labelInput); | ||
await userEvent.type(labelInput, updatedLabel); | ||
|
||
const form = labelInput?.closest('form'); | ||
form?.requestSubmit(); | ||
|
||
await expect(await canvas.findByText(updatedLabel)).toBeInTheDocument(); | ||
await expect( | ||
await canvas.queryByLabelText(message.patterns.ssn.hintLabel) | ||
).toBeNull(); | ||
}, | ||
}; | ||
|
||
export const Error: StoryObj<typeof FormEdit> = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await userEvent.click(canvas.getByText(message.patterns.ssn.displayName)); | ||
|
||
const labelInput = canvas.getByLabelText(message.patterns.ssn.fieldLabel); | ||
await userEvent.clear(labelInput); | ||
labelInput.blur(); | ||
|
||
await expect( | ||
await canvas.findByText( | ||
message.patterns.selectDropdown.errorTextMustContainChar | ||
) | ||
).toBeInTheDocument(); | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
...ages/design/src/FormManager/FormEdit/components/SocialSecurityNumberPatternEdit.tests.tsx
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,7 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describeStories } from '../../../test-helper.js'; | ||
import meta, * as stories from './SocialSecurityNumberPatternEdit.js'; | ||
|
||
describeStories(meta, stories); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just opened a bug for this to address in a different sprint because it's a larger issue we have in various places in the application, but if we're doing the asterisk to indicate a required field, let's add
title="required"
to theabbr
tag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bug I opened for a future sprint for reference: #388