Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #303 from wellyshen/feature/remove-set-values-and-…
Browse files Browse the repository at this point in the history
…set-errors

Remove "setValues" and "setErrors" methods
  • Loading branch information
wellyshen authored Jan 5, 2021
2 parents 7a5b3a5 + 83c2474 commit 558fbd9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 191 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-lies-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Feat(useForm): remove `setValues` and `setErrors`
58 changes: 3 additions & 55 deletions docs/api-reference/use-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ We can configure the [mode of the built-in validation](../getting-started/valida

`boolean`

Tell React Cool Form to run validations on `change` events as well as the [setFieldValue](#setfieldvalue) and [setValues](#setvalues) methods. Default is `true`.
Tell React Cool Form to run validations on `change` events as well as the [setFieldValue](#setfieldvalue) method. Default is `true`.

### validateOnBlur

Expand All @@ -76,9 +76,7 @@ const returnValues = useForm({
onSubmit: async (values, options, e) => {
const {
formState, // The current form state, don't mutate it directly
setValues,
setFieldValue,
setErrors,
setFieldError,
validateForm,
validateField,
Expand All @@ -104,9 +102,7 @@ const returnValues = useForm({
onError: (errors, options, e) => {
const {
formState, // The current form state, don't mutate it directly
setValues,
setFieldValue,
setErrors,
setFieldError,
validateForm,
validateField,
Expand All @@ -132,9 +128,7 @@ const returnValues = useForm({
onReset: (values, options, e) => {
const {
formState, // The current form state, don't mutate it directly
setValues,
setFieldValue,
setErrors,
setFieldError,
validateForm,
validateField,
Expand Down Expand Up @@ -205,30 +199,6 @@ If you just want to validate the field, there's a shortcut for it:

This method provides us a performant way to use/read the form state. Check the [Form State](../getting-started/form-state) document to learn more.

### setValues

`(values: FormValues | Function, options?: Object) => void`

This method allows us to manually set the `values` of the [form state](../getting-started/form-state).

```js
const { setValues } = useForm();

setValues(
{ firstName: "Welly", lastName: "Shen" }, // It will replace the entire values object
{
shouldValidate: true, // (Default = "validateOnChange" option) Triggers form validation
touched: ["firstName"], // Sets fields as touched by passing their names
// touched: (allFieldNames) => allFieldNames, // A reverse way to set touched fields
dirty: ["firstName"], // Sets fields as dirty by passing their names
// dirty: (allFieldNames) => allFieldNames, // A reverse way to set dirty fields
}
);

// We can also pass a callback as the "values" parameter, similar to React's setState callback style
setValues((prevValues) => ({ ...prevValues, firstName: "Bella" }));
```

### setFieldValue

`(name: string, value?: any | Function, options?: Object) => void`
Expand Down Expand Up @@ -256,29 +226,6 @@ setFieldValue("fieldName"); // The field will be unset: { fieldName: "value" }
setFieldValue("fieldName", undefined);
```

### setErrors

`(errors?: FormErrors | Function) => void`

This method allows us to manually set/clear the `errors` of the [form state](../getting-started/form-state).

```js
const { setErrors } = useForm();

setErrors({ firstName: "Required", lastName: "Required" });

// We can also pass a callback as the "errors" parameter, similar to React's setState callback style
setErrors((prevErrors) => ({ ...prevErrors, firstName: "Required" }));
```

We can clear the `errors` of the form state as the following way:

```js
setErrors();
// or
setErrors(undefined); // Works with any falsy values
```

### setFieldError

`(name: string, error?: any | Function) => void`
Expand Down Expand Up @@ -417,7 +364,8 @@ const { controller } = useForm();
// With custom validation
<Component
{...controller("name", {
validate: (value, values /* Form's values */) => !value.length && "Required",
validate: (value, values /* Form's values */) =>
!value.length && "Required",
})}
required
/>;
Expand Down
1 change: 0 additions & 1 deletion docs/getting-started/validation-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ By default, React Cool Form runs all the validation methods as follows. You can
| ---------------------------------------------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------- |
| `onChange` | Individual | Whenever the value of a field has been changed. |
| [`setFieldValue`](../api-reference/use-form#setfieldvalue) | Individual | Whenever the value of a field has been set. |
| [`setValues`](../api-reference/use-form#setvalues) | All | Whenever the `values` of the form have been set. |
| `onBlur` | Individual | Whenever a field has been touched. **If a validation method has been run by the `onChange` event, it won't be run again**. |
| `onSubmit` | All | Whenever a submission attempt is made. |
| [`submit`](../api-reference/use-form#submit) | All | Whenever a submission attempt is made manually. |
Expand Down
31 changes: 1 addition & 30 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ export type FieldArgs = Record<

interface Options<V> {
formState: FormState<V>;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField;
Expand Down Expand Up @@ -135,35 +133,10 @@ export interface GetState {
): any;
}

export interface SetErrors<V> {
(
errors?:
| FormErrors<V>
| ((previousErrors: FormErrors<V>) => FormErrors<V> | undefined)
): void;
}

export interface SetFieldError {
(name: string, error?: any | ((previousError?: any) => any)): void;
}

type ValuesArg<V> = V | ((previousValues: V) => V);

interface FieldNamesFn {
(fieldNames: string[]): string[];
}

export interface SetValues<V> {
(
values: ValuesArg<V>,
options?: {
shouldValidate?: boolean;
touched?: string[] | FieldNamesFn;
dirty?: string[] | FieldNamesFn;
}
): void;
}

export interface SetFieldValue {
(
name: string,
Expand All @@ -184,7 +157,7 @@ export interface ValidateField {

export interface Reset<V> {
(
values?: ValuesArg<V> | null,
values?: V | ((previousValues: V) => V) | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent
): void;
Expand Down Expand Up @@ -251,9 +224,7 @@ export interface Return<V> {
form: RefObject<HTMLFormElement>;
field: FieldRef<V>;
getState: GetState;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField;
Expand Down
27 changes: 0 additions & 27 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ declare module "react-cool-form" {

interface Options<V> {
formState: FormState<V>;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField;
Expand All @@ -28,17 +26,6 @@ declare module "react-cool-form" {
): any;
}

interface SetValues<V> {
(
values: V | PreviousValuesFn<V>,
options?: {
shouldValidate?: boolean;
touched?: string[] | FieldNamesFn;
dirty?: string[] | FieldNamesFn;
}
): void;
}

interface SetFieldValue {
(
name: string,
Expand All @@ -49,10 +36,6 @@ declare module "react-cool-form" {
): void;
}

interface SetErrors<V> {
(errors?: FormErrors<V> | PreviousErrorsFn<FormErrors<V>>): void;
}

interface SetFieldError {
(name: string, error?: any | PreviousErrorFn): void;
}
Expand Down Expand Up @@ -112,10 +95,6 @@ declare module "react-cool-form" {
submitCount: number;
}>;

export interface FieldNamesFn {
(fieldNames: string[]): string[];
}

export interface PreviousValuesFn<V = any> {
(previousValues: V): V;
}
Expand All @@ -124,10 +103,6 @@ declare module "react-cool-form" {
(previousValue: any): any;
}

export interface PreviousErrorsFn<E = FormErrors> {
(previousErrors: E): E | undefined;
}

export interface PreviousErrorFn {
(previousError?: any): any;
}
Expand Down Expand Up @@ -218,9 +193,7 @@ declare module "react-cool-form" {
form: RefObject<HTMLFormElement>;
field: FieldRef<V>;
getState: GetState;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField;
Expand Down
Loading

0 comments on commit 558fbd9

Please sign in to comment.