-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create phone number component tckt-363 (#382)
* feat: create phone number pattern tckt-363 * feat: create phone number pattern edit form tckt-363 * test: add tests phone number config tckt-363 * refactor: Clear old error message on session object when updating valid submission data and pass previous value into React component tckt-363 Co-authored-by: Daniel Naab [email protected] * test: update tests tckt-363 * feat: update error state and message for phone pattern tckt-363 --------- Co-authored-by: kalasgarov <[email protected]>
- Loading branch information
1 parent
5ee1c8c
commit 5ee8954
Showing
22 changed files
with
693 additions
and
31 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
76 changes: 76 additions & 0 deletions
76
packages/design/src/Form/components/PhoneNumber/PhoneNumber.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 { PhoneNumberPattern } from './PhoneNumber.js'; | ||
|
||
const meta: Meta<typeof PhoneNumberPattern> = { | ||
title: 'patterns/PhoneNumberPattern', | ||
component: PhoneNumberPattern, | ||
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 PhoneNumberPattern> = { | ||
args: { | ||
phoneId: 'phone', | ||
label: 'Phone number', | ||
required: false, | ||
}, | ||
}; | ||
|
||
export const WithRequired: StoryObj<typeof PhoneNumberPattern> = { | ||
args: { | ||
phoneId: 'phone', | ||
label: 'Phone number', | ||
required: true, | ||
}, | ||
}; | ||
|
||
export const WithError: StoryObj<typeof PhoneNumberPattern> = { | ||
args: { | ||
phoneId: 'phone', | ||
label: 'Phone number with error', | ||
required: true, | ||
error: { | ||
type: 'custom', | ||
message: 'This field has an error', | ||
}, | ||
}, | ||
}; | ||
|
||
export const WithHint: StoryObj<typeof PhoneNumberPattern> = { | ||
args: { | ||
phoneId: 'phone', | ||
label: 'Phone number', | ||
hint: '10-digit, U.S. only, for example 999-999-9999', | ||
required: true, | ||
}, | ||
}; | ||
|
||
export const WithHintAndError: StoryObj<typeof PhoneNumberPattern> = { | ||
args: { | ||
phoneId: 'phone', | ||
label: 'Phone number', | ||
hint: '10-digit, U.S. only, for example 999-999-9999', | ||
required: true, | ||
error: { | ||
type: 'custom', | ||
message: 'This field has an error', | ||
}, | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/design/src/Form/components/PhoneNumber/PhoneNumber.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 './PhoneNumber.stories.js'; | ||
|
||
describeStories(meta, stories); |
57 changes: 57 additions & 0 deletions
57
packages/design/src/Form/components/PhoneNumber/PhoneNumber.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,57 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import { type PhoneNumberProps } from '@atj/forms'; | ||
import { type PatternComponent } from '../../index.js'; | ||
|
||
export const PhoneNumberPattern: PatternComponent<PhoneNumberProps> = ({ | ||
phoneId, | ||
hint, | ||
label, | ||
required, | ||
error, | ||
value, | ||
}) => { | ||
const { register } = useFormContext(); | ||
|
||
return ( | ||
<fieldset className="usa-fieldset"> | ||
<div className={classNames('usa-form-group margin-top-2')}> | ||
<label | ||
className={classNames('usa-label', { | ||
'usa-label--error': error, | ||
})} | ||
id={`input-message-${phoneId}`} | ||
htmlFor={phoneId} | ||
> | ||
{label} | ||
{required && <span className="required-indicator">*</span>} | ||
</label> | ||
{hint && ( | ||
<div className="usa-hint" id="primaryPnHint"> | ||
{hint} | ||
</div> | ||
)} | ||
{error && ( | ||
<div | ||
className="usa-error-message" | ||
id={`input-error-message-${phoneId}`} | ||
role="alert" | ||
> | ||
{error.message} | ||
</div> | ||
)} | ||
<input | ||
className={classNames('usa-input', { | ||
'usa-input--error': error, | ||
})} | ||
id={phoneId} | ||
type="tel" | ||
defaultValue={value} | ||
{...register(phoneId, { required })} | ||
aria-describedby="primaryPnHint" | ||
/> | ||
</div> | ||
</fieldset> | ||
); | ||
}; |
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 { PhoneNumberPattern } from './PhoneNumber.js'; | ||
|
||
export default PhoneNumberPattern; |
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
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
104 changes: 104 additions & 0 deletions
104
packages/design/src/FormManager/FormEdit/components/PhoneNumberPatternEdit.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,104 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { expect, userEvent } from '@storybook/test'; | ||
import { within } from '@testing-library/react'; | ||
|
||
import { type PhoneNumberPattern } from '@atj/forms'; | ||
import { createPatternEditStoryMeta } from './common/story-helper.js'; | ||
import FormEdit from '../index.js'; | ||
import { enLocale as message } from '@atj/common'; | ||
|
||
const pattern: PhoneNumberPattern = { | ||
id: 'phone-number-1', | ||
type: 'phone-number', | ||
data: { | ||
label: message.patterns.phoneNumber.displayName, | ||
required: false, | ||
hint: undefined, | ||
}, | ||
}; | ||
|
||
const storyConfig: Meta = { | ||
title: 'Edit components/PhoneNumberPattern', | ||
...createPatternEditStoryMeta({ | ||
pattern, | ||
}), | ||
} as Meta<typeof FormEdit>; | ||
|
||
export default storyConfig; | ||
|
||
export const Basic: StoryObj<typeof FormEdit> = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const updatedLabel = 'Phone Number update'; | ||
const updatedHint = 'Updated hint for Phone Number'; | ||
|
||
await userEvent.click( | ||
canvas.getByText(message.patterns.phoneNumber.displayName) | ||
); | ||
|
||
const labelInput = canvas.getByLabelText( | ||
message.patterns.phoneNumber.fieldLabel | ||
); | ||
await userEvent.clear(labelInput); | ||
await userEvent.type(labelInput, updatedLabel); | ||
|
||
const hintInput = canvas.getByLabelText( | ||
message.patterns.phoneNumber.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 = 'Phone Number update'; | ||
|
||
await userEvent.click( | ||
canvas.getByText(message.patterns.phoneNumber.displayName) | ||
); | ||
|
||
const labelInput = canvas.getByLabelText( | ||
message.patterns.phoneNumber.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.phoneNumber.hintLabel) | ||
).toBeNull(); | ||
}, | ||
}; | ||
|
||
export const Error: StoryObj<typeof FormEdit> = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await userEvent.click( | ||
canvas.getByText(message.patterns.phoneNumber.displayName) | ||
); | ||
|
||
const labelInput = canvas.getByLabelText( | ||
message.patterns.phoneNumber.fieldLabel | ||
); | ||
await userEvent.clear(labelInput); | ||
labelInput.blur(); | ||
|
||
await expect( | ||
await canvas.findByText( | ||
message.patterns.selectDropdown.errorTextMustContainChar | ||
) | ||
).toBeInTheDocument(); | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/design/src/FormManager/FormEdit/components/PhoneNumberPatternEdit.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 './PhoneNumberPatternEdit.stories.js'; | ||
|
||
describeStories(meta, stories); |
Oops, something went wrong.