-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Clears errors on an input when the value changes
BREAKING CHANGE: 🧨 Clears errors by default on value change. Must pass `clearErrorsOnChange: false` to `useInertiaInput` to disable behavior
- Loading branch information
Showing
5 changed files
with
120 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react' | ||
import { useInertiaInput } from '../src' | ||
|
||
interface TestInputProps { | ||
name: string | ||
model?: string | ||
clearErrorsOnChange?: boolean | ||
} | ||
|
||
const TestInput = ({ name, model, clearErrorsOnChange = true }: TestInputProps) => { | ||
const { inputName, inputId, value, setValue } = useInertiaInput<string>({ | ||
name, | ||
model, | ||
clearErrorsOnChange, | ||
}) | ||
|
||
return ( | ||
<input | ||
type="text" | ||
role="input" | ||
name={ inputName } | ||
id={ inputId } | ||
value={ value } | ||
onChange={ e => setValue(e.target.value) } | ||
/> | ||
) | ||
} | ||
|
||
export default TestInput |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react' | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Form, useForm } from '../src/Form' | ||
import TestInput from './TestInput' | ||
|
||
const FormContextTest = () => { | ||
const { errors, setError } = useForm() | ||
|
||
const handleClick = e => { | ||
e.preventDefault() | ||
setError('name', 'Error') | ||
} | ||
|
||
return ( | ||
<> | ||
<button onClick={ handleClick } role="button" aria-label="error" /> | ||
<div data-testid="errors">{ JSON.stringify(errors) }</div> | ||
</> | ||
) | ||
} | ||
|
||
describe ('useInertiaInput', () => { | ||
describe('With clearErrorsOnChange = true', () => { | ||
it('clears errors on an input when the value changes ', () => { | ||
render( | ||
<Form role="form" to="/" data={ { name: '' } } remember={ false }> | ||
<FormContextTest /> | ||
<TestInput name="name" /> | ||
</Form>, | ||
) | ||
|
||
const errorButton = screen.getByRole('button', { name: 'error' }) | ||
const input = screen.getByRole('input') | ||
|
||
fireEvent.click(errorButton) | ||
expect(screen.getByTestId('errors')).toHaveTextContent('{"name":"Error"}') | ||
|
||
fireEvent.change(input, { target: { value: 'something' } }) | ||
expect(screen.getByTestId('errors')).toHaveTextContent('{}') | ||
}) | ||
|
||
}) | ||
|
||
describe('With clearErrorsOnChange = true', () => { | ||
it('doesn\'t clear errors on an input when the value changes', () => { | ||
render( | ||
<Form role="form" to="/" data={ { name: '' } } remember={ false }> | ||
<FormContextTest /> | ||
<TestInput name="name" clearErrorsOnChange={ false } /> | ||
</Form>, | ||
) | ||
|
||
const errorButton = screen.getByRole('button', { name: 'error' }) | ||
const input = screen.getByRole('input') | ||
|
||
fireEvent.click(errorButton) | ||
expect(screen.getByTestId('errors')).toHaveTextContent('{"name":"Error"}') | ||
|
||
fireEvent.change(input, { target: { value: 'something' } }) | ||
expect(screen.getByTestId('errors')).toHaveTextContent('{"name":"Error"}') | ||
}) | ||
|
||
}) | ||
}) |