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 #159 from wellyshen/feature/format-for-controller
Browse files Browse the repository at this point in the history
Feat(useForm): add "format" option for "controller"
  • Loading branch information
wellyshen authored Nov 24, 2020
2 parents bf90b3b + afab297 commit 5a28924
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-beds-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

feat(useForm): add `format` option for `controller`
1 change: 1 addition & 0 deletions examples/src/BasicForm/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const Controller = ({
value,
defaultValue: "welly",
// parse,
format: (val) => `formatted ${val}`,
onChange: (e, val) => {
setValue(val);
// setValue(e.target.value);
Expand Down
6 changes: 3 additions & 3 deletions examples/src/BasicForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fib = (n: number): number => (n < 3 ? 1 : fib(n - 2) + fib(n - 1));

export interface FormValues {
text: Record<string, string>;
controller1?: any;
controller1: any;
controller2: any;
dynamicText1?: string;
dynamicText2?: string;
Expand All @@ -32,7 +32,7 @@ export interface FormValues {

const defaultValues = {
text: { nest: "new test" },
// controller1: "new test",
controller1: "new test",
controller2: "new test",
dynamicText1: "new test",
dynamicText2: "new test",
Expand Down Expand Up @@ -241,7 +241,7 @@ export default (): JSX.Element => {
}, */
})}
// required
data-rcf-ignore
// data-rcf-ignore
// defaultChecked
/>
<Controller
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export interface Controller<V, E = any> {
value?: any;
defaultValue?: any;
parse?: (event: E) => any;
format?: (value: any) => any;
onChange?: (event: E, value?: any) => void;
onBlur?: (event: FocusEvent<any>) => void;
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ declare module "react-cool-form" {
(event: E): R;
}

export interface Format {
(value: any): any;
}

export interface OnChange<E = any> {
(event: E, value?: any): void;
}
Expand All @@ -148,6 +152,7 @@ declare module "react-cool-form" {
value?: any;
defaultValue?: any;
parse?: Parse<E>;
format?: Format;
onChange?: OnChange<E>;
onBlur?: OnBlur;
}
Expand Down
10 changes: 8 additions & 2 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,10 @@ export default <V extends FormValues = FormValues>({
);

const controller = useCallback<Controller<V>>(
(name, { validate, value, defaultValue, parse, onChange, onBlur } = {}) => {
(
name,
{ validate, value, defaultValue, parse, format, onChange, onBlur } = {}
) => {
if (!name) {
warn('💡 react-cool-form > controller: Missing the "name" parameter.');
return {};
Expand All @@ -694,9 +697,12 @@ export default <V extends FormValues = FormValues>({
if (validate) fieldValidatorsRef.current[name] = validate;
if (!isUndefined(defaultValue)) setDefaultValue(name, defaultValue);

value = !isUndefined(value) ? value : getState(`values.${name}`);
value = (format ? format(value) : value) ?? "";

return {
name,
value: !isUndefined(value) ? value : getState(`values.${name}`) ?? "",
value,
onChange: (e) => {
const parsedE = parse ? parse(e) : e;
let value;
Expand Down

0 comments on commit 5a28924

Please sign in to comment.