-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): Native validation tracking and syncing (#41)
* feat: basic validity state tracking by form * fix: return empty array if no errors and clone retrieved errors * fix: touched state pathless form * test: add form validation tests * fix: always show errors on mounted with native errors * feat: added displayError utility * feat: allow display error to recieve custom message externally * feat: sync errors to native input validity
- Loading branch information
Showing
22 changed files
with
466 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { renderSetup } from '@test-utils/index'; | ||
import { useErrorDisplay } from './useErrorDisplay'; | ||
import { useFormField } from './useFormField'; | ||
|
||
test('displays field errors only if they are touched', async () => { | ||
const { setErrors, isValid, errorMessage, displayError, setTouched } = await renderSetup(() => { | ||
const field = useFormField({ initialValue: 'bar' }); | ||
const { displayError } = useErrorDisplay(field); | ||
|
||
return { ...field, displayError }; | ||
}); | ||
|
||
expect(isValid.value).toBe(true); | ||
expect(errorMessage.value).toBe(''); | ||
expect(displayError()).toBe(''); | ||
|
||
setErrors('error'); | ||
expect(errorMessage.value).toBe('error'); | ||
expect(displayError()).toBe(''); | ||
expect(isValid.value).toBe(false); | ||
|
||
setTouched(true); | ||
expect(displayError()).toBe('error'); | ||
}); | ||
|
||
test('controls display of custom messages as well', async () => { | ||
const { isValid, displayError, setTouched } = await renderSetup(() => { | ||
const field = useFormField({ initialValue: 'bar' }); | ||
const { displayError } = useErrorDisplay(field); | ||
|
||
return { ...field, displayError }; | ||
}); | ||
|
||
expect(displayError('custom error')).toBe(''); | ||
expect(isValid.value).toBe(true); | ||
|
||
setTouched(true); | ||
expect(displayError('custom error')).toBe('custom error'); | ||
}); |
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,11 @@ | ||
import { FormField } from './useFormField'; | ||
|
||
export function useErrorDisplay(field: FormField<any>) { | ||
function displayError(msg?: string) { | ||
const error = msg || field.errorMessage.value; | ||
|
||
return field.isTouched.value ? error : ''; | ||
} | ||
|
||
return { displayError }; | ||
} |
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
Oops, something went wrong.