Skip to content

Commit

Permalink
Port over DataTable code from earlier Baklava.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrause committed Nov 26, 2024
1 parent b2e9cde commit 47af732
Show file tree
Hide file tree
Showing 31 changed files with 5,524 additions and 1 deletion.
23 changes: 23 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"postcss-color-contrast": "^1.1.0",
"lightningcss": "^1.28.1",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc"
"@types/react-dom": "npm:types-react-dom@rc",
"@types/react-table": "^7.7.20"
},
"dependencies": {
"date-fns": "^4.1.0",
Expand All @@ -100,6 +101,7 @@
"react-error-boundary": "^4.1.2",
"@floating-ui/react": "^0.26.28",
"react-toastify": "^10.0.6",
"react-table": "^7.8.0",
"effect": "^3.10.15",
"react-hook-form": "^7.53.2",
"optics-ts": "^2.4.1"
Expand Down
3 changes: 3 additions & 0 deletions package.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ const packageConfig = {
//'@types/react-dom': '^18.2.22',
'@types/react': 'npm:types-react@rc',
'@types/react-dom': 'npm:types-react-dom@rc',

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

// Dependencies needed when running the generated build
Expand All @@ -156,6 +158,7 @@ const packageConfig = {

'@floating-ui/react': '^0.26.28',
'react-toastify': '^10.0.6',
'react-table': '^7.8.0',

'effect': '^3.10.15',
'react-hook-form': '^7.53.2',
Expand Down
7 changes: 7 additions & 0 deletions src/assets/icons/_icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ export const icons = {
'warning': {},
'workflows': {},
} as const satisfies Record<string, IconDef>;

export type IconKey = keyof typeof icons;

const iconKeys = new Set(Object.keys(icons));
export const isIconKey = (iconKey: string): iconKey is IconKey => {
return iconKeys.has(iconKey);
};
98 changes: 98 additions & 0 deletions src/components/tables/DataTable/DataTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* 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 * as React from 'react';

import type { Meta, StoryObj } from '@storybook/react';

import { DataTable } from './table/DataTable.tsx';


type DataTableArgs = React.ComponentProps<typeof DataTable>;
type Story = StoryObj<typeof DataTable>;



import * as ReactTable from 'react-table';
import { createTableContext, TableContextState } from './DataTableContext.tsx';
const DataTableContext = ({ children }: React.PropsWithChildren) => {
type User = { name: string };
const columns = React.useMemo<Array<ReactTable.Column<User>>>(() => [
{
id: 'name',
}
], []);
const data = React.useMemo<Array<User>>(() => [
{ name: 'Alice' },
], []);
const options = React.useMemo<ReactTable.TableOptions<User>>(() => ({ columns, data }), [columns, data]);
const table = ReactTable.useTable(options);

const context = React.useMemo<TableContextState<User>>(() => ({
status: { ready: true, loading: false, error: null },
setStatus() {},
reload() {},
table,
}), [table.state]);
// Note: the `table` reference is mutated, so cannot use it as dependency for `useMemo` directly

const TableContext = React.useMemo(() => createTableContext<User>(), []);

return (
<TableContext.Provider value={context}>
{children}
</TableContext.Provider>
);
};

const DataTableTemplate = (args: DataTableArgs) => {
type User = { name: string };
const columns = React.useMemo<Array<ReactTable.Column<User>>>(() => [
{
id: 'name',
getSortByToggleProps() { return {}; },
Header: 'Name:',
accessor: (user: User) => user.name,
}
], []);
const data = React.useMemo<Array<User>>(() => [
{ name: 'Alice' },
], []);
const options = React.useMemo<ReactTable.TableOptions<User>>(() => ({ columns, data }), [columns, data]);
const table = ReactTable.useTable(
options,
ReactTable.useGlobalFilter,
ReactTable.useFilters,
ReactTable.useSortBy,
ReactTable.usePagination,
ReactTable.useRowSelect,
);

return (
<div>
Table:
<DataTable
table={table}
/>
</div>
);
};


export default {
component: DataTable,
parameters: {
layout: 'centered',
},
tags: ['autodocs'],
argTypes: {},
args: {
},
render: (args) => (
<DataTableTemplate {...args}/>
),
} satisfies Meta<DataTableArgs>;


export const Standard: Story = {};
31 changes: 31 additions & 0 deletions src/components/tables/DataTable/DataTableContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

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


export type DataTableStatus = {
ready: boolean, // Whether the data is ready to be used/shown in the UI
loading: boolean, // Whether we're (re)loading the data
error: null | Error, // Whether the last loading attempt resulted in an error
};

export type TableContextState<D extends object> = {
status: DataTableStatus,
setStatus: (status: DataTableStatus) => void,
reload: () => void,
table: ReactTable.TableInstance<D>,
};

const TableContext = React.createContext<null | TableContextState<object>>(null); // Memoized
export const createTableContext = <D extends object>() => TableContext as React.Context<null | TableContextState<D>>;


export const useTable = <D extends object>(): TableContextState<D> => {
const context = React.useContext(TableContext as React.Context<null | TableContextState<D>>);

if (context === null) {
throw new TypeError('TableContext not yet initialized');
}

return context;
};
8 changes: 8 additions & 0 deletions src/components/tables/DataTable/DataTableEager.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

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


.bkl-data-table-eager--loading {
@include dataTableLazy.data-table-loading;
}
Loading

0 comments on commit 47af732

Please sign in to comment.