Skip to content

Commit 81a3475

Browse files
dwsosadependabot[bot]jonrohan
authored andcommitted
added default button type to switch button to prevent submit when included in form (#5841)
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jon Rohan <[email protected]>
1 parent 6684d0f commit 81a3475

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

.changeset/dull-boats-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
added default button type to switch button to prevent submit when included in form

packages/react/src/ToggleSwitch/ToggleSwitch.docs.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@
9494
"type": "'start' | 'end'",
9595
"defaultValue": "'start'",
9696
"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>"
97+
},
98+
{
99+
"name": "buttonType",
100+
"type": "'button' | 'submit' | 'reset'",
101+
"defaultValue": "'button'",
102+
"description": "<div>As it’s part of form behavior, this controls whether the button is of type button, submit, or reset.</div>"
97103
}
98104
],
99105
"subcomponents": []
100-
}
106+
}

packages/react/src/ToggleSwitch/ToggleSwitch.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,17 @@ describe('ToggleSwitch', () => {
180180
expect(ref).toHaveBeenCalledWith(expect.any(HTMLButtonElement))
181181
})
182182

183+
it('renders a switch that has button type button', () => {
184+
const {getByLabelText} = render(
185+
<>
186+
<div id="switchLabel">{SWITCH_LABEL_TEXT}</div>
187+
<ToggleSwitch aria-labelledby="switchLabel" />
188+
</>,
189+
)
190+
191+
const toggleSwitch = getByLabelText(SWITCH_LABEL_TEXT)
192+
expect(toggleSwitch).toHaveAttribute('type', 'button')
193+
})
194+
183195
checkStoriesForAxeViolations('ToggleSwitch.features', '../ToggleSwitch/')
184196
})

packages/react/src/ToggleSwitch/ToggleSwitch.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElem
3434
* **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.
3535
*/
3636
statusLabelPosition?: CellAlignment
37+
/** type of button to account for behavior when added to a form*/
38+
buttonType?: 'button' | 'submit' | 'reset'
3739
}
3840

3941
const sizeVariants = variant({
@@ -221,6 +223,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
221223
checked,
222224
onChange,
223225
onClick,
226+
buttonType = 'button',
224227
size = 'medium',
225228
statusLabelPosition = 'start',
226229
sx: sxProp,
@@ -243,7 +246,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
243246
if (onChange && isControlled) {
244247
onChange(Boolean(checked))
245248
}
246-
}, [onChange, checked, isControlled])
249+
}, [onChange, checked, isControlled, buttonType])
247250

248251
return (
249252
<Box
@@ -271,6 +274,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
271274
</Text>
272275
<SwitchButton
273276
ref={ref}
277+
type={buttonType}
274278
onClick={handleToggleClick}
275279
aria-labelledby={ariaLabelledby}
276280
aria-describedby={ariaDescribedby}

0 commit comments

Comments
 (0)