1
- import { ComponentInternalInstance , computed , ComputedRef , defineComponent , getCurrentInstance , inject , nextTick , onMounted , reactive , ref , toRefs , watch , Prop } from 'vue' ;
1
+ import { ComponentInternalInstance , computed , ComputedRef , defineComponent , getCurrentInstance , inject , nextTick , onMounted , reactive , ref , toRefs , watch , Prop , WritableComputedRef } from 'vue' ;
2
2
import { ElForm , ElFormItem , CheckboxGroup , ICheckboxProps } from './type'
3
3
import elementOptions from '@/elementOptions'
4
4
5
-
6
- export const useCheckbox = ( props : ICheckboxProps ) => {
5
+ export const useInject = ( ) => {
7
6
const elForm = inject ( 'ElForm' , { } ) as ElForm
8
7
const elFormItem = inject ( 'elFormItem' , { } ) as ElFormItem
9
8
const checkboxGroup = inject ( 'checkboxGroup' , { } ) as CheckboxGroup
10
- const instance = getCurrentInstance ( ) as ComponentInternalInstance
11
- const { emit } = instance
9
+ const elFormItemSize = ( elFormItem || { } ) . elFormItemSize
10
+ return {
11
+ elForm,
12
+ elFormItem,
13
+ checkboxGroup,
14
+ elFormItemSize
15
+ }
16
+ }
17
+
18
+ export const useInstance = ( ) => {
19
+ const instance = getCurrentInstance ( ) as ComponentInternalInstance
20
+ const { emit, refs, attrs, type, props, vnode } = instance || { }
21
+ return { instance, emit, refs, attrs, type, props, vnode }
22
+ }
23
+
24
+ export const useState = ( props : ICheckboxProps ) => {
25
+ const { elForm, checkboxGroup, elFormItemSize } = useInject ( )
26
+ const { instance, emit } = useInstance ( )
12
27
const state = reactive ( {
13
28
selfModel : false ,
14
29
focus : false ,
15
30
isLimitExceeded : false
16
31
} )
17
-
18
32
const isChecked = computed ( ( ) => {
19
33
let result
20
34
if ( { } . toString . call ( realModelValue . value ) === '[object Boolean]' ) {
@@ -26,7 +40,6 @@ export const useCheckbox = (props: ICheckboxProps) => {
26
40
}
27
41
return result
28
42
} )
29
-
30
43
const isGroup = computed ( ( ) => {
31
44
let parent = instance . parent
32
45
while ( parent ) {
@@ -38,31 +51,24 @@ export const useCheckbox = (props: ICheckboxProps) => {
38
51
}
39
52
return false ;
40
53
} )
41
-
42
54
const store = computed ( ( ) => {
43
55
return checkboxGroup . hasOwnProperty ( 'modelValue' ) ? checkboxGroup . realModelValue . value : props . modelValue ;
44
56
} )
45
-
46
57
/* used to make the isDisabled judgment under max/min props */
47
58
const isLimitDisabled = computed ( ( ) => {
48
59
const { max, min } = checkboxGroup ;
49
60
return ! ! ( max || min ) && Array . isArray ( realModelValue . value ) && (
50
61
( realModelValue . value . length >= max && ! isChecked . value ) ||
51
62
( realModelValue . value . length <= min && isChecked . value ) ) ;
52
63
} )
53
-
54
64
const isDisabled = computed ( ( ) => {
55
65
return isGroup . value
56
66
? checkboxGroup . disabled || props . disabled || ( elForm || { } ) . disabled || isLimitDisabled . value
57
67
: props . disabled || ( elForm || { } ) . disabled ;
58
68
} )
59
-
60
- const elFormItemSize = computed ( ( ) => {
61
- return ( elFormItem || { } ) . elFormItemSize ;
62
- } )
63
- const size = computed ( ( ) => checkboxGroup ?. size || elFormItemSize . value || elementOptions . size )
69
+ const size = computed ( ( ) => checkboxGroup ?. size || elFormItemSize || elementOptions . size )
64
70
const checkboxSize = computed ( ( ) => {
65
- const temCheckboxSize = props . size || elFormItemSize . value || elementOptions . size ;
71
+ const temCheckboxSize = props . size || elFormItemSize || elementOptions . size ;
66
72
return isGroup . value
67
73
? checkboxGroup . size || temCheckboxSize
68
74
: temCheckboxSize ;
@@ -117,21 +123,43 @@ export const useCheckbox = (props: ICheckboxProps) => {
117
123
} ) ;
118
124
ev . stopPropagation ( ) ;
119
125
}
120
-
121
- watch ( ( ) => props . modelValue , ( value ) => {
122
- elFormItem . emitter ?. emit ( 'el.form.change' , value )
123
- } )
124
-
125
- props . checked && addToStore ( )
126
126
return {
127
127
state,
128
+ isGroup,
128
129
isDisabled,
129
130
isChecked,
130
131
checkboxSize,
131
132
realModelValue,
132
133
size,
133
- handleChange,
134
- checkboxGroup,
135
- instance,
134
+ addToStore,
135
+ handleChange
136
+ }
137
+ }
138
+
139
+ const whenCreated = ( props : ICheckboxProps , realModelValue : WritableComputedRef < unknown > ) => {
140
+ const { elFormItem } = useInject ( )
141
+ watch ( ( ) => props . modelValue , ( value ) => {
142
+ elFormItem . emitter ?. emit ( 'el.form.change' , value )
143
+ } )
144
+ const addToStore = ( ) => {
145
+ if (
146
+ Array . isArray ( realModelValue . value ) &&
147
+ realModelValue . value . indexOf ( props . label ) === - 1
148
+ ) {
149
+ realModelValue . value . push ( props . label ) ;
150
+ } else {
151
+ realModelValue . value = props . checked || true
152
+ }
153
+ }
154
+ props . checked && addToStore ( )
155
+ }
156
+
157
+ export const useCheckbox = ( props : ICheckboxProps ) => {
158
+ const { realModelValue, ...states } = useState ( props )
159
+ whenCreated ( props , realModelValue )
160
+ return {
161
+ ...states ,
162
+ realModelValue
136
163
}
137
- }
164
+ }
165
+ export default useCheckbox
0 commit comments