Skip to content
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

Menu granular subcomponents: Refactor dataviews table layout header menu #67640

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 129 additions & 122 deletions packages/dataviews/src/dataviews-layouts/table/column-header-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,152 +101,159 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item >(
}

return (
<Menu
align="start"
trigger={
<Button
size="compact"
className="dataviews-view-table-header-button"
ref={ ref }
variant="tertiary"
>
{ header }
{ view.sort && isSorted && (
<span aria-hidden="true">
{ sortArrows[ view.sort.direction ] }
</span>
) }
</Button>
}
style={ { minWidth: '240px' } }
>
<WithMenuSeparators>
{ isSortable && (
<Menu.Group>
{ SORTING_DIRECTIONS.map(
( direction: SortDirection ) => {
const isChecked =
view.sort &&
isSorted &&
view.sort.direction === direction;
<Menu>
<Menu.TriggerButton
render={
<Button
size="compact"
className="dataviews-view-table-header-button"
ref={ ref }
variant="tertiary"
/>
}
>
{ header }
{ view.sort && isSorted && (
<span aria-hidden="true">
{ sortArrows[ view.sort.direction ] }
</span>
) }
</Menu.TriggerButton>
<Menu.Popover style={ { minWidth: '240px' } }>
<WithMenuSeparators>
{ isSortable && (
<Menu.Group>
{ SORTING_DIRECTIONS.map(
( direction: SortDirection ) => {
const isChecked =
view.sort &&
isSorted &&
view.sort.direction === direction;

const value = `${ fieldId }-${ direction }`;
const value = `${ fieldId }-${ direction }`;

return (
<Menu.RadioItem
key={ value }
// All sorting radio items share the same name, so that
// selecting a sorting option automatically deselects the
// previously selected one, even if it is displayed in
// another submenu. The field and direction are passed via
// the `value` prop.
name="view-table-sorting"
value={ value }
checked={ isChecked }
onChange={ () => {
onChangeView( {
...view,
sort: {
field: fieldId,
direction,
},
} );
} }
>
<Menu.ItemLabel>
{ sortLabels[ direction ] }
</Menu.ItemLabel>
</Menu.RadioItem>
);
}
) }
</Menu.Group>
) }
{ canAddFilter && (
return (
<Menu.RadioItem
key={ value }
// All sorting radio items share the same name, so that
// selecting a sorting option automatically deselects the
// previously selected one, even if it is displayed in
// another submenu. The field and direction are passed via
// the `value` prop.
name="view-table-sorting"
value={ value }
checked={ isChecked }
onChange={ () => {
onChangeView( {
...view,
sort: {
field: fieldId,
direction,
},
} );
} }
>
<Menu.ItemLabel>
{ sortLabels[ direction ] }
</Menu.ItemLabel>
</Menu.RadioItem>
);
}
) }
</Menu.Group>
) }
{ canAddFilter && (
<Menu.Group>
<Menu.Item
prefix={ <Icon icon={ funnel } /> }
onClick={ () => {
setOpenedFilter( fieldId );
onChangeView( {
...view,
page: 1,
filters: [
...( view.filters || [] ),
{
field: fieldId,
value: undefined,
operator: operators[ 0 ],
},
],
} );
} }
>
<Menu.ItemLabel>
{ __( 'Add filter' ) }
</Menu.ItemLabel>
</Menu.Item>
</Menu.Group>
) }
<Menu.Group>
<Menu.Item
prefix={ <Icon icon={ funnel } /> }
prefix={ <Icon icon={ arrowLeft } /> }
disabled={ index < 1 }
onClick={ () => {
setOpenedFilter( fieldId );
onChangeView( {
...view,
page: 1,
filters: [
...( view.filters || [] ),
{
field: fieldId,
value: undefined,
operator: operators[ 0 ],
},
fields: [
...( visibleFieldIds.slice(
0,
index - 1
) ?? [] ),
fieldId,
visibleFieldIds[ index - 1 ],
...visibleFieldIds.slice( index + 1 ),
],
} );
} }
>
<Menu.ItemLabel>
{ __( 'Add filter' ) }
{ __( 'Move left' ) }
</Menu.ItemLabel>
</Menu.Item>
</Menu.Group>
) }
<Menu.Group>
<Menu.Item
prefix={ <Icon icon={ arrowLeft } /> }
disabled={ index < 1 }
onClick={ () => {
onChangeView( {
...view,
fields: [
...( visibleFieldIds.slice(
0,
index - 1
) ?? [] ),
fieldId,
visibleFieldIds[ index - 1 ],
...visibleFieldIds.slice( index + 1 ),
],
} );
} }
>
<Menu.ItemLabel>{ __( 'Move left' ) }</Menu.ItemLabel>
</Menu.Item>
<Menu.Item
prefix={ <Icon icon={ arrowRight } /> }
disabled={ index >= visibleFieldIds.length - 1 }
onClick={ () => {
onChangeView( {
...view,
fields: [
...( visibleFieldIds.slice( 0, index ) ??
[] ),
visibleFieldIds[ index + 1 ],
fieldId,
...visibleFieldIds.slice( index + 2 ),
],
} );
} }
>
<Menu.ItemLabel>{ __( 'Move right' ) }</Menu.ItemLabel>
</Menu.Item>
{ isHidable && field && (
<Menu.Item
prefix={ <Icon icon={ unseen } /> }
prefix={ <Icon icon={ arrowRight } /> }
disabled={ index >= visibleFieldIds.length - 1 }
onClick={ () => {
onHide( field );
onChangeView( {
...view,
fields: visibleFieldIds.filter(
( id ) => id !== fieldId
),
fields: [
...( visibleFieldIds.slice(
0,
index
) ?? [] ),
visibleFieldIds[ index + 1 ],
fieldId,
...visibleFieldIds.slice( index + 2 ),
],
} );
} }
>
<Menu.ItemLabel>
{ __( 'Hide column' ) }
{ __( 'Move right' ) }
</Menu.ItemLabel>
</Menu.Item>
) }
</Menu.Group>
</WithMenuSeparators>
{ isHidable && field && (
<Menu.Item
prefix={ <Icon icon={ unseen } /> }
onClick={ () => {
onHide( field );
onChangeView( {
...view,
fields: visibleFieldIds.filter(
( id ) => id !== fieldId
),
} );
} }
>
<Menu.ItemLabel>
{ __( 'Hide column' ) }
</Menu.ItemLabel>
</Menu.Item>
) }
</Menu.Group>
</WithMenuSeparators>
</Menu.Popover>
</Menu>
);
} );
Expand Down
Loading