Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions public/r/data-grid.json

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions src/components/data-grid/data-grid-cell-variants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,10 @@ export function MultiSelectCell<TData>({
isEditing,
isSelected,
}: CellVariantProps<TData>) {
const cellValue = React.useMemo(
() => (cell.getValue() as string[]) ?? [],
[cell],
);
const cellValue = React.useMemo(() => {
const value = cell.getValue() as string[];
return value ?? [];
}, [cell]);

const cellKey = getCellKey(rowIndex, columnId);
const prevCellKeyRef = React.useRef(cellKey);
Expand All @@ -908,9 +908,14 @@ export function MultiSelectCell<TData>({
const options = cellOpts?.variant === "multi-select" ? cellOpts.options : [];
const sideOffset = -(containerRef.current?.clientHeight ?? 0);

const prevCellValueRef = React.useRef(cellValue);
if (cellValue !== prevCellValueRef.current) {
prevCellValueRef.current = cellValue;
setSelectedValues(cellValue);
}

if (prevCellKeyRef.current !== cellKey) {
prevCellKeyRef.current = cellKey;
Comment thread
sadmann7 marked this conversation as resolved.
setSelectedValues(cellValue);
setSearchValue("");
}

Expand All @@ -922,7 +927,6 @@ export function MultiSelectCell<TData>({

setSelectedValues(newValues);
meta?.onDataUpdate?.({ rowIndex, columnId, value: newValues });
// Clear search input and focus back on input after selection
setSearchValue("");
queueMicrotask(() => inputRef.current?.focus());
},
Expand Down Expand Up @@ -1337,9 +1341,16 @@ export function FileCell<TData>({
[accept],
);

// Sync files when cellValue changes (same pattern as other cells)
const prevCellValueRef = React.useRef(cellValue);
if (cellValue !== prevCellValueRef.current) {
prevCellValueRef.current = cellValue;
setFiles(cellValue);
setError(null);
}
Comment thread
sadmann7 marked this conversation as resolved.

if (prevCellKeyRef.current !== cellKey) {
prevCellKeyRef.current = cellKey;
setFiles(cellValue);
setError(null);
}

Expand Down
18 changes: 16 additions & 2 deletions src/components/data-grid/data-grid-context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,32 @@ function ContextMenuImpl<TData>({
return;

const updates: Array<UpdateCell> = [];
const tableColumns = table.getAllColumns();

for (const cellKey of selectionState.selectedCells) {
const { rowIndex, columnId } = parseCellKey(cellKey);
updates.push({ rowIndex, columnId, value: "" });

const column = tableColumns.find((col) => col.id === columnId);
const cellVariant = column?.columnDef?.meta?.cell?.variant;

let emptyValue: unknown = "";
if (cellVariant === "multi-select" || cellVariant === "file") {
emptyValue = [];
} else if (cellVariant === "number" || cellVariant === "date") {
emptyValue = null;
} else if (cellVariant === "checkbox") {
emptyValue = false;
}

updates.push({ rowIndex, columnId, value: emptyValue });
}

onDataUpdate?.(updates);

toast.success(
`${updates.length} cell${updates.length !== 1 ? "s" : ""} cleared`,
);
}, [onDataUpdate, selectionState]);
}, [onDataUpdate, selectionState, table]);

const onDelete = React.useCallback(async () => {
if (
Expand Down
19 changes: 18 additions & 1 deletion src/hooks/use-data-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1122,15 +1122,32 @@ function useDataGrid<TData>({
if (key === "Delete" || key === "Backspace") {
if (currentState.selectionState.selectedCells.size > 0) {
event.preventDefault();

const updates: Array<{
rowIndex: number;
columnId: string;
value: unknown;
}> = [];

const currentTable = tableRef.current;
const tableColumns = currentTable?.getAllColumns() ?? [];

currentState.selectionState.selectedCells.forEach((cellKey) => {
const { rowIndex, columnId } = parseCellKey(cellKey);
updates.push({ rowIndex, columnId, value: "" });

const column = tableColumns.find((col) => col.id === columnId);
const cellVariant = column?.columnDef?.meta?.cell?.variant;

let emptyValue: unknown = "";
if (cellVariant === "multi-select" || cellVariant === "file") {
emptyValue = [];
} else if (cellVariant === "number" || cellVariant === "date") {
emptyValue = null;
} else if (cellVariant === "checkbox") {
emptyValue = false;
}

updates.push({ rowIndex, columnId, value: emptyValue });
});

onDataUpdate(updates);
Expand Down
Loading