Skip to content

Commit

Permalink
Pass props through to ToggleSwitch wrapper (#3520)
Browse files Browse the repository at this point in the history
* pass props through to toggle swtich wrapper

* fix lint

* changeset
  • Loading branch information
mattcosta7 authored Jul 14, 2023
1 parent 33d4345 commit daee9a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/empty-guests-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/react': patch
---

passthrough dom props on toggleswitch

<!-- Changed components: ToggleSwitch -->
10 changes: 4 additions & 6 deletions src/ToggleSwitch/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {CellAlignment} from '../DataTable/column'
const TRANSITION_DURATION = '80ms'
const EASE_OUT_QUAD_CURVE = 'cubic-bezier(0.5, 1, 0.89, 1)'

export type ToggleSwitchProps = {
/** The id of the DOM node that describes the switch */
['aria-describedby']?: string
/** The id of the DOM node that labels the switch */
['aria-labelledby']: string
export interface ToggleSwitchProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, SxProp {
/** Uncontrolled - whether the switch is turned on */
defaultChecked?: boolean
/** Whether the switch is ready for user input */
Expand All @@ -36,7 +32,7 @@ export type ToggleSwitchProps = {
* **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
} & SxProp
}

const sizeVariants = variant({
prop: 'size',
Expand Down Expand Up @@ -221,6 +217,7 @@ const ToggleSwitch: React.FC<React.PropsWithChildren<ToggleSwitchProps>> = ({
size = 'medium',
statusLabelPosition = 'start',
sx: sxProp,
...props
}) => {
const isControlled = typeof checked !== 'undefined'
const [isOn, setIsOn] = useProvidedStateOrCreate<boolean>(checked, onChange, Boolean(defaultChecked))
Expand All @@ -247,6 +244,7 @@ const ToggleSwitch: React.FC<React.PropsWithChildren<ToggleSwitchProps>> = ({
alignItems="center"
flexDirection={statusLabelPosition === 'start' ? 'row' : 'row-reverse'}
sx={sxProp}
{...props}
>
{loading ? <Spinner size="small" /> : null}
<Text
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/ToggleSwitch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,19 @@ it('calls onChange when the switch is toggled', async () => {
await user.click(toggleSwitch)
expect(handleChange).toHaveBeenCalledWith(true)
})
it('can pass data attributes to the rendered component', async () => {
const TEST_ID = 'a test id'
const ControlledSwitchComponent = () => {
return (
<>
<div id="switchLabel">{SWITCH_LABEL_TEXT}</div>
<ToggleSwitch data-testid={TEST_ID} defaultChecked aria-labelledby="switchLabel" />
</>
)
}
const {getByTestId} = render(<ControlledSwitchComponent />)
const toggleSwitch = getByTestId(TEST_ID)
expect(toggleSwitch).toBeInTheDocument()
})

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

0 comments on commit daee9a9

Please sign in to comment.