import {iPlcCheckEventType, iPlcCheckPropsType} from "./PlcCheck.utils"; import {createEffectsOfReaction, useModel} from "@peryl/react-compose"; import {PlainObject} from "@peryl/utils/event"; import {iTableRowRenderScope} from "../../table/utils/table.utils"; import {Radio} from "../../../Radio"; import {CheckboxStatus} from "../../../../utils/constant"; import {iPlcCheckHooks} from "./PlcCheck.hooks"; import {iTableRefer} from "../../table/Table"; export function usePlcCheckSingle( { props, event, hooks, table, }: { props: iPlcCheckPropsType, event: iPlcCheckEventType, hooks: iPlcCheckHooks, table: iTableRefer, } ) { const { effects } = createEffectsOfReaction(); /** * 双向绑定值 * @author 韦胜健 * @date 2022.11.27 16:36 */ const model = useModel(() => props.modelValue as PlainObject | null | undefined, event.emit.onUpdateModelValue as any, undefined, effects); const utils = { /** * 判断节点是否选中 * @author 韦胜健 * @date 2022.11.27 17:14 */ isChecked: (row: PlainObject) => { return model.value?.[props.field] === row[props.field]; }, }; const methods = { check: (row: PlainObject) => { model.value = row; }, toggleCheck: (row: PlainObject) => { methods.check(row); }, }; const handler = { onClickCheckbox: (rowRenderScope: iTableRowRenderScope) => { // /*并且判断为不可选中的话,如果已经选中则取消选中,未选中则不处理任何动作???todo*/ if (!!props.isCheckAble && !props.isCheckAble(rowRenderScope) && !utils.isChecked(rowRenderScope.row)) { return; } methods.toggleCheck(rowRenderScope.row); }, }; effects.push(hooks.onGetSelected.use(() => model.value)); effects.push(hooks.onClickRow.use(({ rowRenderScope }) => { handler.onClickCheckbox(rowRenderScope); })); effects.push(hooks.onRenderHead.use(() => (''))); effects.push(hooks.onRenderNormal.use(({ scope }) => ({ scope, content: ( !props.toggleOnClickRow && handler.onClickCheckbox(scope)} /> ) }))); effects.push(table.hooks.onRowClass.use(({ classList, node }) => { if (utils.isChecked(node.data)) { classList.push('plt-row-checked'); } })); return effects.clear; }