-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#271] only clear errors for the changed field
- Loading branch information
1 parent
04d285a
commit 56a0b79
Showing
3 changed files
with
39 additions
and
8 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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
export type FormErrors<Fields> = Array<{ | ||
export type FormErrors<Fields extends string> = Array<{ | ||
readonly field: Fields; | ||
readonly message: string; | ||
}> | ||
|
||
export function firstError(field: string, errors: Readonly<FormErrors<unknown>>): string { | ||
export function firstError<Fields extends string>(field: Fields, errors: Readonly<FormErrors<Fields>>): string { | ||
return errors.find((e) => e.field === field)?.message ?? '' | ||
} | ||
|
||
export function removeError<Fields extends string>(field: Fields, errors: Readonly<FormErrors<Fields>>): Readonly<FormErrors<Fields>> { | ||
return errors.filter((e) => e.field !== field) | ||
} | ||
|
||
export function removeErrorFromState<Fields extends string>(field: Fields): (errors: Readonly<FormErrors<Fields>>) => Readonly<FormErrors<Fields>> { | ||
return (errors: Readonly<FormErrors<Fields>>) => removeError<Fields>(field, errors) | ||
} |
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