Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parse instead of format when calculating checked #974

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 1249,91 @@ describe("Field", () => {
console.error.mockRestore();
});
});

it("should support using format/parse with radio controls", () => {
const format = (value) => value && value.toString();
const parse = (value) => value && parseInt(value, 10);

const { getByTestId } = render(
<Form onSubmit={onSubmitMock} initialValues={{ number: 20 }}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field
name="number"
component="input"
type="radio"
value={10}
data-testid="ten"
parse={parse}
format={format}
/>
<Field
name="number"
component="input"
type="radio"
value={20}
data-testid="twenty"
parse={parse}
format={format}
/>
<Field
name="number"
component="input"
type="radio"
value={30}
data-testid="thirty"
parse={parse}
format={format}
/>
</form>
)}
</Form>,
);
expect(getByTestId("ten").checked).toBe(false);
expect(getByTestId("twenty").checked).toBe(true);
expect(getByTestId("thirty").checked).toBe(false);
});

it("should support using format/parse with checkbox controls", () => {
const format = (value) => value && value.map((x) => x.toString());
const parse = (value) => value && value.map((x) => parseInt(x, 10));

const { getByTestId } = render(
<Form onSubmit={onSubmitMock} initialValues={{ number: [20, 30] }}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field
name="number"
component="input"
type="checkbox"
value={10}
data-testid="ten"
parse={parse}
format={format}
/>
<Field
name="number"
component="input"
type="checkbox"
value={20}
data-testid="twenty"
parse={parse}
format={format}
/>
<Field
name="number"
component="input"
type="checkbox"
value={30}
data-testid="thirty"
parse={parse}
format={format}
/>
</form>
)}
</Form>,
);
expect(getByTestId("ten").checked).toBe(false);
expect(getByTestId("twenty").checked).toBe(true);
expect(getByTestId("thirty").checked).toBe(true);
});
4 changes: 2 additions & 2 deletions src/useField.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 161,14 @@ function useField<FormValues: FormValuesShape>(
get checked() {
let value = state.value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value should probably be const as you can call parse right here, then you could remove line 164 and change 171 to `return value === _value"

if (type === "checkbox") {
value = format(value, name);
value = parse(value, name);
if (_value === undefined) {
return !!value;
} else {
return !!(Array.isArray(value) && ~value.indexOf(_value));
}
} else if (type === "radio") {
return format(value, name) === _value;
return parse(value, name) === _value;
}
return undefined;
},
Expand Down