1- import { AriaAttributes , HTMLAttributes } from 'react'
1+ import { AriaAttributes , useRef } from 'react'
22import { AnimatePresence , motion } from 'framer-motion'
33
44import { Tooltip } from '@Common/Tooltip'
55import { ComponentSizeType } from '@Shared/constants'
6+ import { getUniqueId } from '@Shared/Helpers'
67
78import { Icon } from '../Icon'
8- import { INDETERMINATE_ICON_WIDTH_MAP , LOADING_COLOR_MAP , SQUARE_ICON_DIMENSION_MAP } from './constants'
9+ import { INDETERMINATE_ICON_WIDTH_MAP , LOADING_COLOR_MAP } from './constants'
910import { SwitchProps } from './types'
1011import { getSwitchContainerClass , getSwitchIconColor , getSwitchThumbClass , getSwitchTrackColor } from './utils'
1112
@@ -22,8 +23,11 @@ const Switch = ({
2223 iconName,
2324 indeterminate = false ,
2425 size = ComponentSizeType . medium ,
26+ name,
2527 onChange,
2628} : SwitchProps ) => {
29+ const inputId = useRef ( getUniqueId ( ) )
30+
2731 const getAriaCheckedValue = ( ) : AriaAttributes [ 'aria-checked' ] => {
2832 if ( ! isChecked ) {
2933 return false
@@ -35,7 +39,6 @@ const Switch = ({
3539 const ariaCheckedValue = getAriaCheckedValue ( )
3640
3741 const showIndeterminateIcon = ariaCheckedValue === 'mixed'
38- const role : HTMLAttributes < HTMLButtonElement > [ 'role' ] = showIndeterminateIcon ? 'checkbox' : 'switch'
3942
4043 const renderContent = ( ) => {
4144 if ( isLoading ) {
@@ -69,7 +72,7 @@ const Switch = ({
6972 iconName && (
7073 < motion . span
7174 key = "icon"
72- className = { ` ${ SQUARE_ICON_DIMENSION_MAP [ size ] } flex dc__fill-available-space dc__no-shrink` }
75+ className = "icon-dim-12 flex dc__fill-available-space dc__no-shrink"
7376 initial = { { scale : 0.8 , opacity : 0 } }
7477 animate = { { scale : 1 , opacity : 1 } }
7578 exit = { { scale : 0.8 , opacity : 0 } }
@@ -91,23 +94,37 @@ const Switch = ({
9194 )
9295 }
9396
94- // TODO: Can add hidden input for accessibility in case name [for forms] is given
9597 return (
9698 < Tooltip alwaysShowTippyOnHover = { ! ! tooltipContent } content = { tooltipContent } >
97- < div className = { `${ getSwitchContainerClass ( { shape, size } ) } flex dc__no-shrink py-2` } >
99+ < label
100+ htmlFor = { inputId . current }
101+ className = { `${ getSwitchContainerClass ( { shape, size } ) } flex dc__no-shrink py-2` }
102+ >
103+ < input
104+ type = "checkbox"
105+ id = { inputId . current }
106+ name = { name }
107+ checked = { isChecked }
108+ disabled = { isDisabled }
109+ readOnly
110+ hidden
111+ />
112+
98113 < button
99114 type = "button"
100- role = { role }
115+ role = "checkbox"
101116 aria-checked = { ariaCheckedValue }
117+ aria-labelledby = { inputId . current }
102118 aria-label = { isLoading ? 'Loading...' : ariaLabel }
103119 data-testid = { dataTestId }
104120 disabled = { isDisabled || isLoading }
121+ aria-disabled = { isDisabled }
105122 className = { `p-0-imp h-100 flex flex-grow-1 dc__transparent ${ isDisabled ? 'dc__disabled' : '' } dc__fill-available-space` }
106123 onClick = { onChange }
107124 >
108125 { renderContent ( ) }
109126 </ button >
110- </ div >
127+ </ label >
111128 </ Tooltip >
112129 )
113130}
0 commit comments