import { ref, h, PropType, inject, reactive, computed } from 'vue' import { defineVxeComponent } from '../../ui/src/comp' import XEUtils from 'xe-utils' import { VxeUI } from '@vxe-ui/core' import type { RateReactData, VxeRateEmits, RateMethods, VxeRatePropTypes, RatePrivateMethods, VxeFormDefines, ValueOf, VxeFormConstructor, VxeFormPrivateMethods, RatePrivateRef, VxeRatePrivateComputed, VxeRateConstructor, VxeRatePrivateMethods } from '../../../types' const { getConfig, getIcon, createEvent, useFns } = VxeUI export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeRate', props: { modelValue: [Number, String] as PropType, disabled: { type: Boolean as PropType, default: null }, readonly: { type: Boolean as PropType, default: null }, size: { type: String as PropType, default: () => getConfig().rate.size || getConfig().size }, status: String as PropType }, emits: ['update:modelValue', 'change'] as VxeRateEmits, setup(props, context) { const { emit } = context const $xeForm = inject<(VxeFormConstructor & VxeFormPrivateMethods) | null>('$xeForm', null) const formItemInfo = inject('xeFormItemInfo', null) const xID = XEUtils.uniqueId() const { computeSize } = useFns.useSize(props) const refElem = ref() const reactData = reactive({ activeValue: null }) const refMaps: RatePrivateRef = { refElem } const computeIsDisabled = computed(() => { const { disabled } = props if (disabled === null) { if ($xeForm) { return $xeForm.props.readonly || $xeForm.props.disabled } return false } return disabled }) const computeIsReadonly = computed(() => { const { readonly } = props if (readonly === null) { if ($xeForm) { return $xeForm.props.readonly || $xeForm.props.disabled } return false } return readonly }) const computeNumVal = computed(() => { const { modelValue } = props const { activeValue } = reactData return XEUtils.toNumber(activeValue === null ? modelValue : activeValue) }) const computeItemList = computed(() => { return [1, 2, 3, 4, 5].map((num) => { return { value: num, label: num } }) }) const computeMaps: VxeRatePrivateComputed = {} const $xeRate = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeRateConstructor & VxeRatePrivateMethods const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $rate: $xeRate }, params)) } const rateMethods: RateMethods = { dispatchEvent } const ratePrivateMethods: RatePrivateMethods = {} const emitModel = (value: any) => { emit('update:modelValue', value) } const mouseenterEvent = (evnt: MouseEvent, item: any) => { const isDisabled = computeIsDisabled.value const isReadonly = computeIsReadonly.value if (!(isDisabled || isReadonly)) { const value = item.value reactData.activeValue = value } } const mouseleaveEvent = () => { reactData.activeValue = null } const clickEvent = (evnt: MouseEvent, item: any) => { const isDisabled = computeIsDisabled.value const isReadonly = computeIsReadonly.value if (!(isDisabled || isReadonly)) { const value = item.value emitModel(value) dispatchEvent('change', { value }, evnt) // 自动更新校验状态 if ($xeForm && formItemInfo) { $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value) } } } Object.assign($xeRate, rateMethods, ratePrivateMethods) const renderVN = () => { const { status } = props const isDisabled = computeIsDisabled.value const isReadonly = computeIsReadonly.value const itemList = computeItemList.value const vSize = computeSize.value const numVal = computeNumVal.value return h( 'div', { ref: refElem, class: [ 'vxe-rate', { [`size--${vSize}`]: vSize, [`theme--${status}`]: status, 'is--disabled': isDisabled, 'is--readonly': isReadonly } ] }, itemList.map((item) => { const isChecked = numVal >= item.value return h( 'div', { class: [ 'vxe-rte--item', { 'is--checked': isChecked } ], onMouseenter(evnt) { if (!(isDisabled || isReadonly)) { mouseenterEvent(evnt, item) } }, onMouseleave: mouseleaveEvent, onClick(evnt) { if (!(isDisabled || isReadonly)) { clickEvent(evnt, item) } } }, [ h('i', { class: isChecked ? getIcon().RATE_CHECKED : getIcon().RATE_UNCHECKED }) ] ) }) ) } $xeRate.renderVN = renderVN return $xeRate }, render() { return this.renderVN() } }) /* define-vxe-component end */