Skip to content

Commit

Permalink
PLAT-3985 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Imad A. Bakir committed Dec 3, 2024
1 parent 67da159 commit 01e4a9d
Show file tree
Hide file tree
Showing 36 changed files with 1,087 additions and 322 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"check:types": "tsc --noEmit",
"lint:style": "stylelint 'src/**/*.scss'",
"lint:script": "biome lint",
"lint": "npm run lint:style && npm run lint:script",
"lint": "npm run lint:style; npm run lint:script",
"test:unit": "vitest run --root=.",
"test": "npm run check:types; npm run lint:style; npm run test:unit",
"test-ui": "vitest --ui",
Expand Down Expand Up @@ -91,7 +91,8 @@
"lightningcss": "^1.28.1",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc",
"@types/react-table": "^7.7.20"
"@types/react-table": "^7.7.20",
"@ngneat/falso": "^6.4.0"
},
"dependencies": {
"date-fns": "^4.1.0",
Expand Down
5 changes: 4 additions & 1 deletion package.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ const packageConfig = {
'@types/react-dom': 'npm:types-react-dom@rc',

'@types/react-table': '^7.7.20',

// Fake data
"@ngneat/falso": "^6.4.0",
},

// Dependencies needed when running the generated build
Expand Down Expand Up @@ -187,4 +190,4 @@ const packageConfigWithComment = {
const packageConfigFormatted = JSON.stringify(packageConfigWithComment, null, 2);

// Write to `package.json`
fs.writeFileSync('./package.json', packageConfigFormatted + '\n');
fs.writeFileSync('./package.json', `${packageConfigFormatted}\n`);
2 changes: 1 addition & 1 deletion src/components/tables/DataTable/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Story = StoryObj<typeof DataTable>;


import * as ReactTable from 'react-table';
import { createTableContext, TableContextState } from './DataTableContext.tsx';
import { createTableContext, type TableContextState } from './DataTableContext.tsx';
const DataTableContext = ({ children }: React.PropsWithChildren) => {
type User = { name: string };
const columns = React.useMemo<Array<ReactTable.Column<User>>>(() => [
Expand Down
2 changes: 1 addition & 1 deletion src/components/tables/DataTable/DataTableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import * as React from 'react';
import * as ReactTable from 'react-table';
import type * as ReactTable from 'react-table';


export type DataTableStatus = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tables/DataTable/DataTableEager.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@use '../../../style/variables.scss' as bkl;
// @use '../../../style/variables.scss' as bkl;
@use './DataTableLazy.scss' as dataTableLazy;


Expand Down
211 changes: 211 additions & 0 deletions src/components/tables/DataTable/DataTableEager.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import cx from 'classnames';
import { differenceInDays } from 'date-fns';
import React from 'react';

import { delay } from '../util/async_util.ts';
import { type User, generateData } from '../util/generateData.ts';
import { useEffectAsync } from '../util/hooks.ts';
import * as Filtering from './filtering/Filtering.ts';

import { Panel } from '../../containers/Panel/Panel.tsx';
import * as DataTablePlugins from './plugins/useRowSelectColumn.tsx';
import * as DataTableEager from './DataTableEager.tsx';


const columns = [
{
id: 'name',
accessor: (user: User) => user.name,
Header: 'Name',
Cell: ({ value }: { value: string }) => value,
disableSortBy: false,
disableGlobalFilter: false,
},
{
id: 'email',
accessor: (user: User) => user.email,
Header: 'Email',
disableSortBy: false,
disableGlobalFilter: false,
},
{
id: 'company',
accessor: (user: User) => user.company,
Header: 'Company',
disableSortBy: false,
disableGlobalFilter: false,
},
{
id: 'joinDate',
accessor: (user: User) => user.joinDate,
Header: 'Joined',
Cell: ({ value }: { value: Date }) =>
value.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }),
disableSortBy: false,
disableGlobalFilter: false,
},
];

const fields = {
name: {
type: 'text',
operators: ['$text'],
label: 'Name',
placeholder: 'Search name',
},
email: {
type: 'text',
operators: ['$text'],
label: 'Email',
placeholder: 'Search email',
},
company: {
type: 'text',
operators: ['$text'],
label: 'Company',
placeholder: 'Search company',
},
joinDate: {
type: 'datetime',
operators: ['$lt', '$lte', '$gt', '$gte', '$range'],
label: 'Joined',
placeholder: 'Search by joined date',
},
daysActive: {
type: 'number',
operators: ['$eq', '$ne', '$lt', '$lte', '$gt', '$gte'],
label: 'Days active',
placeholder: 'Number of days active',
accessor: (user: User) => differenceInDays(new Date(), user.joinDate),
},
};

type dataTeableEagerTemplateProps = DataTableEager.TableProviderEagerProps<User> & { delay: number };
const DataTableEagerTemplate = (props: dataTeableEagerTemplateProps) => {
const memoizedColumns = React.useMemo(() => props.columns, [props.columns]);
const memoizedItems = React.useMemo(() => props.items, [props.items]);

const [isReady, setIsReady] = React.useState(props.isReady ?? true);

useEffectAsync(async () => {
if (typeof props.delay !== 'number' && isReady === false) return;
await delay(props.delay);
setIsReady(true);
}, [props.delay]);

return (
<Panel>
<DataTableEager.TableProviderEager
{...props}
isReady={isReady}
columns={memoizedColumns}
items={memoizedItems}
getRowId={(item: User) => item.id}
>
<DataTableEager.Search />
<DataTableEager.DataTableEager status={{ error: null, loading: false, ready: true }} />
</DataTableEager.TableProviderEager>
</Panel>
);
};

// Template: Table with Filtering
const DataTableEagerWithFilterTemplate = (props: dataTeableEagerTemplateProps) => {
const memoizedColumns = React.useMemo(() => props.columns, [props.columns]);

const [filters, setFilters] = React.useState([]);
const [filteredItems, setFilteredItems] = React.useState(props.items);

React.useEffect(() => {
const filtered = Filtering.filterByQuery(fields, props.items, filters);

Check failure on line 124 in src/components/tables/DataTable/DataTableEager.stories.tsx

View workflow job for this annotation

GitHub Actions / build (22.x)

Argument of type '{ name: { type: string; operators: string[]; label: string; placeholder: string; }; email: { type: string; operators: string[]; label: string; placeholder: string; }; company: { type: string; operators: string[]; label: string; placeholder: string; }; joinDate: { type: string; operators: string[]; label: string; placeholder: string; }; daysActive: { type: string; operators: string[]; label: string; placeholder: string; accessor: (user: User) => number; }; }' is not assignable to parameter of type 'Fields'.
setFilteredItems(Object.values(filtered));

Check failure on line 125 in src/components/tables/DataTable/DataTableEager.stories.tsx

View workflow job for this annotation

GitHub Actions / build (22.x)

Argument of type 'TypeOfFieldsSpec<Fields>[]' is not assignable to parameter of type 'SetStateAction<readonly User[]>'.
}, [filters, props.items]);

return (
<Panel>
<DataTableEager.TableProviderEager
{...props}
columns={memoizedColumns}
items={filteredItems}
getRowId={(item: User) => item.id}
plugins={[DataTablePlugins.useRowSelectColumn]}
>
<DataTableEager.DataTableEager status={{ error: null, loading: false, ready: true }} />
</DataTableEager.TableProviderEager>
</Panel>
);
};

export default {
title: 'Components/Tables/DataTableEager',
component: DataTableEager.DataTableEager,
tags: ['autodocs'],
parameters: {
layout: 'centered',
},
};

// Stories
export const Empty = {
args: {
columns,
items: generateData({ numItems: 0 }),
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const SinglePage = {
args: {
columns,
items: generateData({ numItems: 5 }),
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const MultiplePagesSmall = {
args: {
columns,
items: generateData({ numItems: 45 }),
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const MultiplePagesLarge = {
args: {
columns,
items: generateData({ numItems: 1000 }),
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const SlowNetwork = {
args: {
columns,
items: generateData({ numItems: 1000 }),
delay: 1500,
isReady: false,
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const InfiniteDelay = {
args: {
columns,
items: generateData({ numItems: 10 }),
delay: Number.POSITIVE_INFINITY,
isReady: false,
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerTemplate {...args} />,
};

export const WithFilter = {
args: {
columns,
items: generateData({ numItems: 45 }),
},
render: (args: dataTeableEagerTemplateProps) => <DataTableEagerWithFilterTemplate {...args} />,
};
17 changes: 8 additions & 9 deletions src/components/tables/DataTable/DataTableEager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import * as React from 'react';
import { classNames as cx, ClassNameArgument } from '../../../util/componentUtil.ts';
import { classNames as cx, type ClassNameArgument } from '../../../util/componentUtil.ts';
import * as ReactTable from 'react-table';

import { TableContextState, createTableContext, useTable } from './DataTableContext.tsx';
import { type TableContextState, createTableContext, useTable } from './DataTableContext.tsx';
import { Pagination } from './pagination/Pagination.tsx';
import { SearchInput } from '../../../prefab/forms/SearchInput/SearchInput.tsx';
import { MultiSearch as MultiSearchInput } from '../../../prefab/forms/MultiSearch/MultiSearch.tsx';
import { DataTableSync } from './table/DataTable';
import { SearchInput } from '../SearchInput/SearchInput.tsx';
import { MultiSearch as MultiSearchInput } from '../MultiSearch/MultiSearch.tsx';
import { DataTableSync } from './table/DataTable.tsx';

import './DataTableEager.scss';
// import './DataTableEager.scss';

export * from './DataTableContext';
export { Pagination };
Expand Down Expand Up @@ -44,15 +44,14 @@ export const TableProviderEager = <D extends object>(props: TableProviderEagerPr
isReady = true,
} = props;

const tableOptions = {
const tableOptions: ReactTable.TableOptions<D> = {
columns,
data: items,
getRowId,
...(getRowId && { getRowId }),
};
const table = ReactTable.useTable(
{
...tableOptions,

defaultColumn: {
disableGlobalFilter: true,
disableSortBy: true,
Expand Down
2 changes: 1 addition & 1 deletion src/components/tables/DataTable/DataTableLazy.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@use '../../../style/variables.scss' as bkl;
// @use '../../../style/variables.scss' as bkl;
@use '../../../style/mixins.scss' as mixins;


Expand Down
Loading

0 comments on commit 01e4a9d

Please sign in to comment.