Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/record is not a data record #330

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bambooapp/bamboo-molecules",
"version": "0.3.23",
"version": "0.3.24",
"repository": {
"type": "git",
"url": "git+https://github.com/webbeetechnologies/bamboo-molecules.git"
Expand Down
16 changes: 8 additions & 8 deletions src/datagrid/hooks/useHandleKeydownEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
usePluginsDataStoreRef,
} from '../plugins';
import { useNormalizeSelectionHandler } from '../plugins/utils';
import { isDataRow, GroupRecord, getRecordByIndex } from '../utils';
import { GroupRecord, getRecordByIndex } from '../utils';
import type { TableManagerContextType } from '../contexts/TableManagerContext';

export type Props = {
Expand Down Expand Up @@ -67,6 +67,7 @@ const useHandleKeydownEvents = ({ ref }: Props) => {
const continuePaste = beforePasteCell({
selection,
});

if (osKeyMap.paste && continuePaste !== false) {
onPasteCell({
selection: normalizeSelectionForGrouping(selection, tableManagerStore),
Expand Down Expand Up @@ -102,19 +103,18 @@ const normalizeSelectionForGrouping = (
store: React.MutableRefObject<TableManagerContextType>,
) => {
// Normalize selection in case of grouping
const getRowData = (index: number): GroupRecord => {
const record = getRecordByIndex(store.current.records, index);
if (!isDataRow(record)) throw new Error('Record is not a data row');
return record;
};
const getRowData = (index: number) =>
getRecordByIndex(store.current.records, index) as GroupRecord;

const {
index: startIndex,
indexInGroup: startRowIndexInGroup,
indexInGroup: startRowIndexInGroup = startIndex,
groupConstants,
} = getRowData(start.rowIndex);

const { index: endIndex, indexInGroup: endRowIndexInGroup } = getRowData(end.rowIndex);
const { index: endIndex, indexInGroup: endRowIndexInGroup = endIndex } = getRowData(
end.rowIndex,
);

return {
...rest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const useSetFocusCellPluginStore = () => {
if (focusedCell === null) {
const shouldContinue = beforeUnFocusCell({
cell: pluginsStoreRef.current[CELL_FOCUS_PLUGIN_KEY]?.focusedCell
? normalizeCell(pluginsStoreRef.current[CELL_FOCUS_PLUGIN_KEY]?.focusedCell)
? normalizeCell(
pluginsStoreRef.current[CELL_FOCUS_PLUGIN_KEY]?.focusedCell,
) ?? null
: null,
});

Expand Down
2 changes: 1 addition & 1 deletion src/datagrid/plugins/cell-selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const useOnDragAndSelectStart = () => {
[CELL_SELECTION_PLUGIN_KEY]: {
...prev[CELL_SELECTION_PLUGIN_KEY],
start: normalizeCell(cell),
isSelecting: true,
isSelecting: !!normalizeCell(cell),
},
}));
},
Expand Down
2 changes: 1 addition & 1 deletion src/datagrid/plugins/drag-and-extend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const useOnDragEnd = () => {

if (!start && !end) return;

if (!start || !end) {
if (!start || !end || !normalizeCell(start) || !normalizeCell(end)) {
setPluginsDataStore(() => ({
[DRAG_AND_EXTEND_PLUGIN_KEY]: undefined,
}));
Expand Down
2 changes: 1 addition & 1 deletion src/datagrid/plugins/plugins-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const defaultPlugins = [] as PluginsManagerContextType['plugins'];
const defaultContextValue = {};

const withPluginsData =
<P,>(Component: ComponentType<P>) =>
<P extends {}>(Component: ComponentType<P>) =>
(props: P) => {
return (
<PluginsDataContextProvider value={defaultContextValue}>
Expand Down
35 changes: 20 additions & 15 deletions src/datagrid/plugins/utils/useNormalizeCellHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,32 @@ const useNormalizeCellHandler = () => {
const getDataRow = (index: number) => {
const record = getRecordByIndex(tableManagerStore.current.records, index);
if (!isDataRow(record)) throw new Error('Record is not a data row');

return record;
};

const record = getDataRow(rowIndex);
try {
const record = getDataRow(rowIndex);

// Drop row id entirely. Let the component consumer be responsible for clearing the state when records are removed.
// // for the case when the record is deleted
// if (!rowId && !record.id) return undefined;
// Drop row id entirely. Let the component consumer be responsible for clearing the state when records are removed.
// // for the case when the record is deleted
// if (!rowId && !record.id) return undefined;

// for focused cell, we can't just check the indices, we have to check with the ids to see if it exists or not
// it's allowed for the record to be undefined thus we do not check for rowId.
if (columnId && dataTableStore.current.columns[columnIndex] !== columnId)
return undefined;
// for focused cell, we can't just check the indices, we have to check with the ids to see if it exists or not
// it's allowed for the record to be undefined thus we do not check for rowId.
if (columnId && dataTableStore.current.columns[columnIndex] !== columnId)
return undefined;

return {
columnIndex,
rowIndex: record.index,
rowId: rowId ?? tableManagerStore.current.getRowId(rowIndex) ?? record.id,
record,
columnId: dataTableStore.current.columns[columnIndex],
};
return {
columnIndex,
rowIndex: record.index,
rowId: rowId ?? tableManagerStore.current.getRowId(rowIndex) ?? record.id,
record,
columnId: dataTableStore.current.columns[columnIndex],
};
} catch {
return;
}
},
[tableManagerStore, dataTableStore],
);
Expand Down