Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion packages/react/src/switch/root/SwitchRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,14 @@ describe('<Switch.Root />', () => {
expect(thumb).not.to.have.attribute('data-checked');
});

it('should set the name attribute on the input', async () => {
it('should set the name attribute only on the input', async () => {
const { getByRole } = await render(<Switch.Root name="switch-name" />);

const switchElement = screen.getByRole('switch');
const input = getByRole('checkbox', { hidden: true });

expect(input).to.have.attribute('name', 'switch-name');
expect(switchElement).not.to.have.attribute('name');
});

describe('Form', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/react/src/switch/root/SwitchRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const SwitchRoot = React.forwardRef(function SwitchRoot(
defaultChecked,
id: idProp,
inputRef: externalInputRef,
name: nameProp,
nativeButton = true,
onCheckedChange: onCheckedChangeProp,
readOnly = false,
Expand All @@ -61,7 +62,7 @@ export const SwitchRoot = React.forwardRef(function SwitchRoot(
} = useFieldRootContext();

const disabled = fieldDisabled || disabledProp;
const name = fieldName ?? elementProps.name;
const name = fieldName ?? nameProp;

const {
getValidationProps,
Expand Down Expand Up @@ -252,9 +253,7 @@ export const SwitchRoot = React.forwardRef(function SwitchRoot(
return (
<SwitchRootContext.Provider value={state}>
{element}
{!checked && elementProps.name && (
<input type="hidden" name={elementProps.name} value="off" />
)}
{!checked && nameProp && <input type="hidden" name={nameProp} value="off" />}
<input {...inputProps} />
</SwitchRootContext.Provider>
);
Expand Down
Loading