1- import { computed , inject , SetupContext , toRef , provide } from 'vue' ;
1+ import { computed , inject , SetupContext , toRef , provide , watch } from 'vue' ;
2+ import { FORM_TOKEN , FORM_ITEM_TOKEN } from '../../form' ;
23import {
34 CheckboxProps ,
45 UseCheckboxFn ,
@@ -9,6 +10,8 @@ import {
910} from './checkbox-types' ;
1011
1112export function useCheckbox ( props : CheckboxProps , ctx : SetupContext ) : UseCheckboxFn {
13+ const formContext = inject ( FORM_TOKEN , undefined ) ;
14+ const formItemContext = inject ( FORM_ITEM_TOKEN , undefined ) ;
1215 const checkboxGroupConf = inject ( checkboxGroupInjectionKey , null ) ;
1316
1417 const isChecked = computed ( ( ) => props . checked || props . modelValue ) ;
@@ -20,7 +23,7 @@ export function useCheckbox(props: CheckboxProps, ctx: SetupContext): UseCheckbo
2023 return ! ! max && checkboxGroupConf ?. modelValue . value . length >= max && ! mergedChecked . value ;
2124 } ) ;
2225 const mergedDisabled = computed ( ( ) => {
23- return checkboxGroupConf ?. disabled . value || props . disabled || isLimitDisabled . value ;
26+ return checkboxGroupConf ?. disabled . value || props . disabled || formContext ?. disabled || isLimitDisabled . value ;
2427 } ) ;
2528 const mergedIsShowTitle = computed ( ( ) => {
2629 return checkboxGroupConf ?. isShowTitle . value ?? props . isShowTitle ;
@@ -59,8 +62,14 @@ export function useCheckbox(props: CheckboxProps, ctx: SetupContext): UseCheckbo
5962 const handleClick = ( ) => {
6063 canChange ( ! isChecked . value , props . label ) . then ( ( res ) => res && toggle ( ) ) ;
6164 } ;
62- const size = computed ( ( ) => checkboxGroupConf ?. size . value ?? props . size ) ;
65+ const size = computed ( ( ) => formContext ?. size || checkboxGroupConf ?. size . value || props . size ) ;
6366 const border = computed ( ( ) => checkboxGroupConf ?. border . value ?? props . border ) ;
67+ watch (
68+ ( ) => props . modelValue ,
69+ ( ) => {
70+ formItemContext ?. validate ( 'change' ) . catch ( ( err ) => console . warn ( err ) ) ;
71+ }
72+ ) ;
6473 return {
6574 mergedChecked,
6675 mergedDisabled,
@@ -76,6 +85,7 @@ export function useCheckbox(props: CheckboxProps, ctx: SetupContext): UseCheckbo
7685}
7786
7887export function useCheckboxGroup ( props : CheckboxGroupProps , ctx : SetupContext ) : UseCheckboxGroupFn {
88+ const formItemContext = inject ( FORM_ITEM_TOKEN , undefined ) ;
7989 const valList = toRef ( props , 'modelValue' ) ;
8090
8191 const defaultOpt = {
@@ -117,6 +127,13 @@ export function useCheckboxGroup(props: CheckboxGroupProps, ctx: SetupContext):
117127 return valList . value . some ( ( item ) => item . value === itemVal ) ;
118128 }
119129 } ;
130+ watch (
131+ ( ) => props . modelValue ,
132+ ( ) => {
133+ formItemContext ?. validate ( 'change' ) . catch ( ( err ) => console . warn ( err ) ) ;
134+ } ,
135+ { deep : true }
136+ ) ;
120137
121138 provide ( checkboxGroupInjectionKey , {
122139 disabled : toRef ( props , 'disabled' ) ,
0 commit comments