11import type { DId } from '../../utils/types' ;
22import type { DFormControl } from '../form' ;
3+ import type { DCheckboxItem } from './CheckboxGroupRenderer' ;
34
45import { getClassName } from '@react-devui/utils' ;
56
6- import { useGeneralContext , useDValue } from '../../hooks' ;
7- import { cloneHTMLElement , registerComponentMate } from '../../utils' ;
8- import { useFormControl } from '../form' ;
7+ import { registerComponentMate } from '../../utils' ;
98import { useComponentConfig , usePrefixConfig } from '../root' ;
10- import { DCheckbox } from './Checkbox ' ;
9+ import { DCheckboxGroupRenderer } from './CheckboxGroupRenderer ' ;
1110
12- export interface DCheckboxItem < V extends DId > {
13- label : React . ReactNode ;
14- value : V ;
15- disabled ?: boolean ;
16- }
1711export interface DCheckboxGroupProps < V extends DId > extends Omit < React . HTMLAttributes < HTMLDivElement > , 'children' > {
1812 dFormControl ?: DFormControl ;
1913 dModel ?: V [ ] ;
@@ -38,48 +32,26 @@ export function DCheckboxGroup<V extends DId>(props: DCheckboxGroupProps<V>): JS
3832
3933 //#region Context
4034 const dPrefix = usePrefixConfig ( ) ;
41- const { gDisabled } = useGeneralContext ( ) ;
4235 //#endregion
4336
44- const formControlInject = useFormControl ( dFormControl ) ;
45- const [ value , changeValue ] = useDValue < V [ ] > ( [ ] , dModel , onModelChange , undefined , formControlInject ) ;
46-
47- const disabled = dDisabled || gDisabled || dFormControl ?. control . disabled ;
48-
4937 return (
50- < div
51- { ...restProps }
52- className = { getClassName ( restProps . className , `${ dPrefix } checkbox-group` , {
53- [ `${ dPrefix } checkbox-group--vertical` ] : dVertical ,
54- } ) }
55- role = "group"
56- >
57- { dList . map ( ( item , index ) => (
58- < DCheckbox
59- key = { item . value }
60- dDisabled = { item . disabled || disabled }
61- dInputRender = { ( el ) =>
62- cloneHTMLElement ( el , {
63- [ 'data-form-item-label-for' as string ] : index === 0 ,
64- } )
65- }
66- dModel = { value . includes ( item . value ) }
67- onModelChange = { ( checked ) => {
68- changeValue ( ( draft ) => {
69- if ( checked ) {
70- draft . push ( item . value ) ;
71- } else {
72- draft . splice (
73- draft . findIndex ( ( v ) => v === item . value ) ,
74- 1
75- ) ;
76- }
77- } ) ;
78- } }
38+ < DCheckboxGroupRenderer
39+ dFormControl = { dFormControl }
40+ dList = { dList }
41+ dModel = { dModel }
42+ dDisabled = { dDisabled }
43+ dRender = { ( nodes ) => (
44+ < div
45+ { ...restProps }
46+ className = { getClassName ( restProps . className , `${ dPrefix } checkbox-group` , {
47+ [ `${ dPrefix } checkbox-group--vertical` ] : dVertical ,
48+ } ) }
49+ role = "group"
7950 >
80- { item . label }
81- </ DCheckbox >
82- ) ) }
83- </ div >
51+ { nodes }
52+ </ div >
53+ ) }
54+ onModelChange = { onModelChange }
55+ />
8456 ) ;
8557}
0 commit comments