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 #157 from wellyshen/feature/change-parser-to-parse
Browse files Browse the repository at this point in the history
Refactor(useForm): change the "parser" option of controller to "parse"
  • Loading branch information
wellyshen authored Nov 24, 2020
2 parents 885fe0b + 5c79904 commit bf90b3b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-spoons-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

refactor(useForm): change the `parser` option of controller to `parse`
6 changes: 3 additions & 3 deletions examples/src/BasicForm/Controller.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChangeEvent, memo, useState } from "react";
import {
Controller as ControllerType,
Parser,
Parse,
FieldValidator,
} from "react-cool-form";

Expand Down Expand Up @@ -31,7 +31,7 @@ const Controller = ({
// console.log(`LOG ==> ${name} is re-rendered`);

const [value, setValue] = useState(defaultValue);
const parser: Parser<ChangeEvent<HTMLInputElement>> = (e) =>
const parse: Parse<ChangeEvent<HTMLInputElement>> = (e) =>
e.target.value.length % 2 ? "case 1" : "case 2";

return (
Expand All @@ -42,7 +42,7 @@ const Controller = ({
validate,
value,
defaultValue: "welly",
// parser,
// parse,
onChange: (e, val) => {
setValue(val);
// setValue(e.target.value);
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export interface Controller<V, E = any> {
validate?: FieldValidator<V>;
value?: any;
defaultValue?: any;
parser?: (event: E) => any;
parse?: (event: E) => any;
onChange?: (event: E, value?: any) => void;
onBlur?: (event: FocusEvent<any>) => void;
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ declare module "react-cool-form" {
(event?: SyntheticEvent<any>): Promise<{ values?: V; errors?: Errors<V> }>;
}

export interface Parser<E = any, R = any> {
export interface Parse<E = any, R = any> {
(event: E): R;
}

Expand All @@ -147,7 +147,7 @@ declare module "react-cool-form" {
validate?: FieldValidator<V>;
value?: any;
defaultValue?: any;
parser?: Parser<E>;
parse?: Parse<E>;
onChange?: OnChange<E>;
onBlur?: OnBlur;
}
Expand Down
7 changes: 2 additions & 5 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,7 @@ export default <V extends FormValues = FormValues>({
);

const controller = useCallback<Controller<V>>(
(
name,
{ validate, value, defaultValue, parser, onChange, onBlur } = {}
) => {
(name, { validate, value, defaultValue, parse, onChange, onBlur } = {}) => {
if (!name) {
warn('💡 react-cool-form > controller: Missing the "name" parameter.');
return {};
Expand All @@ -701,7 +698,7 @@ export default <V extends FormValues = FormValues>({
name,
value: !isUndefined(value) ? value : getState(`values.${name}`) ?? "",
onChange: (e) => {
const parsedE = parser ? parser(e) : e;
const parsedE = parse ? parse(e) : e;
let value;

if (
Expand Down

0 comments on commit bf90b3b

Please sign in to comment.