Skip to content

Commit

Permalink
Fix CSV export (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcginnes committed Apr 12, 2024
1 parent 361c4ba commit 800ac88
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 44 deletions.
6 changes: 3 additions & 3 deletions packages/graph-explorer/src/components/Tabular/Tabular.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface TabularProps<T extends object> extends TabularOptions<T> {
globalSearch?: string;
}

export const Tabular = <T extends object>(
export const Tabular = <T extends Record<string, unknown>>(
{
children,
classNamePrefix = "ft",
Expand Down Expand Up @@ -129,7 +129,7 @@ export const Tabular = <T extends object>(
);
};

const TabularContent = <T extends object>({
const TabularContent = <T extends Record<string, unknown>>({
children,
classNamePrefix = "ft",
className,
Expand Down Expand Up @@ -277,7 +277,7 @@ const TabularContent = <T extends object>({
);
};

export default forwardRef(Tabular) as <T extends object>(
export default forwardRef(Tabular) as <T extends Record<string, unknown>>(
props: PropsWithChildren<TabularProps<T>> & {
ref?: ForwardedRef<TabularInstance<T>>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

import type { TabularInstance } from "./helpers/tableInstanceToTabularInstance";

type TabularContextValue<T extends object> = {
type TabularContextValue<T extends Record<string, unknown>> = {
tableRef: RefObject<HTMLDivElement>;
instance: TabularInstance<T>;
headerControlsRef: RefObject<HTMLDivElement>;
Expand All @@ -19,7 +19,7 @@ type TabularContextValue<T extends object> = {
disablePagination?: boolean;
};

const createTabularContext = <T extends object = any>(
const createTabularContext = <T extends Record<string, unknown> = any>(
defaultValue: TabularContextValue<T>
) => {
return createContext<TabularContextValue<T>>(defaultValue);
Expand All @@ -28,7 +28,7 @@ const createTabularContext = <T extends object = any>(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const TabularContext = createTabularContext(undefined!);

const TabularControlsProvider = <T extends object>({
const TabularControlsProvider = <T extends Record<string, unknown>>({
tabularInstance,
children,
}: PropsWithChildren<{ tabularInstance: TabularInstance<T> }>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import Switch from "../../../Switch";
import type { TabularInstance } from "../../helpers/tableInstanceToTabularInstance";
import { useTabularControl } from "../../TabularControlsProvider";

type ColumnItemProps<T extends object> = {
type ColumnItemProps<T extends Record<string, unknown>> = {
classNamePrefix: string;
columnId: string;
column: TabularInstance<T>["columns"][number];
index: number;
};

const ColumnItem = <T extends object>({
const ColumnItem = <T extends Record<string, unknown>>({
classNamePrefix,
columnId,
column,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const rootStyles = () => css`
align-items: center;
`;

interface ColumnOrderControlProps<T extends object> {
interface ColumnOrderControlProps<T extends Record<string, unknown>> {
instance: TabularInstance<T>;
classNamePrefix?: string;
className?: string;
Expand All @@ -34,7 +34,7 @@ const reorder = (list: string[], startIndex: number, endIndex: number) => {
return result;
};

export const ColumnSettingsControl = <T extends object>({
export const ColumnSettingsControl = <T extends Record<string, unknown>>({
classNamePrefix = "ft",
className,
}: ColumnOrderControlProps<T>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ const rootStyles = () => css`
align-items: center;
`;

type ExportControlProps<T extends object> = {
type ExportControlProps<T extends Record<string, unknown>> = {
classNamePrefix?: string;
className?: string;
omittedColumnsIds?: string[];
instance: TabularInstance<T>;
};

export const ExternalExportControl = <T extends object>({
export const ExternalExportControl = <T extends Record<string, unknown>>({
classNamePrefix = "ft",
className,
omittedColumnsIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ import type { Row } from "react-table";
import type { TabularColumnInstance } from "../../helpers/tableInstanceToTabularInstance";
import getNestedObjectValue from "./getNestedObjectValue";

const transformToCsv = (
currentDataSource: readonly any[],
export default function transformToCsv<T extends Record<string, unknown>>(
currentDataSource: readonly T[] | Row<T>[],
selectedColumns: Record<string, boolean>,
columns: TabularColumnInstance<any>[]
) => {
const filteredRows = currentDataSource.map((row: any | Row<any>) => {
return Object.entries(selectedColumns).reduce(
(cells, [columnId, shouldExport]) => {
if (shouldExport) {
const colDef = columns.find(
colDef => colDef.instance.id === columnId
)?.definition;

if (typeof colDef?.accessor === "string") {
cells.push(
getNestedObjectValue(row.original || row, columnId.split("."))
);
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cells.push(colDef?.accessor?.(row));
columns: TabularColumnInstance<T>[]
) {
const filteredRows = currentDataSource
.map(row => (row as Row<T>).original || row)
.map(row => {
return Object.entries(selectedColumns).reduce(
(cells, [columnId, shouldExport]) => {
if (shouldExport) {
const colDef = columns.find(
colDef => colDef.instance.id === columnId
)?.definition;

if (typeof colDef?.accessor === "string") {
cells.push(getNestedObjectValue(row, columnId.split(".")));
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cells.push(colDef?.accessor?.(row));
}
}
}

return cells;
},
[] as (string | number)[]
);
}, []);
return cells;
},
[] as (string | number)[]
);
}, []);

const headers = Object.entries(selectedColumns).reduce<string[]>(
(header, [columnId, shouldExport]) => {
Expand All @@ -50,6 +50,4 @@ const transformToCsv = (
);

return unparse([headers, ...filteredRows], { header: false });
};

export default transformToCsv;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import {
import type { TabularProps } from "../Tabular";
import type { ColumnDefinition } from "../useTabular";

export type TabularColumnInstance<T extends object> = {
export type TabularColumnInstance<T extends Record<string, unknown>> = {
instance: ColumnInstance<T>;
definition?: ColumnDefinition<T>;
};

export type TabularInstance<T extends object> = {
export type TabularInstance<T extends Record<string, unknown>> = {
/**
* Original data
*/
Expand Down Expand Up @@ -142,7 +142,7 @@ export type TabularInstance<T extends object> = {
initialHiddenColumns: Array<IdType<T>>;
};

const tableInstanceToTabularInstance = <T extends object>(
const tableInstanceToTabularInstance = <T extends Record<string, unknown>>(
tableInstance: TableInstance<T>,
tableProps: TabularProps<T>
): TabularInstance<T> => {
Expand Down

0 comments on commit 800ac88

Please sign in to comment.