import {CheckboxProps, CheckboxGroupInstance} from "./agent"; import {getCurrentInstance, inject, computed, ref, watch} from "vue"; import {toTypeString} from '@vue/shared' import {GlobalConfig} from "@roui/utils/util"; import {UPDATE_MODEL_VALUE} from "@roui/utils/common"; import {PartialReturnType} from "@roui/utils/types"; import {FormItemKey, FormKey} from "@roui/form"; import type {FormCtx, FormItemCtx} from "@roui/form"; export const getCheckboxGroup = () => { const $EL = GlobalConfig() const Form = inject(FormKey, {} as FormCtx) const FormItem = inject(FormItemKey, {} as FormItemCtx) const checkboxGroup = inject('CheckboxGroup', {}) const judgeGroup = computed(() => checkboxGroup && checkboxGroup?.name === 'RoCheckboxGroup') const FormItemSize = computed(() => { return FormItem.size }) return { judgeGroup, checkboxGroup, $EL, Form, FormItem, FormItemSize } } const getModel = (props: CheckboxProps) => { let selfModel = false const {emit} = getCurrentInstance() const {judgeGroup, checkboxGroup} = getCheckboxGroup() const judgeLimitExceeded = ref(false) const store = computed(() => checkboxGroup ? checkboxGroup.modelValue?.value : props.modelValue) const model = computed({ get() { return judgeGroup.value ? store.value : props.modelValue ?? selfModel }, set(v: unknown) { if (judgeGroup.value && Array.isArray(v)) { judgeLimitExceeded.value = checkboxGroup.min !== undefined && v.length < checkboxGroup.min.value; if (checkboxGroup.max !== undefined && v.length > checkboxGroup.max.value) { judgeLimitExceeded.value = true } judgeLimitExceeded.value === false && checkboxGroup?.changeEvent?.(v) } else { emit(UPDATE_MODEL_VALUE, v) selfModel = v as boolean } } }) return { model, judgeLimitExceeded } } const getCheckboxStatus = (props: CheckboxProps, {model}: PartialReturnType) => { const {judgeGroup, checkboxGroup, FormItemSize, $EL} = getCheckboxGroup() const size = computed(() => checkboxGroup?.checkboxGroupSize?.value || FormItemSize.value || $EL.size) const judgeChecked = computed(() => { const v = model.value if (toTypeString(v) === '[object Boolean]') { return v } else if (Array.isArray(v)) { return v.includes(props.label) } else if (v !== null && v !== undefined) { return v === props.trueLabel } }) const checkboxSize = computed(() => { const tCheckboxSize = props.size || FormItemSize.value || $EL.value return judgeGroup.value ? checkboxGroup?.checkboxGroupSize?.value || tCheckboxSize : tCheckboxSize }) return { judgeChecked, size, checkboxSize } } const getDisabled = (props: CheckboxProps, {model, judgeChecked}: PartialReturnType & PartialReturnType) => { const {judgeGroup, checkboxGroup, Form} = getCheckboxGroup() const isLimitDisabled = computed(() => { const min = checkboxGroup.min?.value const max = checkboxGroup.max?.value return !!(max || min) && (model.value.length >= max && !judgeChecked.value) || (model.value.length <= min && judgeChecked.value) }) const judgeDisabled = computed(() => { const disabled = props.disabled || Form.disabled return judgeGroup.value ? checkboxGroup.disabled?.value || disabled || isLimitDisabled.value : props.disabled || Form.disabled }) return { judgeDisabled, isLimitDisabled } } const getEvent = (props: CheckboxProps, {judgeLimitExceeded}: PartialReturnType) => { const {FormItem} = getCheckboxGroup() const {emit} = getCurrentInstance() function handleChange(e: InputEvent) { if (judgeLimitExceeded.value) return const target = e.target as HTMLInputElement const v = target.checked ? props.trueLabel ?? true : props.falseLabel ?? false emit('change', v, e) } watch(() => props.modelValue, v => { FormItem.formItemMitt?.emit('el.form.change', [v]) }) return { handleChange } } export const useCheckbox = (props: CheckboxProps) => { const {model, judgeLimitExceeded} = getModel(props) const {judgeChecked, checkboxSize, size} = getCheckboxStatus(props, {model}) const {judgeDisabled} = getDisabled(props, {model, judgeChecked}) const {handleChange} = getEvent(props, {judgeLimitExceeded}) return { model, judgeChecked, judgeDisabled, handleChange, checkboxSize, size } }