import {computed, designComponent, getComponentCls, inject, useCollect, useNumber, useRefs} from "@peryl/react-compose"; import {EditProps, useEdit} from "../../uses/useEdit"; import {StyleProps, ThemeStatus, useStyle} from "../../uses/useStyle"; import {CheckboxStatus} from "../../utils/constant"; import Checkbox from "../Checkbox"; import {useMessage} from "../useMessage"; import {i18n} from "../../i18n"; import {useMultipleModel} from "../../uses/useMultipleModel"; import {createIsMatch, IsMatchPropsOption} from "../../utils/isObjectCommonMatch"; export const CheckboxGroup = designComponent({ name: 'checkbox-group', props: { ...EditProps, ...StyleProps, ...IsMatchPropsOption, modelValue: {}, multiple: { type: Boolean, default: true, }, // 是否多选 min: { type: Number }, // 最大勾选个数 max: { type: Number }, // 最小勾选个数 itemWidth: { type: [String, Number] }, // 文本宽度 }, inheritPropsType: HTMLDivElement, slots: ['default'], emits: { onUpdateModelValue: (val: any) => true, }, setup({ props, slots, event: { emit } }) { const { refs, onRef } = useRefs({ el: HTMLDivElement }); const formItem = inject('@@form-item', null); const { $message } = useMessage(); /*可编辑控制*/ const { editComputed } = useEdit(); /*样式控制*/ useStyle({ status: ThemeStatus.primary }); /*格式化属性*/ const { numberState } = useNumber(props, ['max', 'min']); /*绑定值*/ const modelValue = useMultipleModel(() => props.modelValue, emit.onUpdateModelValue, props) as { value: any[] }; /*子组件Checkbox*/ const children = CheckboxGroupCollector.parent(); /*可用子组件*/ const availableChildren = computed(() => children.filter(({ innerState: { props: { checkboxForAll, customReadonly } } }) => !checkboxForAll && !customReadonly)); /*可用子组件keys*/ const allKeys = computed((() => availableChildren.value.map(({ innerState: { props: { val } } }) => val))); /*判断值是否相等*/ const isMatch = createIsMatch(props); /*当前选择状态*/ const checkStatus = computed(() => { let hasCheck = false; let hasUncheck = false; if (!modelValue.value) { return CheckboxStatus.uncheck; } availableChildren .value .forEach( child => modelValue.value!.findIndex(i => isMatch({ value: i, option: child.innerState.props.val })) > -1 ? hasCheck = true : hasUncheck = true ); if (hasCheck && !hasUncheck) { return CheckboxStatus.check; } if (!hasCheck && hasUncheck) { return CheckboxStatus.uncheck; } return CheckboxStatus.minus; }); const utils = { /** * 子组件用来获取状态工具函数 * @author 韦胜健 * @date 2020/11/4 11:26 */ getCheckStatus: (checkbox: typeof Checkbox.use.class): typeof CheckboxStatus.TYPE => { const { innerState: { props: { checkboxForAll, val } } } = checkbox; if (checkboxForAll) { return checkStatus.value; } else { return !!modelValue.value && modelValue.value.findIndex(i => isMatch({ value: i, option: val })) > -1 ? CheckboxStatus.check : CheckboxStatus.uncheck; } }, exceed: { /** * 是否已经超出了max * @author 韦胜健 * @date 2020/11/4 11:26 */ max: (val: (string | number)[] | undefined) => { if (numberState.max == null) { return false; } return !!val && val.length > numberState.max; }, /** * 是否已经超出了min * @author 韦胜健 * @date 2020/11/4 11:26 */ min: (val: (string | number)[] | undefined) => { if (numberState.min == null) { return false; } return !val || val.length < numberState.min; } }, changeValue: { /** * 添加选项赋值 * @author 韦胜健 * @date 2020/11/4 11:26 */ add: (val: (string | number)[]) => { if (utils.exceed.max(val)) { $message.warn({ render: () => i18n.$it('base.select.selectOptionsAtMost', { val: numberState.max }).d(`最多选择 ${numberState.max} 个选项`) }); } else { modelValue.value = val; } }, /** * 删除选项赋值 * @author 韦胜健 * @date 2020/11/4 11:26 */ delete: (val: (string | number)[]) => { if (utils.exceed.min(val)) { $message.warn({ render: () => i18n.$it('base.select.selectOptionsAtLeast', { val: numberState.min }).d(`最少选择 ${numberState.min} 个选项`) }); } else { modelValue.value = val; } } }, }; const handler = { /** * 处理点击按钮事件 * @author 韦胜健 * @date 2020/11/4 11:27 */ clickCheckbox: (checkbox: typeof Checkbox.use.class) => { if (!editComputed.value.editable) { return; } const { innerState: { props: { val, checkboxForAll } } } = checkbox; /*全选按钮*/ if (checkboxForAll) { if (checkStatus.value !== CheckboxStatus.check) { /*全选*/ utils.changeValue.add([...allKeys.value as string[]]); } else { /*取消全选*/ utils.changeValue.delete([]); } return; } /*单个按钮*/ if (val == null) { throw new Error('Checkbox: val is necessary when Checkbox in CheckboxGroup'); } if (!modelValue.value) { return utils.changeValue.add([val as any]); } const value = [...modelValue.value]; const index = value.findIndex(i => isMatch({ value: i, option: val })); if (index > -1) { /*删除*/ value.splice(index, 1); utils.changeValue.delete(value); } else { /*添加*/ utils.changeValue.add([...value, val]); } } }; return { refer: { utils, handler, numberState, props, refs, }, render: () => !formItem ? slots.default() : (