Skip to content

Commit c035cf6

Browse files
erikras-gilfoyle-agentErik Rasmussen
andauthored
Fix: Prevent field name from shadowing DOM properties (fixes #871) (#1077)
- Extract 'name' from inputProps before spreading to avoid shadowing - Fixes crash when field name is 'nodeName', 'className', etc. - Add test case for field named 'nodeName' Co-authored-by: Erik Rasmussen <erik@mini.local>
1 parent 21d9f3b commit c035cf6

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/Field.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,3 +1338,42 @@ describe("Field", () => {
13381338
expect(getByTestId("thirty").checked).toBe(true);
13391339
});
13401340
});
1341+
1342+
describe("Field.nodeName issue #871", () => {
1343+
it("should not crash when field name is 'nodeName'", () => {
1344+
const onSubmit = jest.fn();
1345+
const { getByTestId } = render(
1346+
<Form onSubmit={onSubmit}>
1347+
{({ handleSubmit }) => (
1348+
<form onSubmit={handleSubmit}>
1349+
<Field
1350+
name="nodeName"
1351+
component="input"
1352+
data-testid="nodeName-input"
1353+
/>
1354+
<button type="submit" data-testid="submit">
1355+
Submit
1356+
</button>
1357+
</form>
1358+
)}
1359+
</Form>,
1360+
);
1361+
1362+
const input = getByTestId("nodeName-input");
1363+
const submit = getByTestId("submit");
1364+
1365+
// Should not crash when interacting with the field
1366+
expect(() => {
1367+
fireEvent.change(input, { target: { value: "test" } });
1368+
fireEvent.blur(input);
1369+
fireEvent.click(submit);
1370+
}).not.toThrow();
1371+
1372+
// Verify the field value was set correctly
1373+
expect(onSubmit).toHaveBeenCalledWith(
1374+
{ nodeName: "test" },
1375+
expect.any(Object),
1376+
expect.any(Function),
1377+
);
1378+
});
1379+
});

src/Field.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ function FieldComponent<
6868

6969
if (typeof component === "string") {
7070
// ignore meta, combine input with any other props
71-
const inputProps = { ...mergedField.input };
71+
const { name: inputName, ...restInputProps } = mergedField.input;
7272

7373
// Ensure multiple select has array value
7474
if (
7575
component === "select" &&
7676
multiple &&
77-
!Array.isArray(inputProps.value)
77+
!Array.isArray(restInputProps.value)
7878
) {
79-
inputProps.value = [] as any;
79+
restInputProps.value = [] as any;
8080
}
8181

8282
return React.createElement(component, {
83-
...inputProps,
83+
name: inputName, // Pass name explicitly to avoid shadowing DOM properties
84+
...restInputProps,
8485
children,
8586
ref,
8687
...rest,

0 commit comments

Comments
 (0)