Skip to content

Commit

Permalink
fix: fix checkbox and radio state update when change row index #1712
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui-Sun committed May 17, 2024
1 parent e56bb5d commit 98bb289
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vtable",
"comment": "fix: fix checkbox and radio state update when change row index #1712",
"type": "none"
}
],
"packageName": "@visactor/vtable"
}
1 change: 1 addition & 0 deletions packages/vtable/src/state/cell-move/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function endMoveCol(state: StateManager) {
)
) {
state.table.changeRecordOrder(moveContext.sourceIndex, moveContext.targetIndex);
state.changeCheckboxAndRadioOrder(moveContext.sourceIndex, moveContext.targetIndex);
}
// clear columns width and rows height cache
if (moveContext.moveType === 'column') {
Expand Down
16 changes: 16 additions & 0 deletions packages/vtable/src/state/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,19 @@ export function setCellCheckboxState(col: number, row: number, checked: boolean,
}
}
}

export function changeCheckboxOrder(sourceIndex: number, targetIndex: number, state: StateManager) {
const { checkedState, table } = state;
if (table.internalProps.transpose) {
sourceIndex = table.getRecordShowIndexByCell(sourceIndex, 0);
targetIndex = table.getRecordShowIndexByCell(targetIndex, 0);
} else {
sourceIndex = table.getRecordShowIndexByCell(0, sourceIndex);
targetIndex = table.getRecordShowIndexByCell(0, targetIndex);
}
if (sourceIndex !== targetIndex) {
const sourceRecord = checkedState[sourceIndex];
checkedState[sourceIndex] = checkedState[targetIndex];
checkedState[targetIndex] = sourceRecord;
}
}
16 changes: 16 additions & 0 deletions packages/vtable/src/state/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,19 @@ export function setCellRadioState(col: number, row: number, index: number | unde
radio?._handlePointerUp();
}
}

export function changeRadioOrder(sourceIndex: number, targetIndex: number, state: StateManager) {
const { radioState, table } = state;
if (table.internalProps.transpose) {
sourceIndex = table.getRecordShowIndexByCell(sourceIndex, 0);
targetIndex = table.getRecordShowIndexByCell(targetIndex, 0);
} else {
sourceIndex = table.getRecordShowIndexByCell(0, sourceIndex);
targetIndex = table.getRecordShowIndexByCell(0, targetIndex);
}
if (sourceIndex !== targetIndex) {
const sourceRecord = radioState[sourceIndex];
radioState[sourceIndex] = radioState[targetIndex];
radioState[targetIndex] = sourceRecord;
}
}
12 changes: 11 additions & 1 deletion packages/vtable/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import { getIconAndPositionFromTarget } from '../scenegraph/utils/icon';
import type { BaseTableAPI, HeaderData } from '../ts-types/base-table';
import { debounce } from '../tools/debounce';
import { updateResizeColumn } from './resize/update-resize-column';
import { setRadioState, syncRadioState } from './radio/radio';
import { changeRadioOrder, setRadioState, syncRadioState } from './radio/radio';
import {
changeCheckboxOrder,
initCheckedState,
initLeftRecordsCheckState,
setCheckedState,
Expand Down Expand Up @@ -1364,4 +1365,13 @@ export class StateManager {
) {
return syncRadioState(col, row, field, radioType, indexInCell, isChecked, this);
}

changeCheckboxAndRadioOrder(sourceIndex: number, targetIndex: number) {
if (this.checkedState.length) {
changeCheckboxOrder(sourceIndex, targetIndex, this);
}
if (this.radioState.length) {
changeRadioOrder(sourceIndex, targetIndex, this);
}
}
}

0 comments on commit 98bb289

Please sign in to comment.