Skip to content

Commit ae9ae2d

Browse files
authored
Fix issue with column deletion (#1104)
1 parent 4043721 commit ae9ae2d

File tree

2 files changed

+80
-27
lines changed

2 files changed

+80
-27
lines changed

packages/core/src/data-editor/stories/data-editor.stories.tsx

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,7 @@ export const SimpleEditable = () => {
713713
};
714714

715715
export function GroupHeaderActionClick() {
716-
const cols = [
717-
{ title: "Col1", width: 100, grow: 1, group: "Group"},
718-
];
716+
const cols = [{ title: "Col1", width: 100, grow: 1, group: "Group" }];
719717

720718
const [clickCount, setClickCount] = useState(0);
721719

@@ -727,16 +725,72 @@ export function GroupHeaderActionClick() {
727725
height={500}
728726
rows={0}
729727
columns={cols}
730-
getCellContent={() => ({ kind: GridCellKind.Text, data: '', displayData: '',allowOverlay: false })}
731-
getGroupDetails={(name) => ({
732-
name,
733-
actions: [{
734-
icon: 'headerString',
735-
title: "Action",
736-
onClick: (e) => setClickCount(c => c + 1 ),
737-
}]
728+
getCellContent={() => ({ kind: GridCellKind.Text, data: "", displayData: "", allowOverlay: false })}
729+
getGroupDetails={name => ({
730+
name,
731+
actions: [
732+
{
733+
icon: "headerString",
734+
title: "Action",
735+
onClick: _e => setClickCount(c => c + 1),
736+
},
737+
],
738738
})}
739739
/>
740740
</div>
741741
);
742742
}
743+
744+
export function DeleteColumnsViaOnDelete() {
745+
const [headers, setHeaders] = useState<string[]>(["col-a", "col-b"]);
746+
747+
const dynColumns: GridColumn[] = headers.map(value => ({
748+
id: value,
749+
title: value,
750+
width: 100,
751+
}));
752+
753+
const renderCell = React.useCallback((): GridCell => {
754+
return { kind: GridCellKind.Loading, allowOverlay: false };
755+
}, []);
756+
757+
const addColumn = () => {
758+
const str = Math.random().toString(36).slice(2, 8);
759+
setHeaders(prev => [...prev, str]);
760+
};
761+
762+
const removeColumns = (indices: number[]) => {
763+
setHeaders(prev => prev.filter((_, i) => !indices.includes(i)));
764+
};
765+
766+
const [selection, setSelection] = useState<GridSelection | undefined>(undefined);
767+
768+
const onDelete = (sel: GridSelection) => {
769+
if (sel.columns.length > 0) {
770+
setSelection(undefined);
771+
removeColumns(sel.columns.toArray());
772+
return false;
773+
}
774+
return true;
775+
};
776+
777+
return (
778+
<div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
779+
<div style={{ marginBottom: 8 }}>
780+
<button onClick={addColumn}>Add</button>
781+
</div>
782+
<div style={{ flex: 1, position: "relative" }}>
783+
<DataEditor
784+
width="100%"
785+
height="100%"
786+
columns={dynColumns}
787+
rows={0}
788+
getCellContent={renderCell}
789+
gridSelection={selection}
790+
onGridSelectionChange={setSelection}
791+
onDelete={onDelete}
792+
/>
793+
</div>
794+
</div>
795+
);
796+
}

packages/core/src/internal/data-grid/data-grid.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,12 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
952952
const [overFill, setOverFill] = React.useState(false);
953953

954954
const [hCol, hRow] = hoveredItem ?? [];
955-
const headerHovered = hCol !== undefined && hRow === -1 && columns[hCol].headerRowMarkerDisabled !== true;
955+
const headerHovered =
956+
hCol !== undefined &&
957+
hRow === -1 &&
958+
hCol >= 0 &&
959+
hCol < mappedColumns.length &&
960+
mappedColumns[hCol].headerRowMarkerDisabled !== true;
956961
const groupHeaderHovered = hCol !== undefined && hRow === -2;
957962
let clickableInnerCellHovered = false;
958963
let editableBoolHovered = false;
@@ -969,14 +974,14 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
969974
const cursor = isDragging
970975
? "grabbing"
971976
: canDrag || isResizing
972-
? "col-resize"
973-
: overFill || isFilling
974-
? "crosshair"
975-
: cursorOverride !== undefined
976-
? cursorOverride
977-
: headerHovered || clickableInnerCellHovered || editableBoolHovered || groupHeaderHovered
978-
? "pointer"
979-
: "default";
977+
? "col-resize"
978+
: overFill || isFilling
979+
? "crosshair"
980+
: cursorOverride !== undefined
981+
? cursorOverride
982+
: headerHovered || clickableInnerCellHovered || editableBoolHovered || groupHeaderHovered
983+
? "pointer"
984+
: "default";
980985

981986
const style = React.useMemo(
982987
() => ({
@@ -1239,13 +1244,7 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
12391244
}
12401245
}
12411246
},
1242-
[
1243-
eventTargetRef,
1244-
getMouseArgsForPosition,
1245-
isOverHeaderElement,
1246-
onHeaderMenuClick,
1247-
onHeaderIndicatorClick,
1248-
]
1247+
[eventTargetRef, getMouseArgsForPosition, isOverHeaderElement, onHeaderMenuClick, onHeaderIndicatorClick]
12491248
);
12501249
useEventListener("click", onClickImpl, windowEventTarget, false);
12511250

0 commit comments

Comments
 (0)