-
Notifications
You must be signed in to change notification settings - Fork 94
refactor: move input to app #18819
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
refactor: move input to app #18819
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0666d9a
moved Datepicker to lib
adamhaeger a09dac2
merge
adamhaeger 6aa43c2
fix
adamhaeger ba1ac3c
changed paths update
adamhaeger 4cdd85b
extracted mocks to __mocks__
adamhaeger 0a0d7b6
Merge branch 'main' into refactor/move-Datepicker-to-app
adamhaeger 731f2a2
importing from @app
adamhaeger 3124efd
cleanup after code review
adamhaeger cca3019
Merge branch 'main' into refactor/move-Datepicker-to-app
adamhaeger d4e361d
fixed broken imports
adamhaeger ca3d464
moved Input to lib
adamhaeger 5402916
Merge branch 'refactor/move-Datepicker-to-app' into refactor/move-Inp…
adamhaeger da05e62
merge
adamhaeger 789ae87
Merge branch 'refactor/move-Datepicker-to-app' into refactor/move-Inp…
adamhaeger fe95528
removed unused IGridStyling.xl size
adamhaeger bf79f00
moved constants to layout component
adamhaeger 061cf17
merge and fix broken test
adamhaeger 4d707fc
fixed unused
adamhaeger 1bfea13
Merge branch 'main' into refactor/move-Input-to-app
adamhaeger 2bc469d
Merge branch 'main' into refactor/move-Input-to-app
adamhaeger e2248b9
Merge branch 'main' into refactor/move-Input-to-app
adamhaeger 610ef1c
merge
adamhaeger 0828395
cleanup after code review
adamhaeger 7c93f76
Merge branch 'refactor/move-Input-to-app' of https://github.com/Altin…
adamhaeger 69fab81
Merge branch 'main' into refactor/move-Input-to-app
adamhaeger 2232c5e
moved FormattedInput and NumericInput to sepearate files
adamhaeger 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
18 changes: 18 additions & 0 deletions
18
libs/form-component/src/app-components/Input/FormattedInput.test.tsx
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,18 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { FormattedInput } from './FormattedInput'; | ||
|
|
||
| describe('FormattedInput', () => { | ||
| it('formats the value according to the pattern', () => { | ||
| render( | ||
| <FormattedInput | ||
| aria-label='Phone' | ||
| format='### ## ###' | ||
| value='12345678' | ||
| onValueChange={() => {}} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Phone' })).toHaveValue('123 45 678'); | ||
| }); | ||
| }); |
12 changes: 12 additions & 0 deletions
12
libs/form-component/src/app-components/Input/FormattedInput.tsx
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,12 @@ | ||
| import React from 'react'; | ||
| import { PatternFormat } from 'react-number-format'; | ||
| import type { PatternFormatProps } from 'react-number-format'; | ||
|
|
||
| import { Input } from './Input'; | ||
| import type { InputProps } from './Input'; | ||
|
|
||
| export function FormattedInput( | ||
| props: Omit<PatternFormatProps, 'customInput' | 'size'> & InputProps, | ||
| ) { | ||
| return <PatternFormat {...props} customInput={Input} />; | ||
| } |
File renamed without changes.
69 changes: 69 additions & 0 deletions
69
libs/form-component/src/app-components/Input/Input.stories.tsx
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,69 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
|
|
||
| import { Input } from './Input'; | ||
|
|
||
| const meta = { | ||
| title: 'AppComponents/Input', | ||
| component: Input, | ||
| args: { | ||
| label: 'First name', | ||
| }, | ||
| } satisfies Meta<typeof Input>; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Preview: Story = {}; | ||
|
|
||
| export const WithPlaceholder: Story = { | ||
| args: { | ||
| placeholder: 'Type your name', | ||
| }, | ||
| }; | ||
|
|
||
| export const WithPrefixAndSuffix: Story = { | ||
| args: { | ||
| label: 'Amount', | ||
| prefix: 'NOK', | ||
| suffix: ',-', | ||
| }, | ||
| }; | ||
|
|
||
| export const Search: Story = { | ||
| args: { | ||
| label: 'Search', | ||
| type: 'search', | ||
| }, | ||
| }; | ||
|
|
||
| export const WithError: Story = { | ||
| args: { | ||
| error: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const ReadOnly: Story = { | ||
| args: { | ||
| value: 'Ada Lovelace', | ||
| readOnly: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const TextOnly: Story = { | ||
| args: { | ||
| value: 'Ada Lovelace', | ||
| textonly: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const WithCharacterLimit: Story = { | ||
| args: { | ||
| label: 'Short bio', | ||
| characterLimit: { | ||
| limit: 50, | ||
| under: 'characters remaining', | ||
| over: 'characters too many', | ||
| }, | ||
| }, | ||
| }; | ||
109 changes: 109 additions & 0 deletions
109
libs/form-component/src/app-components/Input/Input.test.tsx
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,109 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| import { Input } from './Input'; | ||
|
|
||
| describe('Input', () => { | ||
| it('renders a textbox with a visible label', () => { | ||
| render(<Input label='First name' />); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'First name' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('uses aria-label as the accessible name', () => { | ||
| render(<Input aria-label='Search' />); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Search' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('uses aria-labelledby as the accessible name', () => { | ||
| render( | ||
| <> | ||
| <span id='ssn-label'>Social security number</span> | ||
| <Input aria-labelledby='ssn-label' /> | ||
| </>, | ||
| ); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Social security number' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('forwards change events', async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<Input aria-label='Name' onChange={onChange} />); | ||
|
|
||
| await user.type(screen.getByRole('textbox', { name: 'Name' }), 'Ada'); | ||
|
|
||
| expect(onChange).toHaveBeenCalled(); | ||
|
adamhaeger marked this conversation as resolved.
|
||
| expect(screen.getByRole('textbox', { name: 'Name' })).toHaveValue('Ada'); | ||
| }); | ||
|
|
||
| it('renders a read-only input', () => { | ||
| render(<Input aria-label='Name' value='Locked' readOnly onChange={() => {}} />); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Name' })).toHaveAttribute('readonly'); | ||
| }); | ||
|
|
||
| it('marks the input as invalid when an error is passed', () => { | ||
| render(<Input aria-label='Name' error='Required' />); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Name' })).toHaveAttribute('aria-invalid', 'true'); | ||
| }); | ||
|
|
||
| it('renders prefix and suffix as already-translated strings', () => { | ||
|
adamhaeger marked this conversation as resolved.
|
||
| render(<Input aria-label='Amount' prefix='NOK' suffix='per month' />); | ||
|
|
||
| expect(screen.getByText('NOK')).toBeInTheDocument(); | ||
| expect(screen.getByText('per month')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders a placeholder', () => { | ||
| render(<Input aria-label='Search' placeholder='Type to search' />); | ||
|
|
||
| expect(screen.getByPlaceholderText('Type to search')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows a character counter when characterLimit is set', () => { | ||
| render( | ||
| <Input | ||
| aria-label='Bio' | ||
| value='Hi' | ||
| onChange={() => {}} | ||
| characterLimit={{ limit: 10, under: 'characters left', over: 'too many characters' }} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByText(/characters left/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('hides the character counter for read-only inputs', () => { | ||
| render( | ||
| <Input | ||
| aria-label='Bio' | ||
| value='Hi' | ||
| readOnly | ||
| onChange={() => {}} | ||
| characterLimit={{ limit: 10, under: 'characters left', over: 'too many characters' }} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.queryByText(/characters left/i)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| describe('textonly', () => { | ||
| it('renders the value as plain text instead of an input', () => { | ||
| render(<Input aria-label='Name' textonly value='Ada Lovelace' onChange={() => {}} />); | ||
|
|
||
| expect(screen.getByText('Ada Lovelace')).toBeInTheDocument(); | ||
| expect(screen.queryByRole('textbox')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders nothing when the value is empty', () => { | ||
| const { container } = render( | ||
| <Input aria-label='Name' textonly value='' onChange={() => {}} />, | ||
| ); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
| }); | ||
| }); | ||
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
18 changes: 18 additions & 0 deletions
18
libs/form-component/src/app-components/Input/NumericInput.test.tsx
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,18 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { NumericInput } from './NumericInput'; | ||
|
|
||
| describe('NumericInput', () => { | ||
| it('formats the value with a thousand separator', () => { | ||
| render( | ||
| <NumericInput | ||
| aria-label='Amount' | ||
| thousandSeparator=' ' | ||
| value='1234567' | ||
| onValueChange={() => {}} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByRole('textbox', { name: 'Amount' })).toHaveValue('1 234 567'); | ||
| }); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
libs/form-component/src/app-components/Input/NumericInput.tsx
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,10 @@ | ||
| import React from 'react'; | ||
| import { NumericFormat } from 'react-number-format'; | ||
| import type { NumericFormatProps } from 'react-number-format'; | ||
|
|
||
| import { Input } from './Input'; | ||
| import type { InputProps } from './Input'; | ||
|
|
||
| export function NumericInput(props: Omit<NumericFormatProps, 'customInput' | 'size'> & InputProps) { | ||
| return <NumericFormat {...props} customInput={Input} />; | ||
| } |
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 @@ | ||
| export { Input } from './Input'; | ||
| export type { InputProps } from './Input'; | ||
| export { FormattedInput } from './FormattedInput'; | ||
| export { NumericInput } from './NumericInput'; | ||
|
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.