Skip to content

Commit c3530ab

Browse files
authored
feat(Form): new methods (#1093)
1 parent fe66c81 commit c3530ab

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

.changeset/form-qol-methods.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": minor
3+
---
4+
5+
Add `getFieldNames`, `isFieldDirty`, `getDirtyFieldNames`, `getValidFieldNames`, and `getInvalidFieldNames` methods to `CubeFormInstance`.

src/components/form/Form/use-form.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export class CubeFormInstance<
5858
this.createField = this.createField.bind(this);
5959
this.isFieldInvalid = this.isFieldInvalid.bind(this);
6060
this.isFieldTouched = this.isFieldTouched.bind(this);
61+
this.isFieldDirty = this.isFieldDirty.bind(this);
62+
this.getFieldNames = this.getFieldNames.bind(this);
63+
this.getDirtyFieldNames = this.getDirtyFieldNames.bind(this);
64+
this.getValidFieldNames = this.getValidFieldNames.bind(this);
65+
this.getInvalidFieldNames = this.getInvalidFieldNames.bind(this);
6166
this.setFields = this.setFields.bind(this);
6267
}
6368

@@ -326,27 +331,46 @@ export class CubeFormInstance<
326331
return !!field.touched;
327332
}
328333

334+
isFieldDirty<Name extends keyof T & string>(name: Name): boolean {
335+
const field = this.getFieldInstance(name);
336+
337+
if (!field) return false;
338+
339+
return !isEqual(field.value, this.defaultValues[name]);
340+
}
341+
342+
getFieldNames(): (keyof T & string)[] {
343+
return Object.keys(this.fields) as (keyof T & string)[];
344+
}
345+
346+
getDirtyFieldNames(): (keyof T & string)[] {
347+
return this.getFieldNames().filter((name) => this.isFieldDirty(name));
348+
}
349+
350+
getValidFieldNames(): (keyof T & string)[] {
351+
return this.getFieldNames().filter((name) => this.isFieldValid(name));
352+
}
353+
354+
getInvalidFieldNames(): (keyof T & string)[] {
355+
return this.getFieldNames().filter((name) => this.isFieldInvalid(name));
356+
}
357+
329358
get isTouched(): boolean {
330359
return Object.values(this.fields).some((field) => field?.touched);
331360
}
332361

333362
get isDirty(): boolean {
334-
return Object.values(this.fields).some((field) => {
335-
return field && field.name
336-
? JSON.stringify(field?.value) !==
337-
JSON.stringify(this.defaultValues[field?.name])
338-
: false;
339-
});
363+
return this.getDirtyFieldNames().length > 0;
340364
}
341365

342366
/**
343367
* True if all fields are verified and valid
344368
* IMPORTANT: This is not the same as `!isInvalid`, because it also checks if all fields are verified.
345369
*/
346370
get isValid(): boolean {
347-
return Object.values(this.fields).every((field) => {
348-
return field?.status === 'valid';
349-
});
371+
return Object.values(this.fields).every(
372+
(field) => field?.status === 'valid',
373+
);
350374
}
351375

352376
/**
@@ -355,9 +379,9 @@ export class CubeFormInstance<
355379
* one field is verified and invalid.
356380
*/
357381
get isInvalid(): boolean {
358-
return Object.values(this.fields).some((field) => {
359-
return field?.status === 'invalid';
360-
});
382+
return Object.values(this.fields).some(
383+
(field) => field?.status === 'invalid',
384+
);
361385
}
362386

363387
getFieldError<Name extends keyof T & string>(name: Name): ReactNode[] {

0 commit comments

Comments
 (0)