-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port over DataTable code from earlier Baklava.
- Loading branch information
mkrause
committed
Nov 26, 2024
1 parent
b2e9cde
commit 47af732
Showing
31 changed files
with
5,524 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.