Skip to content

Commit 21d9f3b

Browse files
erikras-richard-agenterikras-dinesh-agenterikras-richard-agent
authored
Fix #974: Use parse instead of format in checkbox/radio checked logic (#1074)
* Fix #974: Use parse instead of format in getInputChecked The bug: getInputChecked() used format() to transform the value before comparing to _value. But _value is in internal format while format() converts to display format, causing type mismatches (e.g. string "10" vs number 10). Fix: Use parse() instead of format() in checkbox/radio checked logic. This compares the parsed internal value with the raw _value attribute. Added tests with numeric radio/checkbox values using format/parse to prevent future regressions. * chore: remove unused defaultIsEqual --------- Co-authored-by: erikras-dinesh-agent <dinesh@openclaw.dev> Co-authored-by: erikras-richard-agent <richard@openclaw.ai>
1 parent 1c1d0a9 commit 21d9f3b

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,4 +1249,92 @@ describe("Field", () => {
12491249
}).toThrow("prop name cannot be undefined in <Field> component");
12501250
console.error = consoleError;
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+
});
12521340
});

src/useField.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,14 @@ function useField<
223223
const getInputChecked = () => {
224224
let value = state.value;
225225
if (type === "checkbox") {
226-
value = format(value, name);
226+
value = parse(value, name);
227227
if (_value === undefined) {
228228
return !!value;
229229
} else {
230230
return !!(Array.isArray(value) && ~value.indexOf(_value));
231231
}
232232
} else if (type === "radio") {
233-
return format(value, name) === _value;
233+
return parse(value, name) === _value;
234234
}
235235
return undefined;
236236
};

0 commit comments

Comments
 (0)