11import React , {
22 createContext ,
3- FC ,
43 FieldsetHTMLAttributes ,
54 useCallback ,
65 useContext ,
@@ -10,6 +9,7 @@ import React, {
109import classnames from 'classnames' ;
1110import { FormElementProps } from './FormElement' ;
1211import { FieldSetColumnContext } from './FieldSet' ;
12+ import { createFC } from './common' ;
1313
1414/**
1515 *
@@ -36,87 +36,91 @@ export type CheckboxGroupProps = {
3636/**
3737 *
3838 */
39- export const CheckboxGroup : FC < CheckboxGroupProps > = ( props ) => {
40- const {
41- className,
42- label,
43- cols,
44- style,
45- required,
46- error,
47- onValueChange,
48- onChange : onChange_ ,
49- children,
50- ...rprops
51- } = props ;
52- const { totalCols } = useContext ( FieldSetColumnContext ) ;
53- const controlElRef = useRef < HTMLDivElement | null > ( null ) ;
39+ export const CheckboxGroup = createFC <
40+ CheckboxGroupProps ,
41+ { isFormElement : boolean }
42+ > (
43+ ( props ) => {
44+ const {
45+ className,
46+ label,
47+ cols,
48+ style,
49+ required,
50+ error,
51+ onValueChange,
52+ onChange : onChange_ ,
53+ children,
54+ ...rprops
55+ } = props ;
56+ const { totalCols } = useContext ( FieldSetColumnContext ) ;
57+ const controlElRef = useRef < HTMLDivElement | null > ( null ) ;
5458
55- const onChange = useCallback (
56- ( e : React . FormEvent < HTMLFieldSetElement > ) => {
57- if ( onValueChange ) {
58- const checkboxes =
59- controlElRef . current ?. querySelectorAll < HTMLInputElement > (
60- 'input[type=checkbox]'
61- ) ;
62- if ( ! checkboxes ) {
63- return ;
59+ const onChange = useCallback (
60+ ( e : React . FormEvent < HTMLFieldSetElement > ) => {
61+ if ( onValueChange ) {
62+ const checkboxes =
63+ controlElRef . current ?. querySelectorAll < HTMLInputElement > (
64+ 'input[type=checkbox]'
65+ ) ;
66+ if ( ! checkboxes ) {
67+ return ;
68+ }
69+ const values = [ ...checkboxes ]
70+ . filter ( ( checkbox ) => checkbox . checked )
71+ . map ( ( checkbox ) => checkbox . value ) ;
72+ onValueChange ?.( values ) ;
6473 }
65- const values = [ ...checkboxes ]
66- . filter ( ( checkbox ) => checkbox . checked )
67- . map ( ( checkbox ) => checkbox . value ) ;
68- onValueChange ?.( values ) ;
69- }
70- onChange_ ?.( e ) ;
71- } ,
72- [ onChange_ , onValueChange ]
73- ) ;
74+ onChange_ ?.( e ) ;
75+ } ,
76+ [ onChange_ , onValueChange ]
77+ ) ;
7478
75- const grpClassNames = classnames (
76- className ,
77- 'slds-form-element' ,
78- {
79- 'slds-has-error' : error ,
80- 'slds-is-required' : required ,
81- } ,
82- typeof totalCols === 'number'
83- ? `slds-size_${ cols || 1 } -of-${ totalCols } `
84- : null
85- ) ;
86- const grpStyles =
87- typeof totalCols === 'number'
88- ? { display : 'inline-block' , ...style }
89- : style ;
90- const errorMessage = error
91- ? typeof error === 'string'
92- ? error
93- : typeof error === 'object'
94- ? error . message
95- : undefined
96- : undefined ;
97- const grpCtx = useMemo ( ( ) => ( { grouped : true } ) , [ ] ) ;
79+ const grpClassNames = classnames (
80+ className ,
81+ 'slds-form-element' ,
82+ {
83+ 'slds-has-error' : error ,
84+ 'slds-is-required' : required ,
85+ } ,
86+ typeof totalCols === 'number'
87+ ? `slds-size_${ cols || 1 } -of-${ totalCols } `
88+ : null
89+ ) ;
90+ const grpStyles =
91+ typeof totalCols === 'number'
92+ ? { display : 'inline-block' , ...style }
93+ : style ;
94+ const errorMessage = error
95+ ? typeof error === 'string'
96+ ? error
97+ : typeof error === 'object'
98+ ? error . message
99+ : undefined
100+ : undefined ;
101+ const grpCtx = useMemo ( ( ) => ( { grouped : true } ) , [ ] ) ;
98102
99- return (
100- < fieldset
101- className = { grpClassNames }
102- style = { grpStyles }
103- { ...rprops }
104- onChange = { onChange }
105- >
106- < legend className = 'slds-form-element__label' >
107- { label }
108- { required ? < abbr className = 'slds-required' > *</ abbr > : undefined }
109- </ legend >
110- < div className = 'slds-form-element__control' ref = { controlElRef } >
111- < CheckboxGroupContext . Provider value = { grpCtx } >
112- { children }
113- </ CheckboxGroupContext . Provider >
114- { errorMessage ? (
115- < div className = 'slds-form-element__help' > { errorMessage } </ div >
116- ) : undefined }
117- </ div >
118- </ fieldset >
119- ) ;
120- } ;
121-
122- ( CheckboxGroup as unknown as { isFormElement ?: boolean } ) . isFormElement = true ;
103+ return (
104+ < fieldset
105+ className = { grpClassNames }
106+ style = { grpStyles }
107+ { ...rprops }
108+ onChange = { onChange }
109+ >
110+ < legend className = 'slds-form-element__label' >
111+ { label }
112+ { required ? < abbr className = 'slds-required' > *</ abbr > : undefined }
113+ </ legend >
114+ < div className = 'slds-form-element__control' ref = { controlElRef } >
115+ < CheckboxGroupContext . Provider value = { grpCtx } >
116+ { children }
117+ </ CheckboxGroupContext . Provider >
118+ { errorMessage ? (
119+ < div className = 'slds-form-element__help' > { errorMessage } </ div >
120+ ) : undefined }
121+ </ div >
122+ </ fieldset >
123+ ) ;
124+ } ,
125+ { isFormElement : true }
126+ ) ;
0 commit comments