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: add accessor key to select column when data is undefined [MDS-1321] #211

Merged
merged 9 commits into from
Oct 11, 2024
5 changes: 5 additions & 0 deletions .changeset/polite-rice-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heathmont/moon-table-v8-tw": patch
---

fix: add accessor key to select column to avoid crashing when no data is present [MDS-1321]
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"prettier.printWidth": 2,
"eslint.workingDirectories": [
"./docs"
]
],
"editor.defaultFormatter": "esbenp.prettier-vscode"
MarioGranada marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 1 addition & 2 deletions packages/table-v8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
},
"dependencies": {
"@heathmont/moon-core-tw": "^10.17.3",
"@tanstack/react-table": "^8.9.11",
"@tanstack/react-table": "^8.20.5",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {},
"peerDependencies": {
"react": ">=16",
"react-dom": ">=16"
Expand Down
151 changes: 65 additions & 86 deletions packages/table-v8/src/components/TD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Cell } from "../private/types";
import type ClipProps from "../private/types/ClipProps";
import type StickyColumn from "../private/types/StickyColumn";
import type TDProps from "../private/types/TDProps";
import TDContent from "./TDContent";

const getStickyShift = (
cells: Cell<{}, unknown>[],
Expand All @@ -31,94 +32,72 @@ const getStickyShift = (
}
};

const TD = forwardRef<HTMLTableCellElement, TDProps>(
(
{
cell,
index,
cells,
rowSize,
noGap,
className,
isFirstColumn,
isLastColumn,
columnData,
textClip,
withBorder,
},
ref,
) => {
const stickyColumn: StickyColumn = cell.column.parent
? cell.column.parent?.columnDef
: cell.column.columnDef;
const stickySide = stickyColumn.sticky;
const TD = forwardRef<HTMLTableCellElement, TDProps>((props, ref) => {
const {
cell,
index,
cells,
rowSize,
noGap,
className,
isFirstColumn,
isLastColumn,
columnData,
textClip,
withBorder,
} = props;
const stickyColumn: StickyColumn = cell.column.parent
? cell.column.parent?.columnDef
: cell.column.columnDef;
const stickySide = stickyColumn.sticky;

const styles = new Map([
["width", `${cell.column.getSize()}px`],
[
"minWidth",
// prettier-ignore
`${stickySide ? cell.column.columnDef.size : cell.column.columnDef.minSize}px`,
],
[
"maxWidth",
// prettier-ignore
`${stickySide ? cell.column.columnDef.size : cell.column.columnDef.maxSize}px`,
],
]);
const styles = new Map([
["width", `${cell.column.getSize()}px`],
[
"minWidth",
// prettier-ignore
`${stickySide ? cell.column.columnDef.size : cell.column.columnDef.minSize}px`,
],
[
"maxWidth",
// prettier-ignore
`${stickySide ? cell.column.columnDef.size : cell.column.columnDef.maxSize}px`,
],
]);

if (stickySide) {
styles.set(
stickySide,
stickySide === "left"
? // prettier-ignore
`${columnData ? columnData?.left : getStickyShift(cells, index, "left")}px`
: // prettier-ignore
`${columnData ? columnData?.right : getStickyShift(cells, index, "right")}px`,
);
}

return (
<td
key={cell.id}
style={Object.fromEntries(styles)}
className={mergeClassnames(
"relative box-border text-start",
getFontSize(rowSize),
getPadding(rowSize),
isFirstColumn && !noGap && "rounded-s-lg after:rounded-s-lg",
isLastColumn && !noGap && "rounded-e-lg after:rounded-e-lg",
stickySide && "sticky z-[1] before:-z-[1] after:-z-[1]",
stickySide &&
"before:absolute before:top-0 before:left-0 before:-right-[1px] before:h-full",
stickySide &&
"after:absolute after:top-0 after:left-0 after:-right-[1px] after:h-full",
className,
)}
ref={ref}
>
{
<CellBorder
withBorder={withBorder}
isFirstColumn={isFirstColumn}
stickySide={stickySide}
/>
}
{textClip ? (
<div
className={mergeClassnames(
textClip === ("clip" as ClipProps) && "break-all truncate",
textClip === ("break" as ClipProps) && "break-all text-clip",
)}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</div>
) : (
flexRender(cell.column.columnDef.cell, cell.getContext())
)}
</td>
if (stickySide) {
styles.set(
stickySide,
stickySide === "left"
? // prettier-ignore
`${columnData ? columnData?.left : getStickyShift(cells, index, "left")}px`
: // prettier-ignore
`${columnData ? columnData?.right : getStickyShift(cells, index, "right")}px`,
);
},
);
}

return (
<td
key={cell.id}
style={Object.fromEntries(styles)}
className={mergeClassnames(
"relative box-border text-start",
getFontSize(rowSize),
getPadding(rowSize),
isFirstColumn && !noGap && "rounded-s-lg after:rounded-s-lg",
isLastColumn && !noGap && "rounded-e-lg after:rounded-e-lg",
stickySide && "sticky z-[1] before:-z-[1] after:-z-[1]",
stickySide &&
"before:absolute before:top-0 before:left-0 before:-right-[1px] before:h-full",
stickySide &&
"after:absolute after:top-0 after:left-0 after:-right-[1px] after:h-full",
className,
)}
ref={ref}
>
<TDContent {...props} stickySide={stickySide} />
</td>
);
});

export default TD;
27 changes: 27 additions & 0 deletions packages/table-v8/src/components/TDContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { FC } from "react";
import TDProps from "../private/types/TDProps";
import CellBorder from "./CellBorder";
import TDFlexRender from "./TDFlexRender";

type Props = TDProps & {
stickySide?: string;
};

const TDContent: FC<Props> = (props) => {
const { isFirstColumn, stickySide, withBorder } = props;

return (
<>
<CellBorder
withBorder={withBorder}
isFirstColumn={isFirstColumn}
stickySide={stickySide}
/>

<TDFlexRender {...props} />
</>
);
};

export default TDContent;
32 changes: 32 additions & 0 deletions packages/table-v8/src/components/TDFlexRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { FC } from "react";
import { mergeClassnames } from "@heathmont/moon-core-tw";
import type ClipProps from "../private/types/ClipProps";
import TDProps from "../private/types/TDProps";
import { flexRender } from "../private/utils";

const TDFlexRender: FC<TDProps> = ({ cell, textClip }) => {
if (cell.getValue() === undefined) {
return null;
}

const flexRenderedCell = flexRender(
cell.column.columnDef.cell,
cell.getContext(),
);

if (textClip) {
return (
<div
className={mergeClassnames(
textClip === ("clip" as ClipProps) && "break-all truncate",
textClip === ("break" as ClipProps) && "break-all text-clip",
)}
>
{flexRenderedCell}
</div>
);
}
return flexRenderedCell;
};

export default TDFlexRender;
32 changes: 30 additions & 2 deletions packages/table-v8/src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Minimap from "./Minimap";
import type { ColumnResizeMode } from "../private/types";
import type ColumnData from "../private/types/ColumnData";
import type TableProps from "../private/types/TableProps";
import DataHelper from "../private/types/DataHelper";

const Table = ({
columns,
Expand Down Expand Up @@ -60,10 +61,37 @@ const Table = ({
React.useState<ColumnResizeDirection>('ltr')
*/

const curatedData: DataHelper[] = isSelectable
? data.map((dataItem) => ({
...dataItem,
select: dataItem.select ?? false,
}))
: data;

const selectColumnIndex = isSelectable
? columns.findIndex((column) => column.id === "select")
: -1;

const selectColumn =
selectColumnIndex > -1
? {
...columns[selectColumnIndex],
accessorKey: "select",
}
: undefined;

const curatedColumns = selectColumn
? [
...columns.slice(0, selectColumnIndex),
{ ...selectColumn },
...columns.slice(selectColumnIndex + 1),
]
: columns;

const table = useReactTable({
columns,
columns: curatedColumns,
columnResizeMode,
data,
data: curatedData,
defaultColumn,
state,
enableColumnResizing: isResizable,
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ datetime
datetimelocal
dodoria
Draghandle
esbenp
fraunces
frieza
frontmatter
Expand Down