-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rename methods to improve clarity of field-level or form-level
- Loading branch information
1 parent
d0dcc12
commit 6fcfe73
Showing
21 changed files
with
372 additions
and
313 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
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
export * from './get-error'; | ||
export * from './get-value'; | ||
export * from './get-field-error'; | ||
export * from './get-field-value'; | ||
export * from './is-dirty'; | ||
export * from './is-field-dirty'; | ||
export * from './is-field-invalid'; | ||
export * from './is-field-touched'; | ||
export * from './is-invalid'; | ||
export * from './is-touched'; | ||
export * from './set-dirty'; | ||
export * from './set-touched'; | ||
export * from './set-field-dirty'; | ||
export * from './set-field-touched'; | ||
export * from './set-field-value'; | ||
export * from './set-value'; | ||
export * from './unset-dirty'; | ||
export * from './unset-field-dirty'; | ||
export * from './unset-field-touched'; | ||
export * from './unset-touched'; |
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 |
---|---|---|
@@ -1,40 +1,15 @@ | ||
import type { FieldPath, FormState, FormValue } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
import type { FormState, FormValue } from '../types'; | ||
|
||
/** | ||
* Whether the form or form field is dirty. | ||
* Whether the form is dirty. | ||
* | ||
* A form or form field is dirty if: | ||
* * The direct field path has been modified. | ||
* * Any of its descendant field paths have been modified. | ||
* A form is dirty if any of its field paths are dirty. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath (optional) If provided, checks whether the field is dirty; | ||
* otherwise checks the form as a whole. | ||
*/ | ||
export function isDirty<V extends FormValue, P extends FieldPath<V>>( | ||
export function isDirty<V extends FormValue>( | ||
formState: FormState<V>, | ||
fieldPath?: P, | ||
): boolean { | ||
const { formValue } = formState; | ||
const { dirtyFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (!fieldPath) { | ||
return dirtyFieldPaths.size > 0; | ||
} | ||
|
||
if (dirtyFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const dirtyFieldPath of dirtyFieldPaths) { | ||
if (dirtyFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
return dirtyFieldPaths.size > 0; | ||
} |
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,35 @@ | ||
import type { FieldPath, FormState, FormValue } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
|
||
/** | ||
* Whether the form field is dirty. | ||
* | ||
* A form field is dirty if: | ||
* * The direct field path has been modified. | ||
* * Any of its descendant field paths have been modified. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath The field to check for whether it's dirty. | ||
*/ | ||
export function isFieldDirty<V extends FormValue, P extends FieldPath<V>>( | ||
formState: FormState<V>, | ||
fieldPath: P, | ||
): boolean { | ||
const { value: formValue } = formState; | ||
const { dirtyFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (dirtyFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const dirtyFieldPath of dirtyFieldPaths) { | ||
if (dirtyFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} |
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,35 @@ | ||
import type { FieldPath, FormState, FormValue } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
|
||
/** | ||
* Whether the form field is invalid. | ||
* | ||
* A form field is invalid if: | ||
* * The direct field path is invalid. | ||
* * Any of its descendant field paths are invalid. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath The field to check for whether it's invalid. | ||
*/ | ||
export function isFieldInvalid< | ||
V extends FormValue, | ||
P extends FieldPath<V>, | ||
>(formState: FormState<V>, fieldPath: P): boolean { | ||
const { value: formValue } = formState; | ||
const { invalidFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (invalidFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const invalidFieldPath of invalidFieldPaths) { | ||
if (invalidFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} |
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,35 @@ | ||
import type { FormValue, FormState, FieldPath } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
|
||
/** | ||
* Whether the form field is touched. | ||
* | ||
* A form field is touched if: | ||
* * The direct field path has been touched. | ||
* * Any of its descendant field paths have been touched. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath The field to check for whether it's touched. | ||
*/ | ||
export function isFieldTouched< | ||
V extends FormValue, | ||
P extends FieldPath<V>, | ||
>(formState: FormState<V>, fieldPath: P): boolean { | ||
const { value: formValue } = formState; | ||
const { touchedFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (touchedFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const touchedFieldPath of touchedFieldPaths) { | ||
if (touchedFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} |
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 |
---|---|---|
@@ -1,40 +1,15 @@ | ||
import type { FieldPath, FormState, FormValue } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
import type { FormState, FormValue } from '../types'; | ||
|
||
/** | ||
* Whether the form or form field is invalid. | ||
* Whether the form is invalid. | ||
* | ||
* A form or form field is invalid if: | ||
* * The direct field path is invalid. | ||
* * Any of its descendant field paths are invalid. | ||
* A form is invalid if any of its field paths are invalid. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath (optional) If provided, checks whether the field is invalid; | ||
* otherwise checks the form as a whole. | ||
*/ | ||
export function isInvalid<V extends FormValue, P extends FieldPath<V>>( | ||
export function isInvalid<V extends FormValue>( | ||
formState: FormState<V>, | ||
fieldPath?: P, | ||
): boolean { | ||
const { formValue } = formState; | ||
const { invalidFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (!fieldPath) { | ||
return invalidFieldPaths.size > 0; | ||
} | ||
|
||
if (invalidFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const invalidFieldPath of invalidFieldPaths) { | ||
if (invalidFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
return invalidFieldPaths.size > 0; | ||
} |
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 |
---|---|---|
@@ -1,40 +1,15 @@ | ||
import type { FormValue, FormState, FieldPath } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
import type { FormState, FormValue } from '../types'; | ||
|
||
/** | ||
* Whether the form or form field is touched. | ||
* Whether the form is touched. | ||
* | ||
* A form or form field is touched if: | ||
* * The direct field path has been touched. | ||
* * Any of its descendant field paths have been touched. | ||
* A form is touched if any of its field paths are touched. | ||
* | ||
* @param formState Form state. | ||
* @param fieldPath (optional) If provided, checks whether the field is touched; | ||
* otherwise checks the form as a whole. | ||
*/ | ||
export function isTouched<V extends FormValue, P extends FieldPath<V>>( | ||
export function isTouched<V extends FormValue>( | ||
formState: FormState<V>, | ||
fieldPath?: P, | ||
): boolean { | ||
const { formValue } = formState; | ||
const { touchedFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (!fieldPath) { | ||
return touchedFieldPaths.size > 0; | ||
} | ||
|
||
if (touchedFieldPaths.has(fieldPath)) { | ||
return true; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const touchedFieldPath of touchedFieldPaths) { | ||
if (touchedFieldPath.startsWith(fieldPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
return touchedFieldPaths.size > 0; | ||
} |
Oops, something went wrong.