import { h, computed, inject, PropType, reactive, onMounted } from 'vue' import { defineVxeComponent } from '../../ui/src/comp' import XEUtils from 'xe-utils' import { getFuncText } from '../../ui/src/utils' import { VxeUI } from '@vxe-ui/core' import { createComponentLog } from '../../ui/src/log' import type { VxeCheckboxConstructor, VxeCheckboxGroupConstructor, CheckboxReactData, VxeCheckboxEmits, ValueOf, VxeCheckboxGroupPrivateMethods, CheckboxMethods, VxeCheckboxPropTypes, VxeFormConstructor, VxeFormPrivateMethods, VxeFormDefines } from '../../../types' const { getConfig, createEvent, useFns, getIcon, renderEmptyElement } = VxeUI const { warnLog } = createComponentLog('checkbox') export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeCheckbox', props: { modelValue: [String, Number, Boolean] as PropType, /** * 已废弃,被 checkedValue 替换 * @deprecated */ label: { type: [String, Number] as PropType, default: null }, indeterminate: Boolean as PropType, title: [String, Number] as PropType, checkedValue: { type: [String, Number, Boolean] as PropType, default: true }, uncheckedValue: { type: [String, Number, Boolean] as PropType, default: false }, content: [String, Number] as PropType, disabled: { type: Boolean as PropType, default: null }, size: { type: String as PropType, default: () => getConfig().checkbox.size || getConfig().size } }, emits: ['update:modelValue', 'change'] as VxeCheckboxEmits, setup(props, context) { const { slots, emit } = context const $xeForm = inject<(VxeFormConstructor & VxeFormPrivateMethods) | null>('$xeForm', null) const formItemInfo = inject('xeFormItemInfo', null) const $xeCheckboxGroup = inject<(VxeCheckboxGroupConstructor & VxeCheckboxGroupPrivateMethods) | null>('$xeCheckboxGroup', null) const xID = XEUtils.uniqueId() const reactData: CheckboxReactData = reactive({}) const $xeCheckbox = { xID, props, context, reactData } as unknown as VxeCheckboxConstructor let checkboxMethods = {} as CheckboxMethods const { computeSize } = useFns.useSize(props) const computeCheckValue = computed(() => { const { checkedValue, label } = props return XEUtils.isUndefined(checkedValue) ? label : checkedValue }) const computeIsChecked = computed(() => { const checkValue = computeCheckValue.value if ($xeCheckboxGroup) { return XEUtils.includes($xeCheckboxGroup.props.modelValue, checkValue) } return props.modelValue === props.checkedValue }) const computeIsDisabled = computed(() => { const { disabled } = props const isChecked = computeIsChecked.value if (disabled === null) { if ($xeCheckboxGroup) { const { computeIsDisabled: computeIsGroupDisabled, computeIsReadonly: computeIsGroupReadonly, computeIsMaximize: computeIsGroupMaximize } = $xeCheckboxGroup.getComputeMaps() const isGroupDisabled = computeIsGroupDisabled.value const isGroupReadonly = computeIsGroupReadonly.value const isGroupMaximize = computeIsGroupMaximize.value return isGroupDisabled || isGroupReadonly || (isGroupMaximize && !isChecked) } } return disabled }) const changeEvent = (evnt: Event & { target: { checked: boolean } }) => { const { uncheckedValue } = props const isDisabled = computeIsDisabled.value const checkValue = computeCheckValue.value if (!isDisabled) { const checked = evnt.target.checked if ($xeCheckboxGroup) { const value = checkValue const params = { checked, value, label: value } $xeCheckboxGroup.handleChecked(params, evnt) } else { const value = checked ? checkValue : uncheckedValue const params = { checked, value, label: value } emit('update:modelValue', value) checkboxMethods.dispatchEvent('change', params, evnt) // 自动更新校验状态 if ($xeForm && formItemInfo) { $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value) } } } } const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $checkbox: $xeCheckbox }, params)) } checkboxMethods = { dispatchEvent } Object.assign($xeCheckbox, checkboxMethods) const renderVN = () => { const { content } = props const vSize = computeSize.value const isDisabled = computeIsDisabled.value const isChecked = computeIsChecked.value const checkValue = computeCheckValue.value const indeterminate = !isChecked && props.indeterminate const defaultSlot = slots.default if ($xeCheckboxGroup) { const { computeIsReadonly: computeIsGroupReadonly } = $xeCheckboxGroup.getComputeMaps() const isGroupReadonly = computeIsGroupReadonly.value if (isGroupReadonly) { if (isChecked) { return h( 'label', { class: [ 'vxe-checkbox vxe-checkbox--view', { [`size--${vSize}`]: vSize, 'is--readonly': isGroupReadonly } ] }, [ h( 'span', { class: 'vxe-checkbox--label' }, defaultSlot ? defaultSlot({}) : getFuncText(content) ) ] ) } return renderEmptyElement($xeCheckbox) } } return h( 'label', { key: `${checkValue}`, class: [ 'vxe-checkbox vxe-checkbox--default', { [`size--${vSize}`]: vSize, 'is--indeterminate': indeterminate, 'is--disabled': isDisabled, 'is--checked': isChecked } ], title: props.title }, [ h('input', { class: 'vxe-checkbox--input', type: 'checkbox', disabled: isDisabled, checked: isChecked, onChange: changeEvent }), h('span', { class: ['vxe-checkbox--icon', indeterminate ? getIcon().CHECKBOX_INDETERMINATE : isChecked ? getIcon().CHECKBOX_CHECKED : isDisabled ? getIcon().CHECKBOX_DISABLED_UNCHECKED : getIcon().CHECKBOX_UNCHECKED] }), h( 'span', { class: 'vxe-checkbox--label' }, defaultSlot ? defaultSlot({}) : getFuncText(content) ) ] ) } onMounted(() => { if (props.label !== null) { warnLog('vxe.error.delProp', ['label', 'checked-value']) } }) $xeCheckbox.renderVN = renderVN return $xeCheckbox }, render() { return this.renderVN() } }) /* define-vxe-component end */