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 #192 from wellyshen/feature/controlled-option
Browse files Browse the repository at this point in the history
Feat(useForm): add "controlled" option for "field" method
  • Loading branch information
wellyshen authored Dec 2, 2020
2 parents 0e585ef + 9e2c2f0 commit f9583f1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-moles-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

feat(useForm): add `controlled` option for `field` method
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ interface Field<V = FormValues, E = any> {
format?: Format;
onChange?: OnChange<E>;
onBlur?: OnBlur;
controlled?: boolean;
}
): {
name: string;
Expand Down
3 changes: 2 additions & 1 deletion app/src/Form/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Field = ({
validate,
value,
defaultValue: "welly",
// parse,
parse,
format: (val) => `formatted ${val}`,
onChange: (e, val) => {
setValue(val);
Expand All @@ -48,6 +48,7 @@ const Field = ({
onBlur: (e) => {
// console.log("LOG ===> onBlur: ", e);
},
controlled: false,
})}
{...rest}
/>
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export interface Field<V, E = any> {
format?: (value: any) => any;
onChange?: (event: E, value?: any) => void;
onBlur?: (event: FocusEvent<any>) => void;
controlled?: boolean;
}
): {
name: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ declare module "react-cool-form" {
format?: Format;
onChange?: OnChange<E>;
onBlur?: OnBlur;
controlled?: boolean;
}
): {
name: string;
Expand Down
19 changes: 16 additions & 3 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,16 @@ export default <V extends FormValues = FormValues>({
const field = useCallback<Field<V>>(
(
name,
{ validate, value, defaultValue, parse, format, onChange, onBlur } = {}
{
validate,
value,
defaultValue,
parse,
format,
onChange,
onBlur,
controlled = true,
} = {}
) => {
if (!name) {
warn('💡 react-cool-form > field: Missing the "name" parameter.');
Expand All @@ -698,8 +707,12 @@ export default <V extends FormValues = FormValues>({
defaultValue = !isUndefined(val) ? val : defaultValue;
if (!isUndefined(defaultValue)) setDefaultValue(name, defaultValue);

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

return {
name,
Expand Down

0 comments on commit f9583f1

Please sign in to comment.