Skip to content

Commit 24356ae

Browse files
committed
调整CheckBox
1 parent da7853c commit 24356ae

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

packages/checkbox/src/checkbox-button.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<script lang="ts">
4747
import { computed, defineComponent, inject, toRefs } from 'vue'
4848
import { ICheckboxProps } from './type'
49-
import { useCheckbox } from './checkbox'
49+
import { useCheckbox, useInject } from './useCheckbox'
5050
5151
export default defineComponent ({
5252
name: 'ElCheckboxButton',
@@ -62,14 +62,14 @@ import { useCheckbox } from './checkbox'
6262
},
6363
6464
setup(props, { slots }) {
65+
const { checkboxGroup } = useInject()
6566
const {
6667
state,
6768
size,
6869
isChecked,
6970
isDisabled,
7071
realModelValue,
7172
handleChange,
72-
checkboxGroup,
7373
} = useCheckbox(props as ICheckboxProps)
7474
const activeStyle = computed(() => {
7575
return {

packages/checkbox/src/index.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<script lang="ts">
5757
import { defineComponent, onMounted, toRefs } from 'vue'
5858
import { ICheckboxProps } from './type'
59-
import { useCheckbox } from './checkbox'
59+
import { useCheckbox, useInstance } from './useCheckbox'
6060
export default defineComponent({
6161
name: 'ElCheckbox',
6262
@@ -76,19 +76,19 @@ import { useCheckbox } from './checkbox'
7676
},
7777
7878
setup(props, { emit, slots }) {
79+
const { vnode } = useInstance()
7980
const {
8081
state,
8182
isDisabled,
8283
isChecked,
8384
checkboxSize,
84-
instance,
8585
realModelValue,
8686
handleChange
8787
} = useCheckbox(props as ICheckboxProps)
8888
8989
onMounted(() => {
9090
if (props.indeterminate) {
91-
instance.vnode.el?.setAttribute('aria-controls', props.controls)
91+
vnode.el?.setAttribute('aria-controls', props.controls)
9292
}
9393
})
9494

packages/checkbox/src/checkbox.ts renamed to packages/checkbox/src/useCheckbox.ts

+54-26
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
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';
22
import { ElForm, ElFormItem, CheckboxGroup, ICheckboxProps } from './type'
33
import elementOptions from '@/elementOptions'
44

5-
6-
export const useCheckbox = (props: ICheckboxProps) => {
5+
export const useInject = () => {
76
const elForm = inject('ElForm', {}) as ElForm
87
const elFormItem = inject('elFormItem', {}) as ElFormItem
98
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()
1227
const state = reactive({
1328
selfModel: false,
1429
focus: false,
1530
isLimitExceeded: false
1631
})
17-
1832
const isChecked = computed(() => {
1933
let result
2034
if ({}.toString.call(realModelValue.value) === '[object Boolean]') {
@@ -26,7 +40,6 @@ export const useCheckbox = (props: ICheckboxProps) => {
2640
}
2741
return result
2842
})
29-
3043
const isGroup = computed(() => {
3144
let parent = instance.parent
3245
while (parent) {
@@ -38,31 +51,24 @@ export const useCheckbox = (props: ICheckboxProps) => {
3851
}
3952
return false;
4053
})
41-
4254
const store = computed(() => {
4355
return checkboxGroup.hasOwnProperty('modelValue') ? checkboxGroup.realModelValue.value : props.modelValue;
4456
})
45-
4657
/* used to make the isDisabled judgment under max/min props */
4758
const isLimitDisabled = computed(() => {
4859
const { max, min } = checkboxGroup;
4960
return !!(max || min) && Array.isArray(realModelValue.value) && (
5061
(realModelValue.value.length >= max && !isChecked.value) ||
5162
(realModelValue.value.length <= min && isChecked.value));
5263
})
53-
5464
const isDisabled = computed(() => {
5565
return isGroup.value
5666
? checkboxGroup.disabled || props.disabled || (elForm || {}).disabled || isLimitDisabled.value
5767
: props.disabled || (elForm || {}).disabled;
5868
})
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)
6470
const checkboxSize = computed(() => {
65-
const temCheckboxSize = props.size || elFormItemSize.value || elementOptions.size;
71+
const temCheckboxSize = props.size || elFormItemSize || elementOptions.size;
6672
return isGroup.value
6773
? checkboxGroup.size || temCheckboxSize
6874
: temCheckboxSize;
@@ -117,21 +123,43 @@ export const useCheckbox = (props: ICheckboxProps) => {
117123
});
118124
ev.stopPropagation();
119125
}
120-
121-
watch(() => props.modelValue, (value) => {
122-
elFormItem.emitter?.emit('el.form.change', value)
123-
})
124-
125-
props.checked && addToStore()
126126
return {
127127
state,
128+
isGroup,
128129
isDisabled,
129130
isChecked,
130131
checkboxSize,
131132
realModelValue,
132133
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
136163
}
137-
}
164+
}
165+
export default useCheckbox

0 commit comments

Comments
 (0)