From a8d825bce8c0097b95a7189883f1583945584bed Mon Sep 17 00:00:00 2001 From: zouhang Date: Mon, 31 Aug 2020 09:51:33 +0800 Subject: [PATCH] feat(checkbox): optimize code logic --- packages/checkbox/src/checkbox-button.vue | 44 ++++++++----------- packages/checkbox/src/checkbox-group.vue | 8 ++-- packages/checkbox/src/checkbox.d.ts | 32 ++++++++++++++ packages/checkbox/src/checkbox.vue | 53 ++++++++++------------- packages/checkbox/src/useCheckbox.ts | 51 +++++++++++++++++++--- 5 files changed, 122 insertions(+), 66 deletions(-) create mode 100644 packages/checkbox/src/checkbox.d.ts diff --git a/packages/checkbox/src/checkbox-button.vue b/packages/checkbox/src/checkbox-button.vue index 55484031a6..99fa95e25e 100644 --- a/packages/checkbox/src/checkbox-button.vue +++ b/packages/checkbox/src/checkbox-button.vue @@ -56,17 +56,17 @@ import { PropType, watch, } from 'vue' -import { useCheckbox } from './useCheckbox' +import { useCheckboxGroup } from './useCheckbox' export default defineComponent({ name: 'ElCheckboxButton', props: { modelValue: { - type: [Object, Boolean, String, Number] as PropType | boolean | number>, + type: [Boolean, Number, String] as PropType, default: () => undefined, }, label: { - type: [Object, Boolean, String] as PropType | boolean | string>, + type: [Boolean, Number, String] as PropType, }, indeterminate: Boolean, disabled: Boolean, @@ -86,11 +86,11 @@ export default defineComponent({ }, emits: ['update:modelValue', 'change'], setup(props, { emit }) { - const { elForm, isGroup, _checkboxGroup, _elFormItemSize, elFormItem, ELEMENT } = useCheckbox() - const selfModel = ref(false) + let selfModel = false + const { elForm, isGroup, checkboxGroup, elFormItemSize, elFormItem, ELEMENT } = useCheckboxGroup() const focus = ref(false) const isLimitExceeded = ref(false) - const store = computed(() => _checkboxGroup ? _checkboxGroup.modelValue.value : props.modelValue) + const store = computed(() => checkboxGroup ? checkboxGroup.modelValue.value : props.modelValue) const model = computed({ get() { return isGroup.value ? store.value : props.modelValue !== undefined ? props.modelValue : selfModel @@ -100,17 +100,17 @@ export default defineComponent({ if (isGroup.value) { isLimitExceeded.value = false - if (_checkboxGroup.min !== undefined && val.length < _checkboxGroup.min) { + if (checkboxGroup.min !== undefined && val.length < checkboxGroup.min) { isLimitExceeded.value = true } - if (_checkboxGroup.max !== undefined && val.length > _checkboxGroup.max) { + if (checkboxGroup.max !== undefined && val.length > checkboxGroup.max) { isLimitExceeded.value = true } - isLimitExceeded.value === false && _checkboxGroup.changeEvent?.(val) + isLimitExceeded.value === false && checkboxGroup.changeEvent?.(val) } else { emit('update:modelValue', val) - selfModel.value = val + selfModel = val } }, }) @@ -124,27 +124,27 @@ export default defineComponent({ } }) const isLimitDisabled = computed(() => { - const max = _checkboxGroup.max - const min = _checkboxGroup.min + const max = checkboxGroup.max + const min = checkboxGroup.min return !!(max || min) && (model.value.length >= max && !isChecked.value) || (model.value.length <= min && isChecked.value) }) const isDisabled = computed(() => { return isGroup.value - ? _checkboxGroup.disabled || props.disabled || (elForm as any || {} as any).disabled || isLimitDisabled.value + ? checkboxGroup.disabled || props.disabled || (elForm as any || {} as any).disabled || isLimitDisabled.value : props.disabled || (elForm as any || {} as any).disabled }) const activeStyle = computed(() => { return { - backgroundColor: _checkboxGroup.fill || '', - borderColor: _checkboxGroup.fill || '', - color: _checkboxGroup.textColor || '', - 'box-shadow': '-1px 0 0 0 ' + _checkboxGroup.fill, + backgroundColor: checkboxGroup.fill || '', + borderColor: checkboxGroup.fill || '', + color: checkboxGroup.textColor || '', + 'box-shadow': '-1px 0 0 0 ' + checkboxGroup.fill, } }) - const size = computed(() => _checkboxGroup.checkboxGroupSize || _elFormItemSize || (ELEMENT || {}).size) + const size = computed(() => checkboxGroup.checkboxGroupSize || elFormItemSize || (ELEMENT || {}).size) function addToStore() { if ( @@ -167,14 +167,6 @@ export default defineComponent({ } emit('change', value.value, e) - /** - * to discuss it's useful - */ - // nextTick(() => { - // if (isGroup.value) { - // _checkboxGroup.changeEvent?.(_checkboxGroup.modelValue.value) - // } - // }) } watch(() => props.modelValue, val => { diff --git a/packages/checkbox/src/checkbox-group.vue b/packages/checkbox/src/checkbox-group.vue index 588e7e845f..d2bc0f4ca4 100644 --- a/packages/checkbox/src/checkbox-group.vue +++ b/packages/checkbox/src/checkbox-group.vue @@ -6,7 +6,7 @@