import { defineComponent, h, computed, inject, PropType } from 'vue' import Nutils from 'n-table-utils' import { getFuncText } from '../../tools/utils' import GlobalConfig from '../../v-x-e-table/src/conf' import { useSize } from '../../hooks/size' import { VxeCheckboxConstructor, VxeCheckboxGroupConstructor, VxeCheckboxEmits, VxeCheckboxGroupPrivateMethods, CheckboxMethods, VxeCheckboxPropTypes, VxeFormConstructor, VxeFormPrivateMethods, VxeFormDefines } from '../../../types/all' export default defineComponent({ name: 'VxeCheckbox', props: { modelValue: [String, Number, Boolean] as PropType, 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: Boolean as PropType, size: { type: String as PropType, default: () => GlobalConfig.checkbox.size || GlobalConfig.size } }, emits: [ 'update:modelValue', 'change' ] as VxeCheckboxEmits, setup (props, context) { const { slots, emit } = context const $xeform = inject('$xeform', null) const $xeformiteminfo = inject('$xeformiteminfo', null) const xID = Nutils.uniqueId() const $xecheckbox = { xID, props, context } as unknown as VxeCheckboxConstructor let checkboxMethods = {} as CheckboxMethods const computeSize = useSize(props) const $xecheckboxgroup = inject('$xecheckboxgroup', null as (VxeCheckboxGroupConstructor & VxeCheckboxGroupPrivateMethods) | null) const computeDisabled = computed(() => { return props.disabled || ($xecheckboxgroup && $xecheckboxgroup.props.disabled) }) const computeChecked = computed(() => { return $xecheckboxgroup ? Nutils.includes($xecheckboxgroup.props.modelValue, props.label) : props.modelValue === props.checkedValue }) const changeEvent = (evnt: Event & { target: { checked: boolean } }) => { const { checkedValue, uncheckedValue } = props const isDisabled = computeDisabled.value if (!isDisabled) { const checked = evnt.target.checked const value = checked ? checkedValue : uncheckedValue const params = { checked, value, label: props.label } if ($xecheckboxgroup) { $xecheckboxgroup.handleChecked(params, evnt) } else { emit('update:modelValue', value) checkboxMethods.dispatchEvent('change', params, evnt) // 自动更新校验状态 if ($xeform && $xeformiteminfo) { $xeform.triggerItemEvent(evnt, $xeformiteminfo.itemConfig.field, value) } } } } checkboxMethods = { dispatchEvent (type, params, evnt) { emit(type, Object.assign({ $checkbox: $xecheckbox, $event: evnt }, params)) } } Object.assign($xecheckbox, checkboxMethods) const renderVN = () => { const vSize = computeSize.value const isDisabled = computeDisabled.value return h('label', { class: ['vxe-checkbox', { [`size--${vSize}`]: vSize, 'is--indeterminate': props.indeterminate, 'is--disabled': isDisabled }], title: props.title }, [ h('input', { class: 'vxe-checkbox--input', type: 'checkbox', disabled: isDisabled, checked: computeChecked.value, onChange: changeEvent }), h('span', { class: 'vxe-checkbox--icon' }), h('span', { class: 'vxe-checkbox--label' }, slots.default ? slots.default({}) : getFuncText(props.content)) ]) } $xecheckbox.renderVN = renderVN return $xecheckbox }, render () { return this.renderVN() } })