Skip to content
Merged
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
39 changes: 39 additions & 0 deletions src/Field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,3 +1338,42 @@ describe("Field", () => {
expect(getByTestId("thirty").checked).toBe(true);
});
});

describe("Field.nodeName issue #871", () => {
it("should not crash when field name is 'nodeName'", () => {
const onSubmit = jest.fn();
const { getByTestId } = render(
<Form onSubmit={onSubmit}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field
name="nodeName"
component="input"
data-testid="nodeName-input"
/>
<button type="submit" data-testid="submit">
Submit
</button>
</form>
)}
</Form>,
);

const input = getByTestId("nodeName-input");
const submit = getByTestId("submit");

// Should not crash when interacting with the field
expect(() => {
fireEvent.change(input, { target: { value: "test" } });
fireEvent.blur(input);
fireEvent.click(submit);
}).not.toThrow();

// Verify the field value was set correctly
expect(onSubmit).toHaveBeenCalledWith(
{ nodeName: "test" },
expect.any(Object),
expect.any(Function),
);
});
});
9 changes: 5 additions & 4 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ function FieldComponent<

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

// Ensure multiple select has array value
if (
component === "select" &&
multiple &&
!Array.isArray(inputProps.value)
!Array.isArray(restInputProps.value)
) {
inputProps.value = [] as any;
restInputProps.value = [] as any;
}

return React.createElement(component, {
...inputProps,
name: inputName, // Pass name explicitly to avoid shadowing DOM properties
...restInputProps,
children,
ref,
...rest,
Expand Down