-
Notifications
You must be signed in to change notification settings - Fork 8
feat(DataViewTable): introduce data view table #130
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
LightOfHeaven1994
wants to merge
5
commits into
main
Choose a base branch
from
feat/data-view-table
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
452d954
feat(DataViewTable): introduce data view table
LightOfHeaven1994 bf1a84c
feat(DataViewTable): add sorting
LightOfHeaven1994 1892b6a
feat(DataViewTable): add actions
LightOfHeaven1994 dabe192
feat(DataViewTable): add export
LightOfHeaven1994 221f612
feat(DataViewTable): add table variant
LightOfHeaven1994 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import React from 'react'; | ||
| import propTypes from 'prop-types'; | ||
| import { | ||
| DataView, | ||
| DataViewTable as PatternFlyDataViewTable, | ||
| DataViewToolbar, | ||
| } from '@patternfly/react-data-view'; | ||
| import { Pagination, PaginationVariant } from '@patternfly/react-core'; | ||
|
|
||
| import useTableToolsForDataView from './hooks/useTableToolsForDataView'; | ||
|
|
||
| /** | ||
| * Data View presentation variant. | ||
| * | ||
| * @param {object} props Component props (useTableTools output) | ||
| * @param {boolean} [props.loading] Loading state | ||
| * @param {object} [props.error] Error state | ||
| * @param {object} [props.tableProps] PatternFly table props from useTableTools | ||
| * @param {object} [props.toolbarProps] Toolbar props from useTableTools | ||
| * | ||
| * @group Components | ||
|
qltysh[bot] marked this conversation as resolved.
|
||
| * @returns {React.ReactElement} DataView table | ||
| */ | ||
| const DataViewTable = (props) => { | ||
| const { | ||
| columns: dataViewColumns, | ||
| rows: dataViewRows, | ||
| activeState, | ||
| headStates, | ||
| bodyStates, | ||
| toolbarProps: { pagination, actions } = {}, | ||
| } = useTableToolsForDataView(props); | ||
|
|
||
| return ( | ||
| <DataView activeState={activeState}> | ||
| <DataViewToolbar | ||
| pagination={pagination && <Pagination isCompact {...pagination} />} | ||
| actions={actions} | ||
| ouiaId="data-view-table-toolbar" | ||
| /> | ||
| <PatternFlyDataViewTable | ||
| aria-label="DataViewTable" | ||
| columns={dataViewColumns} | ||
| rows={dataViewRows} | ||
| headStates={headStates} | ||
| bodyStates={bodyStates} | ||
| /> | ||
| <DataViewToolbar | ||
| pagination={ | ||
| pagination && ( | ||
| <Pagination variant={PaginationVariant.bottom} {...pagination} /> | ||
| ) | ||
| } | ||
| /> | ||
| </DataView> | ||
| ); | ||
| }; | ||
|
|
||
| DataViewTable.propTypes = { | ||
| loading: propTypes.bool, | ||
| error: propTypes.object, | ||
| tableProps: propTypes.object, | ||
| toolbarProps: propTypes.object, | ||
| }; | ||
|
|
||
| export default DataViewTable; | ||
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,133 @@ | ||
| import React from 'react'; | ||
| import defaultStoryMeta from '~/support/defaultStoryMeta'; | ||
| import columns from '~/support/factories/columns'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { TableStateProvider, TableToolsTable } from '~/components'; | ||
| import useExampleDataQuery from '~/support/hooks/useExampleDataQuery'; | ||
| import paginationSerialiser from '~/components/StaticTableToolsTable/helpers/serialisers/pagination'; | ||
| import sortSerialiser from '~/components/StaticTableToolsTable/helpers/serialisers/sort'; | ||
| import { actions } from '~/support/constants'; | ||
|
|
||
| const meta = { | ||
| title: 'DataViewTable', | ||
| component: TableToolsTable, | ||
| ...defaultStoryMeta, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| const queryClient = new QueryClient(); | ||
|
|
||
| const defaultOptions = { | ||
| serialisers: { | ||
| pagination: paginationSerialiser, | ||
| sort: sortSerialiser, | ||
| }, | ||
| }; | ||
|
|
||
| const CommonExample = () => { | ||
| const { | ||
| loading, | ||
| result: { data, meta: { total } = {} } = {}, | ||
| error, | ||
| exporter, | ||
| } = useExampleDataQuery({ | ||
| endpoint: '/api', | ||
| useTableState: true, | ||
| }); | ||
|
|
||
| return ( | ||
| <TableToolsTable | ||
| tableToolsTableVariant="dataViewTable" | ||
| items={data} | ||
| columns={columns} | ||
| total={total} | ||
| error={error} | ||
| loading={loading} | ||
| options={{ | ||
| ...defaultOptions, | ||
| actions, | ||
| exporter, | ||
| }} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const Common = { | ||
| decorators: [ | ||
| (Story) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <TableStateProvider> | ||
| <Story /> | ||
| </TableStateProvider> | ||
| </QueryClientProvider> | ||
| ), | ||
| ], | ||
| render: (args) => <CommonExample {...args} />, | ||
| }; | ||
|
|
||
| const WithErrorPassedExample = () => { | ||
| const { | ||
| loading, | ||
| result: { data, meta: { total } = {} } = {}, | ||
| error, | ||
| } = useExampleDataQuery({ endpoint: '/api/error' }); | ||
| return ( | ||
| <TableToolsTable | ||
| tableToolsTableVariant="dataViewTable" | ||
| items={data} | ||
| columns={columns} | ||
| total={total} | ||
| error={error} | ||
| loading={loading} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const WithErrorPassed = { | ||
| decorators: [ | ||
| (Story) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <TableStateProvider> | ||
| <Story /> | ||
| </TableStateProvider> | ||
| </QueryClientProvider> | ||
| ), | ||
| ], | ||
| render: (args) => <WithErrorPassedExample {...args} />, | ||
| }; | ||
|
|
||
| const EmptyExample = () => { | ||
| const { | ||
| loading, | ||
| result: { data, meta: { total } = {} } = {}, | ||
| error, | ||
| } = useExampleDataQuery({ | ||
| endpoint: '/api', | ||
| params: { total: 0 }, | ||
| }); | ||
|
|
||
| return ( | ||
| <TableToolsTable | ||
| tableToolsTableVariant="dataViewTable" | ||
| items={data} | ||
| columns={columns} | ||
| total={total} | ||
| error={error} | ||
| loading={loading} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const Empty = { | ||
| decorators: [ | ||
| (Story) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <TableStateProvider> | ||
| <Story /> | ||
| </TableStateProvider> | ||
| </QueryClientProvider> | ||
| ), | ||
| ], | ||
| render: (args) => <EmptyExample {...args} />, | ||
| }; |
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,36 @@ | ||
| import React from 'react'; | ||
| import { ErrorState } from '@patternfly/react-component-groups'; | ||
| import { EmptyState, EmptyStateBody } from '@patternfly/react-core'; | ||
| import { CubesIcon } from '@patternfly/react-icons'; | ||
| import { Tbody, Td, Tr } from '@patternfly/react-table'; | ||
|
|
||
| export const getErrorBodyState = (columnsCount) => ( | ||
| <Tbody> | ||
| <Tr key="error"> | ||
| <Td colSpan={columnsCount}> | ||
| <ErrorState | ||
| titleText="Unable to load data" | ||
| bodyText="There was an error retrieving data. Check your connection and reload the page." | ||
| /> | ||
| </Td> | ||
| </Tr> | ||
| </Tbody> | ||
| ); | ||
|
|
||
| export const getDefaultEmptyBodyState = (columns) => ( | ||
| <Tbody> | ||
| <Tr> | ||
| <Td colSpan={columns.length}> | ||
| <EmptyState | ||
| headingLevel="h4" | ||
| icon={CubesIcon} | ||
| titleText="No data found" | ||
| > | ||
| <EmptyStateBody> | ||
| There are no matching data to be displayed. | ||
| </EmptyStateBody> | ||
| </EmptyState> | ||
| </Td> | ||
| </Tr> | ||
| </Tbody> | ||
| ); |
46 changes: 46 additions & 0 deletions
46
src/components/DataViewTable/helpers/getDataViewStateProps.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,46 @@ | ||
| import React from 'react'; | ||
| import { DataViewState } from '@patternfly/react-data-view'; | ||
| import { | ||
| SkeletonTableBody, | ||
| SkeletonTableHead, | ||
| } from '@patternfly/react-component-groups'; | ||
|
|
||
| import { getDefaultEmptyBodyState, getErrorBodyState } from './bodyStates'; | ||
|
|
||
| const DEFAULT_SKELETON_ROWS = 10; | ||
|
|
||
| export const getDataViewStateProps = ({ | ||
| loading, | ||
| error, | ||
| rows, | ||
| columns, | ||
| emptyState, | ||
| perPage, | ||
| }) => { | ||
| let activeState; | ||
|
|
||
| if (loading) { | ||
| activeState = DataViewState.loading; | ||
| } else if (error) { | ||
| activeState = DataViewState.error; | ||
| } else if (!rows?.length) { | ||
| activeState = DataViewState.empty; | ||
| } | ||
|
|
||
| return { | ||
| activeState, | ||
| headStates: { | ||
| loading: <SkeletonTableHead columns={columns} />, | ||
| }, | ||
| bodyStates: { | ||
| loading: ( | ||
| <SkeletonTableBody | ||
| rowsCount={perPage || DEFAULT_SKELETON_ROWS} | ||
| columnsCount={columns.length} | ||
| /> | ||
| ), | ||
| error: getErrorBodyState(columns.length), | ||
| empty: emptyState || getDefaultEmptyBodyState(columns), | ||
| }, | ||
| }; | ||
| }; |
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,4 @@ | ||
| export { toDataViewProps } from './toDataViewProps'; | ||
| export { getDataViewStateProps } from './getDataViewStateProps'; | ||
| export { toDataViewActions } from './toDataViewActions'; | ||
| export { toDataViewExport } from './toDataViewExport'; |
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,17 @@ | ||
| import React from 'react'; | ||
| import { Button } from '@patternfly/react-core'; | ||
|
|
||
| export const toDataViewActions = (actions) => | ||
| (actions || []).filter(Boolean).map((action, i) => { | ||
| if (React.isValidElement(action)) { | ||
| return <React.Fragment key={i}>{action}</React.Fragment>; | ||
| } | ||
| const { label, onClick } = action; | ||
| return typeof label === 'string' ? ( | ||
| <Button key={i} variant="secondary" onClick={onClick}> | ||
| {label} | ||
| </Button> | ||
| ) : ( | ||
| <React.Fragment key={i}>{label}</React.Fragment> | ||
| ); | ||
| }); |
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,16 @@ | ||
| import React from 'react'; | ||
| import DownloadButton from '@redhat-cloud-services/frontend-components/DownloadButton'; | ||
|
|
||
| /** | ||
| * Adapts PrimaryToolbar `exportConfig` into a DataViewToolbar actions slot node. | ||
| * | ||
| * @param {object} [exportConfig] exportConfig from useExport / toolbarProps | ||
| * @returns {React.ReactElement|null} DownloadButton or null when export is off | ||
| */ | ||
| export const toDataViewExport = (exportConfig) => { | ||
| if (!exportConfig?.onSelect && !exportConfig?.extraItems) { | ||
| return null; | ||
| } | ||
|
|
||
| return <DownloadButton key="export" {...exportConfig} />; | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.