Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify sorting types a bit
  • Loading branch information
youknowriad committed May 16, 2024
commit 9ab1066416c372ea976a61784792325b35550d67
15 changes: 0 additions & 15 deletions packages/dataviews/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ export const OPERATORS = {
export const SORTING_DIRECTIONS = [ 'asc', 'desc' ] as const;
export type SORTING_DIRECTION = ( typeof SORTING_DIRECTIONS )[ number ];

// This shouldn't live in the constants file, I know.
export function sortingDirectionLabel( direction: SORTING_DIRECTION ) {
switch ( direction ) {
case 'asc':
return __( 'Sort ascending' );
case 'desc':
return __( 'Sort descending' );

// This section uses the type system to confirm it cannot be reached
default:
direction satisfies never;
throw new Error( 'unreachable' );
}
}

// View layouts.
export const LAYOUT_TABLE = 'table';
export const LAYOUT_GRID = 'grid';
Expand Down
75 changes: 41 additions & 34 deletions packages/dataviews/src/view-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import SingleSelectionCheckbox from './single-selection-checkbox';
import { unlock } from './lock-unlock';
import ItemActions from './item-actions';
import { sanitizeOperators } from './utils';
import { SORTING_DIRECTIONS, sortingDirectionLabel } from './constants';
import { SORTING_DIRECTIONS } from './constants';
import {
useSomeItemHasAPossibleBulkAction,
useHasAPossibleBulkAction,
Expand All @@ -43,6 +43,7 @@ import type {
Action,
AnyItem,
NormalizedField,
SortDirection,
ViewTable as ViewTableType,
} from './types';

Expand Down Expand Up @@ -109,6 +110,10 @@ function WithDropDownMenuSeparators( { children }: { children: ReactNode } ) {

const sortArrows = { asc: '↑', desc: '↓' };
const sortValues = { asc: 'ascending', desc: 'descending' } as const;
const sortLabels = {
asc: __( 'Sort ascending' ),
desc: __( 'Sort descending' ),
};

const _HeaderMenu = forwardRef( function HeaderMenu< Item extends AnyItem >(
{
Expand Down Expand Up @@ -159,41 +164,43 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item extends AnyItem >(
<WithDropDownMenuSeparators>
{ isSortable && (
<DropdownMenuGroup>
{ SORTING_DIRECTIONS.map( ( direction ) => {
const isChecked =
view.sort &&
isSorted &&
view.sort.direction === direction;
{ SORTING_DIRECTIONS.map(
( direction: SortDirection ) => {
const isChecked =
view.sort &&
isSorted &&
view.sort.direction === direction;

const value = `${ field.id }-${ direction }`;
const value = `${ field.id }-${ direction }`;

return (
<DropdownMenuRadioItem
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: field.id,
direction,
},
} );
} }
>
<DropdownMenuItemLabel>
{ sortingDirectionLabel( direction ) }
</DropdownMenuItemLabel>
</DropdownMenuRadioItem>
);
} ) }
return (
<DropdownMenuRadioItem
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: field.id,
direction,
},
} );
} }
>
<DropdownMenuItemLabel>
{ sortLabels[ direction ] }
</DropdownMenuItemLabel>
</DropdownMenuRadioItem>
);
}
) }
</DropdownMenuGroup>
) }
{ canAddFilter && (
Expand Down