Skip to content

Commit

Permalink
feat(Form): add resetFieldsValidation and setFieldError methods * 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tenphi committed Dec 27, 2024
1 parent 6e22100 commit dc88774
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .changeset/funny-oranges-hope.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@cube-dev/ui-kit': minor
---

Add `clearFieldsValidation()` and `setFieldError()` methods to form to replace deprecated `setFields()`.
Add `resetFieldsValidation()` and `setFieldError()` methods to form to replace deprecated `setFields()`.
4 changes: 2 additions & 2 deletions src/components/form/Form/submit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe('<Form />', () => {

// Clear all validation errors
await act(async () => {
formInstance.clearFieldsValidation();
formInstance.resetFieldsValidation();
});

await waitFor(() => {
Expand All @@ -308,7 +308,7 @@ describe('<Form />', () => {

// Clear validation error for specific field
await act(async () => {
formInstance.clearFieldsValidation(['test']);
formInstance.resetFieldsValidation(['test']);
});

await waitFor(() => {
Expand Down
10 changes: 7 additions & 3 deletions src/components/form/Form/use-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export class CubeFormInstance<
this.forceReRender();
}

clearFieldsValidation(names?: (keyof T & string)[], skipRender?: boolean) {
resetFieldsValidation(names?: (keyof T & string)[], skipRender?: boolean) {
(names || Object.keys(this.fields)).forEach((name) => {
const field = this.getFieldInstance(name);

Expand All @@ -439,10 +439,14 @@ export class CubeFormInstance<
}
}

setFieldError(name: keyof T & string, error: string, skipRender?: boolean) {
setFieldError(
name: keyof T & string,
error: ReactNode,
skipRender?: boolean,
) {
const field = this.getFieldInstance(name);

if (!field || !error.trim()) return;
if (!field || !error) return;

field.errors = [error];
field.status = 'invalid';
Expand Down

0 comments on commit dc88774

Please sign in to comment.