Skip to content

Commit 9839445

Browse files
committed
Use parse instead of format when calculating checked
1 parent dcf306b commit 9839445

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/Field.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,91 @@ describe("Field", () => {
12491249
console.error.mockRestore();
12501250
});
12511251
});
1252+
1253+
it("should support using format/parse with radio controls", () => {
1254+
const format = (value) => value && value.toString();
1255+
const parse = (value) => value && parseInt(value, 10);
1256+
1257+
const { getByTestId } = render(
1258+
<Form onSubmit={onSubmitMock} initialValues={{ number: 20 }}>
1259+
{({ handleSubmit }) => (
1260+
<form onSubmit={handleSubmit}>
1261+
<Field
1262+
name="number"
1263+
component="input"
1264+
type="radio"
1265+
value={10}
1266+
data-testid="ten"
1267+
parse={parse}
1268+
format={format}
1269+
/>
1270+
<Field
1271+
name="number"
1272+
component="input"
1273+
type="radio"
1274+
value={20}
1275+
data-testid="twenty"
1276+
parse={parse}
1277+
format={format}
1278+
/>
1279+
<Field
1280+
name="number"
1281+
component="input"
1282+
type="radio"
1283+
value={30}
1284+
data-testid="thirty"
1285+
parse={parse}
1286+
format={format}
1287+
/>
1288+
</form>
1289+
)}
1290+
</Form>,
1291+
);
1292+
expect(getByTestId("ten").checked).toBe(false);
1293+
expect(getByTestId("twenty").checked).toBe(true);
1294+
expect(getByTestId("thirty").checked).toBe(false);
1295+
});
1296+
1297+
it("should support using format/parse with checkbox controls", () => {
1298+
const format = (value) => value && value.map((x) => x.toString());
1299+
const parse = (value) => value && value.map((x) => parseInt(x, 10));
1300+
1301+
const { getByTestId } = render(
1302+
<Form onSubmit={onSubmitMock} initialValues={{ number: [20, 30] }}>
1303+
{({ handleSubmit }) => (
1304+
<form onSubmit={handleSubmit}>
1305+
<Field
1306+
name="number"
1307+
component="input"
1308+
type="checkbox"
1309+
value={10}
1310+
data-testid="ten"
1311+
parse={parse}
1312+
format={format}
1313+
/>
1314+
<Field
1315+
name="number"
1316+
component="input"
1317+
type="checkbox"
1318+
value={20}
1319+
data-testid="twenty"
1320+
parse={parse}
1321+
format={format}
1322+
/>
1323+
<Field
1324+
name="number"
1325+
component="input"
1326+
type="checkbox"
1327+
value={30}
1328+
data-testid="thirty"
1329+
parse={parse}
1330+
format={format}
1331+
/>
1332+
</form>
1333+
)}
1334+
</Form>,
1335+
);
1336+
expect(getByTestId("ten").checked).toBe(false);
1337+
expect(getByTestId("twenty").checked).toBe(true);
1338+
expect(getByTestId("thirty").checked).toBe(true);
1339+
});

src/useField.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ function useField<FormValues: FormValuesShape>(
161161
get checked() {
162162
let value = state.value;
163163
if (type === "checkbox") {
164-
value = format(value, name);
164+
value = parse(value, name);
165165
if (_value === undefined) {
166166
return !!value;
167167
} else {
168168
return !!(Array.isArray(value) && ~value.indexOf(_value));
169169
}
170170
} else if (type === "radio") {
171-
return format(value, name) === _value;
171+
return parse(value, name) === _value;
172172
}
173173
return undefined;
174174
},

0 commit comments

Comments
 (0)