From 82ae87bacc64aaedae63e91c90075ed2ec6580a1 Mon Sep 17 00:00:00 2001 From: RUNZE LU <36724300+lurunze1226@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:16:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Table=E7=BB=84=E4=BB=B6=E5=BC=80?= =?UTF-8?q?=E5=90=AFcheckOnItemClick=E5=90=8E=E6=97=A0=E6=B3=95=E9=99=90?= =?UTF-8?q?=E5=88=B6=E9=80=89=E6=8B=A9=E4=B8=8A=E9=99=90=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#5632)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/src/store/table.ts | 39 ++++++++++++++++++- .../amis/src/renderers/Table/TableRow.tsx | 10 +++-- packages/amis/src/renderers/Table/index.tsx | 2 +- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/amis-core/src/store/table.ts b/packages/amis-core/src/store/table.ts index 875ca4ef4..e1ba4f423 100644 --- a/packages/amis-core/src/store/table.ts +++ b/packages/amis-core/src/store/table.ts @@ -202,6 +202,40 @@ export const Row = types return table && table.itemDraggableOn ? evalExpression(table.itemDraggableOn, (self as IRow).locals) : true; + }, + + /** + * 判断当前行点击后是否应该继续触发check + * 用于限制checkOnItemClick触发的check事件 + */ + get isCheckAvaiableOnClick(): boolean { + const table = getParent(self, self.depth * 2) as ITableStore; + const keepItemSelectionOnPageChange = + table?.keepItemSelectionOnPageChange; + const selectionUpperLimit = table?.maxKeepItemSelectionLength; + + // 如果未做配置,或者配置不合法直接通过检查 + if ( + !keepItemSelectionOnPageChange || + !Number.isInteger(selectionUpperLimit) + ) { + return true; + } + + // 使用内置ID,不会重复 + const selectedIds = (table?.selectedRows ?? []).map( + (item: IRow) => item.id + ); + // 此时syncSelected还没有触发,所以需要比较点击之后的数量 + const selectedCount = selectedIds.includes(self.id) + ? selectedIds.length - 1 + : selectedIds.length + 1; + + if (selectedCount > selectionUpperLimit) { + return false; + } + + return true; } })) .actions(self => ({ @@ -688,7 +722,8 @@ export const TableStore = iRendererStore return getHovedRow(); }, - get disabledHeadCheckbox() { + /** 已选择item是否达到数量上限 */ + get isSelectionThresholdReached() { const selectedLength = self.data?.selectedItems?.length; const maxLength = self.maxKeepItemSelectionLength; @@ -696,7 +731,7 @@ export const TableStore = iRendererStore return false; } - return maxLength === selectedLength; + return maxLength <= selectedLength; }, get firstToggledColumnIndex() { diff --git a/packages/amis/src/renderers/Table/TableRow.tsx b/packages/amis/src/renderers/Table/TableRow.tsx index 5f4b69e0a..c5e87c4ca 100644 --- a/packages/amis/src/renderers/Table/TableRow.tsx +++ b/packages/amis/src/renderers/Table/TableRow.tsx @@ -1,12 +1,13 @@ import {observer} from 'mobx-react'; import React from 'react'; +import uniq from 'lodash/uniq'; import type {IColumn, IRow} from 'amis-core/lib/store/table'; import {RendererProps} from 'amis-core'; import {Action} from '../Action'; import {isClickOnInput, createObject} from 'amis-core'; interface TableRowProps extends Pick { - onCheck: (item: IRow) => void; + onCheck: (item: IRow) => Promise; classPrefix: string; renderCell: ( region: string, @@ -42,7 +43,8 @@ export class TableRow extends React.Component { return; } - const {itemAction, onAction, item, data, dispatchEvent} = this.props; + const {itemAction, onAction, item, data, dispatchEvent, onCheck} = + this.props; const rendererEvent = await dispatchEvent( 'rowClick', @@ -59,7 +61,9 @@ export class TableRow extends React.Component { onAction && onAction(e, itemAction, item?.data); item.toggle(); } else { - this.props.onCheck(this.props.item); + if (item.checkable && item.isCheckAvaiableOnClick) { + onCheck?.(item); + } } } diff --git a/packages/amis/src/renderers/Table/index.tsx b/packages/amis/src/renderers/Table/index.tsx index 089c2dfdb..fae164942 100644 --- a/packages/amis/src/renderers/Table/index.tsx +++ b/packages/amis/src/renderers/Table/index.tsx @@ -1876,7 +1876,7 @@ export default class Table extends React.Component { classPrefix={ns} partial={store.someChecked && !store.allChecked} checked={store.someChecked} - disabled={store.disabledHeadCheckbox} + disabled={store.isSelectionThresholdReached} onChange={this.handleCheckAll} /> ) : (