Skip to content

Commit daee9a9

Browse files
authored
Pass props through to ToggleSwitch wrapper (#3520)
* pass props through to toggle swtich wrapper * fix lint * changeset
1 parent 33d4345 commit daee9a9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

.changeset/empty-guests-live.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
passthrough dom props on toggleswitch
6+
7+
<!-- Changed components: ToggleSwitch -->

src/ToggleSwitch/ToggleSwitch.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import {CellAlignment} from '../DataTable/column'
1313
const TRANSITION_DURATION = '80ms'
1414
const EASE_OUT_QUAD_CURVE = 'cubic-bezier(0.5, 1, 0.89, 1)'
1515

16-
export type ToggleSwitchProps = {
17-
/** The id of the DOM node that describes the switch */
18-
['aria-describedby']?: string
19-
/** The id of the DOM node that labels the switch */
20-
['aria-labelledby']: string
16+
export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, SxProp {
2117
/** Uncontrolled - whether the switch is turned on */
2218
defaultChecked?: boolean
2319
/** Whether the switch is ready for user input */
@@ -36,7 +32,7 @@ export type ToggleSwitchProps = {
3632
* **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.
3733
*/
3834
statusLabelPosition?: CellAlignment
39-
} & SxProp
35+
}
4036

4137
const sizeVariants = variant({
4238
prop: 'size',
@@ -221,6 +217,7 @@ const ToggleSwitch: React.FC<React.PropsWithChildren<ToggleSwitchProps>> = ({
221217
size = 'medium',
222218
statusLabelPosition = 'start',
223219
sx: sxProp,
220+
...props
224221
}) => {
225222
const isControlled = typeof checked !== 'undefined'
226223
const [isOn, setIsOn] = useProvidedStateOrCreate<boolean>(checked, onChange, Boolean(defaultChecked))
@@ -247,6 +244,7 @@ const ToggleSwitch: React.FC<React.PropsWithChildren<ToggleSwitchProps>> = ({
247244
alignItems="center"
248245
flexDirection={statusLabelPosition === 'start' ? 'row' : 'row-reverse'}
249246
sx={sxProp}
247+
{...props}
250248
>
251249
{loading ? <Spinner size="small" /> : null}
252250
<Text

src/__tests__/ToggleSwitch.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,19 @@ it('calls onChange when the switch is toggled', async () => {
126126
await user.click(toggleSwitch)
127127
expect(handleChange).toHaveBeenCalledWith(true)
128128
})
129+
it('can pass data attributes to the rendered component', async () => {
130+
const TEST_ID = 'a test id'
131+
const ControlledSwitchComponent = () => {
132+
return (
133+
<>
134+
<div id="switchLabel">{SWITCH_LABEL_TEXT}</div>
135+
<ToggleSwitch data-testid={TEST_ID} defaultChecked aria-labelledby="switchLabel" />
136+
</>
137+
)
138+
}
139+
const {getByTestId} = render(<ControlledSwitchComponent />)
140+
const toggleSwitch = getByTestId(TEST_ID)
141+
expect(toggleSwitch).toBeInTheDocument()
142+
})
129143

130144
checkStoriesForAxeViolations('ToggleSwitch.features', '../ToggleSwitch/')

0 commit comments

Comments
 (0)