11import React , { HTMLProps , PureComponent , SyntheticEvent } from 'react' ;
22import classNames from 'classnames' ;
3+ import LabelBlock from '../utils/LabelBlock' ;
4+ import { HintProps } from '../hint/Hint' ;
5+ import { generateRandomName } from '../../util/RandomName' ;
36import FormContext from '../form/FormContext' ;
4- import ErrorMessage from '../error-message' ;
57import CheckboxContext from './CheckboxContext' ;
68import Box , { BoxProps } from './Box' ;
9+ import { LabelProps } from '../label/Label' ;
10+ import { ErrorMessageProps } from '../error-message/ErrorMessage' ;
711
812interface CheckboxesEvent extends SyntheticEvent < HTMLInputElement > {
913 target : HTMLInputElement ;
1014}
1115
1216interface CheckboxesProps extends HTMLProps < HTMLDivElement > {
13- error ?: string | boolean ;
1417 idPrefix ?: string ;
1518 onChange ?: ( e : CheckboxesEvent ) => any ;
19+ label ?: string ;
20+ labelProps ?: LabelProps ;
21+ hint ?: string ;
22+ hintProps ?: HintProps ;
23+ error ?: string | boolean ;
24+ errorProps ?: ErrorMessageProps ;
1625}
1726
1827interface CheckboxesState {
@@ -27,7 +36,7 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
2736 constructor ( props : CheckboxesProps , ...rest : any [ ] ) {
2837 super ( props , ...rest ) ;
2938 this . state = {
30- name : props . name || `checkbox_ ${ ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 2 , 7 ) } ` ,
39+ name : props . name || generateRandomName ( 'checkbox' ) ,
3140 } ;
3241 this . boxCount = 0 ;
3342 }
@@ -47,7 +56,19 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
4756 static Box : React . FC < BoxProps > = Box ;
4857
4958 render ( ) {
50- const { error, className, id, children, idPrefix, ...rest } = this . props ;
59+ const {
60+ error,
61+ className,
62+ id,
63+ children,
64+ idPrefix,
65+ label,
66+ labelProps,
67+ errorProps,
68+ hint,
69+ hintProps,
70+ ...rest
71+ } = this . props ;
5172 const { name } = this . state ;
5273 const { isForm, setError } = this . context ;
5374
@@ -56,14 +77,22 @@ class Checkboxes extends PureComponent<CheckboxesProps, CheckboxesState> {
5677 }
5778
5879 return (
59- < CheckboxContext . Provider value = { { isCheckbox : true , name, getBoxId : this . getBoxId } } >
60- < div className = { classNames ( 'nhsuk-checkboxes' , className ) } id = { id } { ...rest } >
61- { error && typeof error === 'string' ? (
62- < ErrorMessage id = { id ? `${ id } --error` : undefined } > { error } </ ErrorMessage >
63- ) : null }
64- { children }
65- </ div >
66- </ CheckboxContext . Provider >
80+ < >
81+ < LabelBlock
82+ elementId = { id }
83+ label = { label }
84+ labelProps = { labelProps }
85+ error = { error }
86+ errorProps = { errorProps }
87+ hint = { hint }
88+ hintProps = { hintProps }
89+ />
90+ < CheckboxContext . Provider value = { { isCheckbox : true , name, getBoxId : this . getBoxId } } >
91+ < div className = { classNames ( 'nhsuk-checkboxes' , className ) } id = { id } { ...rest } >
92+ { children }
93+ </ div >
94+ </ CheckboxContext . Provider >
95+ </ >
6796 ) ;
6897 }
6998}
0 commit comments