@@ -2,11 +2,12 @@ import React, {
22 FC ,
33 InputHTMLAttributes ,
44 Ref ,
5+ useId ,
56 useContext ,
67 ReactNode ,
78} from 'react' ;
89import classnames from 'classnames' ;
9- import { FormElement , FormElementProps } from './FormElement' ;
10+ import { FormElement } from './FormElement' ;
1011import { CheckboxGroupContext , CheckboxValueType } from './CheckboxGroup' ;
1112
1213/**
@@ -15,7 +16,6 @@ import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
1516export type CheckboxProps = {
1617 label ?: string ;
1718 required ?: boolean ;
18- error ?: FormElementProps [ 'error' ] ;
1919 cols ?: number ;
2020 name ?: string ;
2121 value ?: CheckboxValueType ;
@@ -33,10 +33,10 @@ export type CheckboxProps = {
3333export const Checkbox : FC < CheckboxProps > = ( props ) => {
3434 const {
3535 type, // eslint-disable-line @typescript-eslint/no-unused-vars
36+ id : id_ ,
3637 className,
3738 label,
3839 required,
39- error,
4040 cols,
4141 tooltip,
4242 tooltipIcon,
@@ -45,22 +45,35 @@ export const Checkbox: FC<CheckboxProps> = (props) => {
4545 children,
4646 ...rprops
4747 } = props ;
48- const { grouped } = useContext ( CheckboxGroupContext ) ;
48+
49+ const prefix = useId ( ) ;
50+ const id = id_ ?? `${ prefix } -id` ;
51+
52+ const { grouped, error, errorId } = useContext ( CheckboxGroupContext ) ;
4953 const formElemProps = {
5054 required,
5155 error,
56+ errorId,
5257 cols,
5358 tooltip,
5459 tooltipIcon,
5560 elementRef,
5661 } ;
5762 const checkClassNames = classnames ( className , 'slds-checkbox' ) ;
5863 const check = (
59- < label className = { checkClassNames } >
60- < input ref = { inputRef } type = 'checkbox' { ...rprops } />
61- < span className = 'slds-checkbox_faux' />
62- < span className = 'slds-form-element__label' > { label || children } </ span >
63- </ label >
64+ < div className = { checkClassNames } >
65+ < input
66+ ref = { inputRef }
67+ type = 'checkbox'
68+ { ...rprops }
69+ id = { id }
70+ aria-describedby = { error ? errorId : undefined }
71+ />
72+ < label className = 'slds-checkbox__label' htmlFor = { id } >
73+ < span className = 'slds-checkbox_faux' />
74+ < span className = 'slds-form-element__label' > { label || children } </ span >
75+ </ label >
76+ </ div >
6477 ) ;
6578 return grouped ? (
6679 check
0 commit comments