import { forwardRef, useId } from 'react'
import Block from '../../Primitives/Block/Block'
import {
SwitchV2ButtonProps,
SwitchV2ContentProps,
SwitchV2Props,
} from './switchV2.types'
import { filterBlockedProps } from '../../../utils/prop-helpers'
import { useResponsiveTokens } from '../../../hooks/useResponsiveTokens'
import { SwitchV2TokensType } from './switchV2.tokens'
import PrimitiveButton from '../../Primitives/PrimitiveButton/PrimitiveButton'
import { getSubtextId, mergeAriaDescribedBy } from '../../Switch/utils'
import SelectorsLabel from '../../SelectorsContent/SelectorsLabel'
import SelectorsSubLabel from '../../SelectorsContent/SelectorsSubLabel'
import {
SelectorsLabelTokensType,
SelectorsSubLabelTokensType,
} from '../../SelectorsContent/SelectorsContent.types'
import { FOUNDATION_THEME } from '../../../tokens'
import { addAccessibleAriaAttributes } from '../../../utils/accessibility/icon-helpers'
import { SelectorV2Size } from '../selectorV2.types'
const SwitchV2Content = ({
uniqueId,
disabled,
error,
required,
size,
label,
subLabel,
slot,
tokens,
labelMaxLength,
subLabelMaxLength,
subLabelId,
}: SwitchV2ContentProps) => {
const labelId = `${uniqueId}-label`
return (
{slot && (
{addAccessibleAriaAttributes(slot.slot)}
)}
)
}
const SwitchV2Button = ({
id,
checked,
disabled,
size,
tokens,
onToggle,
buttonProps,
ref,
}: SwitchV2ButtonProps) => {
return (
)}
>
)
}
const SwitchV2 = forwardRef(
(
{
id,
checked,
onCheckedChange,
required = false,
disabled = false,
error = false,
size = SelectorV2Size.MD,
label,
subLabel,
slot,
maxLength,
...rest
},
ref
) => {
const generatedId = useId()
const uniqueId = id || generatedId
const tokens = useResponsiveTokens('SWITCHV2')
const labelMaxLength = maxLength?.label
const subLabelMaxLength = maxLength?.subLabel
const subLabelId = getSubtextId(uniqueId, !!subLabel)
const { 'aria-describedby': customAriaDescribedBy, ...restProps } =
rest as { 'aria-describedby'?: string; [key: string]: unknown }
const ariaAttributes = {
'aria-required': required || undefined,
'aria-invalid': error || undefined,
'aria-disabled': disabled || undefined,
'aria-describedby': mergeAriaDescribedBy(
subLabelId,
customAriaDescribedBy
),
}
const filteredRest = filterBlockedProps(restProps)
const rootButtonProps = {
...filteredRest,
...ariaAttributes,
}
return (
onCheckedChange?.(!checked)}
buttonProps={rootButtonProps}
/>
)
}
)
SwitchV2.displayName = 'SwitchV2'
export default SwitchV2