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

[component] Custom Mui Data Table component #155

Closed
Closed
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
117 changes: 117 additions & 0 deletions packages/components/src/custom/DataTable/custom-table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React, { useEffect } from 'react';
import { DataTable } from '../../base/DataTable';

interface Column {
name: string;
label: string;
options?: {
filter?: boolean;
sort?: boolean;
searchable?: boolean;
display?: boolean;
sortDescFirst?: boolean;
customBodyRender?: (value: string | number | boolean | object) => JSX.Element;
};
}

interface ResponsiveDataTableProps<T> {
data: T[];
columns: Column[];
options?: object;
tableCols: Column[];
updateCols: (columns: Column[]) => void;
columnVisibility: Record<string, boolean>;
theme?: object;
}

const ResponsiveDataTable: React.FC<ResponsiveDataTableProps<unknown>> = ({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nebula-aac i think unknown is not a right type, can you provide some guidance in here

data,
columns,
options = {},
tableCols,
updateCols,
columnVisibility,
...props
}) => {
const formatDate = (date: Date): string => {
const dateOptions: Intl.DateTimeFormatOptions = {
weekday: 'short',
day: 'numeric',
month: 'long',
year: 'numeric'
};

return new Intl.DateTimeFormat('en-US', dateOptions).format(date);
};

const updatedOptions = {
...options,
onViewColumnsChange: (column: string, action: string) => {
switch (action) {
case 'add': {
const colToAdd = columns.find((obj) => obj.name === column);
colToAdd.options.display = true;
updateCols([...columns]);
break;
}
case 'remove': {
const colToRemove = columns.find((obj) => obj.name === column);
colToRemove.options.display = false;
updateCols([...columns]);
break;
}
}
}
};

useEffect(() => {
columns?.forEach((col) => {
if (!col.options) {
col.options = {};
}
col.options.display = columnVisibility[col.name];

if (
['updated_at', 'created_at', 'deleted_at', 'last_login_time', 'joined_at'].includes(
col.name
)
) {
col.options.customBodyRender = (value: string | number | boolean | object) => {
if (value === 'NA') {
return <>{value}</>;
} else if (typeof value === 'object' && value.Valid === true) {
// Ensure value is an object before accessing Valid and Time
const date = new Date(value.Time as string);
return <>{formatDate(date)}</>;
} else if (typeof value === 'object' && value.Valid === false) {
// Ensure value is an object before accessing Valid
return <>NA</>;
} else if (typeof value === 'string') {
// Ensure value is a string before creating a Date object
const date = new Date(value);
return <>{formatDate(date)}</>;
} else {
return <>{value}</>;
}
};
}
});
updateCols([...columns]);
}, [columnVisibility]);
const components = {
ExpandButton: () => ''
};

return (
<DataTable
title={undefined}
columns={tableCols}
data={data}
components={components}
options={updatedOptions}
{...props}
/>
);
};

export default ResponsiveDataTable;
1 change: 1 addition & 0 deletions packages/components/src/custom/DataTable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './custom-table';
Loading