-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
DataViews: Adds a control to the views actions to switch layouts #55311
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,211 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
getCoreRowModel, | ||
getFilteredRowModel, | ||
getSortedRowModel, | ||
getPaginationRowModel, | ||
useReactTable, | ||
} from '@tanstack/react-table'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalVStack as VStack, | ||
__experimentalHStack as HStack, | ||
VisuallyHidden, | ||
DropdownMenu, | ||
MenuGroup, | ||
MenuItem, | ||
} from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ListView from './list-view'; | ||
import { Pagination } from './pagination'; | ||
import ViewList from './view-list'; | ||
import Pagination from './pagination'; | ||
import ViewActions from './view-actions'; | ||
import TextFilter from './text-filter'; | ||
import { moreVertical } from '@wordpress/icons'; | ||
|
||
const EMPTY_OBJECT = {}; | ||
import { ViewGrid } from './view-grid'; | ||
|
||
export default function DataViews( { | ||
actions, | ||
data, | ||
fields, | ||
view, | ||
onChangeView, | ||
isLoading, | ||
fields, | ||
actions, | ||
data, | ||
isLoading = false, | ||
paginationInfo, | ||
options: { pageCount }, | ||
} ) { | ||
const columns = useMemo( () => { | ||
const _columns = [ ...fields ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically all the tanstack initialization have move to the "view-list.js" file instead because we don't need it in other views like Grid, Kanban... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we don't need them in the other views? Do you think all the callbacks to change the view like sorting, per page, etc.. would be different in some way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I'm not entirely sure what you want to reuse here? by callbacks you mean things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant the callbacks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not by principle or something I'm fine reusing tanstack wherever I can, I'm just not seeing where it can be useful elsewhere. Let's move forward and see. Maybe I'm missing something. |
||
if ( actions?.length ) { | ||
_columns.push( { | ||
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>, | ||
id: 'actions', | ||
cell: ( props ) => { | ||
return ( | ||
<DropdownMenu | ||
icon={ moreVertical } | ||
label={ __( 'Actions' ) } | ||
> | ||
{ () => ( | ||
<MenuGroup> | ||
{ actions.map( ( action ) => ( | ||
<MenuItem | ||
key={ action.id } | ||
onClick={ () => | ||
action.perform( | ||
props.row.original | ||
) | ||
} | ||
isDestructive={ | ||
action.isDesctructive | ||
} | ||
> | ||
{ action.label } | ||
</MenuItem> | ||
) ) } | ||
</MenuGroup> | ||
) } | ||
</DropdownMenu> | ||
); | ||
}, | ||
enableHiding: false, | ||
} ); | ||
} | ||
|
||
return _columns; | ||
}, [ fields, actions ] ); | ||
|
||
const columnVisibility = useMemo( () => { | ||
if ( ! view.hiddenFields?.length ) { | ||
return; | ||
} | ||
return view.hiddenFields.reduce( | ||
( accumulator, fieldId ) => ( { | ||
...accumulator, | ||
[ fieldId ]: false, | ||
} ), | ||
{} | ||
); | ||
}, [ view.hiddenFields ] ); | ||
|
||
const dataView = useReactTable( { | ||
data, | ||
columns, | ||
manualSorting: true, | ||
manualFiltering: true, | ||
manualPagination: true, | ||
enableRowSelection: true, | ||
state: { | ||
sorting: view.sort | ||
? [ | ||
{ | ||
id: view.sort.field, | ||
desc: view.sort.direction === 'desc', | ||
}, | ||
] | ||
: [], | ||
globalFilter: view.search, | ||
pagination: { | ||
pageIndex: view.page, | ||
pageSize: view.perPage, | ||
}, | ||
columnVisibility: columnVisibility ?? EMPTY_OBJECT, | ||
}, | ||
onSortingChange: ( sortingUpdater ) => { | ||
onChangeView( ( currentView ) => { | ||
const sort = | ||
typeof sortingUpdater === 'function' | ||
? sortingUpdater( | ||
currentView.sort | ||
? [ | ||
{ | ||
id: currentView.sort.field, | ||
desc: | ||
currentView.sort | ||
.direction === 'desc', | ||
}, | ||
] | ||
: [] | ||
) | ||
: sortingUpdater; | ||
if ( ! sort.length ) { | ||
return { | ||
...currentView, | ||
sort: {}, | ||
}; | ||
} | ||
const [ { id, desc } ] = sort; | ||
return { | ||
...currentView, | ||
sort: { field: id, direction: desc ? 'desc' : 'asc' }, | ||
}; | ||
} ); | ||
}, | ||
onColumnVisibilityChange: ( columnVisibilityUpdater ) => { | ||
onChangeView( ( currentView ) => { | ||
const hiddenFields = Object.entries( | ||
columnVisibilityUpdater() | ||
).reduce( | ||
( accumulator, [ fieldId, value ] ) => { | ||
if ( value ) { | ||
return accumulator.filter( | ||
( id ) => id !== fieldId | ||
); | ||
} | ||
return [ ...accumulator, fieldId ]; | ||
}, | ||
[ ...( currentView.hiddenFields || [] ) ] | ||
); | ||
return { | ||
...currentView, | ||
hiddenFields, | ||
}; | ||
} ); | ||
}, | ||
onGlobalFilterChange: ( value ) => { | ||
onChangeView( { ...view, search: value, page: 0 } ); | ||
}, | ||
onPaginationChange: ( paginationUpdater ) => { | ||
onChangeView( ( currentView ) => { | ||
const { pageIndex, pageSize } = paginationUpdater( { | ||
pageIndex: currentView.page, | ||
pageSize: currentView.perPage, | ||
} ); | ||
return { ...view, page: pageIndex, perPage: pageSize }; | ||
} ); | ||
}, | ||
getCoreRowModel: getCoreRowModel(), | ||
getFilteredRowModel: getFilteredRowModel(), | ||
getSortedRowModel: getSortedRowModel(), | ||
getPaginationRowModel: getPaginationRowModel(), | ||
pageCount, | ||
} ); | ||
const ViewComponent = view.type === 'list' ? ViewList : ViewGrid; | ||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously we need something more advanced than this to switch view types, but this should work for now. (Like a ViewType registry) |
||
<div className="dataviews-wrapper"> | ||
<VStack spacing={ 4 }> | ||
<HStack justify="space-between"> | ||
<TextFilter onChange={ dataView.setGlobalFilter } /> | ||
<TextFilter view={ view } onChangeView={ onChangeView } /> | ||
<ViewActions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this text filter independent of tanstack APIs. |
||
fields={ fields } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
/> | ||
</HStack> | ||
{ /* This component will be selected based on viewConfigs. Now we only have the list view. */ } | ||
<ListView dataView={ dataView } isLoading={ isLoading } /> | ||
<ViewComponent | ||
fields={ fields } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
paginationInfo={ paginationInfo } | ||
actions={ actions } | ||
data={ data } | ||
isLoading={ isLoading } | ||
/> | ||
<Pagination | ||
dataView={ dataView } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this text filter independent of tanstack APIs. |
||
totalItems={ paginationInfo?.totalItems } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
paginationInfo={ paginationInfo } | ||
/> | ||
</VStack> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as DataViews } from './dataviews'; | ||
export { PAGE_SIZE_VALUES } from './view-actions'; |
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.
This prop duplicated an info we already had in paginationInfo so I just removed it.