Skip to content

added default button type to switch button to prevent submit when included in form #5841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 22, 2025
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
5 changes: 5 additions & 0 deletions .changeset/dull-boats-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

added default button type to switch button to prevent submit when included in form
8 changes: 7 additions & 1 deletion packages/react/src/ToggleSwitch/ToggleSwitch.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@
"type": "'start' | 'end'",
"defaultValue": "'start'",
"description": "<div>Whether the \"on\" and \"off\" labels should appear before or after the switch.</div> <div> <Text fontWeight=\"bold\">This should only be changed when the switch's alignment needs to be adjusted.</Text> For example: It needs to be left-aligned because the label appears above it and the caption appears below it. </div>"
},
{
"name": "buttonType",
"type": "'button' | 'submit' | 'reset'",
"defaultValue": "'button'",
"description": "<div>As it’s part of form behavior, this controls whether the button is of type button, submit, or reset.</div>"
}
],
"subcomponents": []
}
}
12 changes: 12 additions & 0 deletions packages/react/src/ToggleSwitch/ToggleSwitch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,17 @@ describe('ToggleSwitch', () => {
expect(ref).toHaveBeenCalledWith(expect.any(HTMLButtonElement))
})

it('renders a switch that has button type button', () => {
const {getByLabelText} = render(
<>
<div id="switchLabel">{SWITCH_LABEL_TEXT}</div>
<ToggleSwitch aria-labelledby="switchLabel" />
</>,
)

const toggleSwitch = getByLabelText(SWITCH_LABEL_TEXT)
expect(toggleSwitch).toHaveAttribute('type', 'button')
})

checkStoriesForAxeViolations('ToggleSwitch.features', '../ToggleSwitch/')
})
6 changes: 5 additions & 1 deletion packages/react/src/ToggleSwitch/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElem
* **This should only be changed when the switch's alignment needs to be adjusted.** For example: It needs to be left-aligned because the label appears above it and the caption appears below it.
*/
statusLabelPosition?: CellAlignment
/** type of button to account for behavior when added to a form*/
buttonType?: 'button' | 'submit' | 'reset'
}

const sizeVariants = variant({
Expand Down Expand Up @@ -221,6 +223,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
checked,
onChange,
onClick,
buttonType = 'button',
size = 'medium',
statusLabelPosition = 'start',
sx: sxProp,
Expand All @@ -243,7 +246,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
if (onChange && isControlled) {
onChange(Boolean(checked))
}
}, [onChange, checked, isControlled])
}, [onChange, checked, isControlled, buttonType])

return (
<Box
Expand Down Expand Up @@ -271,6 +274,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
</Text>
<SwitchButton
ref={ref}
type={buttonType}
onClick={handleToggleClick}
aria-labelledby={ariaLabelledby}
aria-describedby={ariaDescribedby}
Expand Down
Loading