-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Implement BaseTableToolsTable component #125
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
Draft
bastilian
wants to merge
1
commit into
main
Choose a base branch
from
basetabletoolstable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
| import React from 'react'; | ||
| import TableToolsTable from '~/components/TableToolsTable'; | ||
|
|
||
| import useWithDefaults from './hooks/useWithDefaults'; | ||
|
|
||
| const BaseTableToolsTable = (props) => { | ||
| const propsWithDefaults = useWithDefaults(props); | ||
|
|
||
| return <TableToolsTable {...propsWithDefaults} />; | ||
| }; | ||
|
|
||
| export default BaseTableToolsTable; |
109 changes: 109 additions & 0 deletions
109
src/components/BaseTableToolsTable/BaseTableToolsTable.stories.js
This file contains hidden or 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,109 @@ | ||
| import React from 'react'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
|
|
||
| import defaultStoryMeta from '~/support/defaultStoryMeta'; | ||
| import columns from '~/support/factories/columns'; | ||
| import filters, { | ||
| customNumberFilterType, | ||
| customNumberFilter, | ||
| } from '~/support/factories/filters'; | ||
| import useExampleDataQuery from '~/support/hooks/useExampleDataQuery'; | ||
|
|
||
| import { BaseTableToolsTable, TableStateProvider } from '~/components'; | ||
| import paginationSerialiser from '~/components/StaticTableToolsTable/helpers/serialisers/pagination'; | ||
| import sortSerialiser from '~/components/StaticTableToolsTable/helpers/serialisers/sort'; | ||
| import filtersSerialiser from '~/components/StaticTableToolsTable/helpers/serialisers/filters'; | ||
|
|
||
| const queryClient = new QueryClient(); | ||
|
|
||
| const meta = { | ||
| title: 'BaseTableToolsTable', | ||
| args: { | ||
| debug: true, | ||
| columns, | ||
| filters, | ||
| }, | ||
| ...defaultStoryMeta, | ||
| }; | ||
|
|
||
| const defaultOptions = { | ||
| serialisers: { | ||
| pagination: paginationSerialiser, | ||
| sort: sortSerialiser, | ||
| filters: filtersSerialiser, | ||
| }, | ||
| }; | ||
|
|
||
| const ShareableTable = (props) => ( | ||
| <BaseTableToolsTable | ||
| props={props} | ||
| defaults={{ | ||
| options: defaultOptions, | ||
| columns, | ||
| filters: { | ||
| filterConfig: [...filters, customNumberFilter], | ||
| customFilterTypes: { | ||
| number: customNumberFilterType, | ||
| }, | ||
| }, | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| const SharableVariantTable = (props) => ( | ||
| <ShareableTable | ||
| props={props} | ||
| defaults={{ | ||
| columns: [ | ||
| { | ||
| title: 'Another Artist', | ||
| Component: ({ artist }) => artist, | ||
| }, | ||
| ], | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| const ShareableTableToolsTableExample = () => { | ||
| const { | ||
| loading, | ||
| result: { data, meta: { total } = {} } = {}, | ||
| error, | ||
| } = useExampleDataQuery({ | ||
| endpoint: '/api', | ||
| useTableState: true, | ||
| tableQueries: { | ||
| extraParams: { | ||
| itemIdsInTable: { idsOnly: true }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <SharableVariantTable | ||
| loading={loading} | ||
| items={data} | ||
| total={total} | ||
| error={error} | ||
| filters={{ filterConfig: ['title', 'number-filter'] }} | ||
| columns={['title', { key: 'artist' }, 'another-artist']} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| ShareableTableToolsTableExample.propTypes = {}; | ||
|
|
||
| export const ShareableTableToolsTable = { | ||
| decorators: [ | ||
| (Story) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <TableStateProvider> | ||
| <Story /> | ||
| </TableStateProvider> | ||
| </QueryClientProvider> | ||
| ), | ||
| ], | ||
| render: (args) => <ShareableTableToolsTableExample {...args} />, | ||
| }; | ||
|
|
||
| export default meta; |
91 changes: 91 additions & 0 deletions
91
src/components/BaseTableToolsTable/BaseTableToolsTable.test.js
This file contains hidden or 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,91 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import items from '~/support/factories/items'; | ||
| import columns from '~/support/factories/columns'; | ||
| import filters, { | ||
| customNumberFilterType, | ||
| customNumberFilter, | ||
| } from '~/support/factories/filters'; | ||
|
|
||
| import BaseTableToolsTable from './BaseTableToolsTable'; | ||
|
|
||
| const ShareableTable = (props) => ( | ||
| <BaseTableToolsTable | ||
| props={props} | ||
| defaults={{ | ||
| options: { | ||
| // debug: true, | ||
| }, | ||
| columns, | ||
| filters: { | ||
| filterConfig: [...filters, customNumberFilter], | ||
| customFilterTypes: { | ||
| number: customNumberFilterType, | ||
| }, | ||
| }, | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| const SharableVariantTable = (props) => ( | ||
| <ShareableTable | ||
| props={props} | ||
| defaults={{ | ||
| columns: [ | ||
| { | ||
| title: 'Another Artist', | ||
| Component: ({ artist }) => artist, | ||
| }, | ||
| ], | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| describe('BaseTableToolsTable', () => { | ||
| const exampleItems = items(100).sort((item) => item.name); | ||
| const itemsFunc = async () => [ | ||
| exampleItems.slice(0, 10), | ||
| exampleItems.length, | ||
| ]; | ||
|
|
||
| it('should render a table with all defaults', async () => { | ||
| render(<SharableVariantTable items={itemsFunc} />); | ||
|
|
||
| expect(await screen.findByText(exampleItems[1].title)).toBeInTheDocument(); | ||
|
|
||
| expect( | ||
| screen.getByRole('columnheader', { | ||
| name: /title/i, | ||
| }), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render a table with one default and one additional column', async () => { | ||
| const ariaLabel = 'Async Test Table'; | ||
| const props = { | ||
| 'aria-label': ariaLabel, | ||
| columns: [ | ||
| 'another-artist', | ||
| { | ||
| title: 'Title', | ||
| key: 'title', | ||
| }, | ||
| ], | ||
| items: itemsFunc, | ||
| }; | ||
|
|
||
| render(<SharableVariantTable {...props} />); | ||
|
|
||
| expect(await screen.findByText(exampleItems[1].title)).toBeInTheDocument(); | ||
|
|
||
| expect(await screen.findByText('Another Artist')).toBeInTheDocument(); | ||
|
|
||
| expect( | ||
| screen.getByRole('columnheader', { | ||
| name: /title/i, | ||
| }), | ||
| ).toBeInTheDocument(); | ||
| screen.logTestingPlaygroundURL(); | ||
|
Check warning on line 89 in src/components/BaseTableToolsTable/BaseTableToolsTable.test.js
|
||
| }); | ||
| }); | ||
This file contains hidden or 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,30 @@ | ||
| import deepmerge from 'deepmerge'; | ||
|
|
||
| const getId = (item) => { | ||
| const key = item.key || item.label || item.title || item; | ||
| return typeof key === 'string' ? key.replaceAll(' ', '-').toLowerCase() : key; | ||
| }; | ||
|
|
||
| const findDefault = (defaults, ext) => | ||
| defaults.find((def) => getId(def) === getId(ext)); | ||
|
|
||
| const selectByKeyAndMerge = (defaults, extension) => | ||
| extension | ||
| .map((ext) => { | ||
| const def = findDefault(defaults, ext); | ||
|
|
||
| if (typeof ext === 'string') { | ||
| return def; | ||
| } else if (typeof ext === 'object') { | ||
| return deepmerge(def, ext); | ||
| } | ||
| }) | ||
| .filter((v) => !!v); | ||
|
|
||
| export const compileWithDefaults = (defaults, extension) => { | ||
| if (typeof extension === 'undefined') { | ||
| return defaults; | ||
| } else if (Array.isArray(extension)) { | ||
| return selectByKeyAndMerge(defaults, extension); | ||
| } | ||
| }; |
59 changes: 59 additions & 0 deletions
59
src/components/BaseTableToolsTable/hooks/useWithDefaults.js
This file contains hidden or 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,59 @@ | ||
| import { useMemo } from 'react'; | ||
| import deepmerge from 'deepmerge'; | ||
|
|
||
| import { compileWithDefaults } from '../helpers'; | ||
|
|
||
| const useWithDefaults = (props) => { | ||
| const result = useMemo(() => { | ||
| const { | ||
| props: { props: parentProps, defaults: parentDefaults, ...thisProps }, | ||
| defaults: thisDefaults, | ||
| } = props; | ||
| const allProps = deepmerge(parentProps || {}, { | ||
| ...(thisProps || {}), | ||
| ...(props || {}), | ||
| }); | ||
| const allDefaults = deepmerge(parentDefaults || {}, thisDefaults || {}); | ||
|
|
||
| const columns = compileWithDefaults( | ||
| allDefaults.columns || [], | ||
| allProps.columns, | ||
| ); | ||
|
|
||
| const filters = { | ||
| ...allDefaults.filters, | ||
| ...allProps.filters, | ||
| filterConfig: compileWithDefaults( | ||
| allDefaults.filters?.filterConfig, | ||
| allProps.filters?.filterConfig, | ||
| ), | ||
| }; | ||
|
|
||
| const options = deepmerge( | ||
| allDefaults.options || {}, | ||
| allProps.options || {}, | ||
| ); | ||
|
|
||
| const result = { | ||
| ...allProps, | ||
| columns, | ||
| filters, | ||
| options, | ||
| }; | ||
|
|
||
| if (result.options?.debug) { | ||
| console.group('Table with defaults'); | ||
| console.log('Props:', props); | ||
| console.log('Combined props:', allProps); | ||
| console.log('Combined defaults:', allDefaults); | ||
| console.log('Result:', result); | ||
| console.groupEnd(); | ||
| } | ||
|
|
||
| return result; | ||
| }, [props]); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| export default useWithDefaults; |
This file contains hidden or 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 @@ | ||
| export { default } from './BaseTableToolsTable'; |
This file contains hidden or 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,19 @@ | ||
| import React from 'react'; | ||
| import propTypes from 'prop-types'; | ||
|
|
||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
|
|
||
| const queryClient = new QueryClient(); | ||
| window.__TANSTACK_QUERY_CLIENT__ = queryClient; | ||
|
|
||
| const QueryProviderWithUtilities = ({ children }) => { | ||
| return ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ); | ||
| }; | ||
|
|
||
| QueryProviderWithUtilities.propTypes = { | ||
| children: propTypes.node, | ||
| }; | ||
|
|
||
| export default QueryProviderWithUtilities; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected debug statement [eslint:testing-library/no-debugging-utils]