import React from 'react' import classnames from 'classnames' import { __ } from '@wordpress/i18n' import type { ListTableColumn, ListTableSortDirection } from './ListTable' import type { Key, ThHTMLAttributes } from 'react' interface SortableHeadingCellProps { column: ListTableColumn cellProps: ThHTMLAttributes sortColumn: ListTableColumn | undefined sortDirection: ListTableSortDirection setSortColumn: (column: ListTableColumn | undefined) => void setSortDirection: (direction: ListTableSortDirection) => void } const SortableHeadingCell = ({ column, cellProps, sortColumn, sortDirection, setSortColumn, setSortDirection }: SortableHeadingCellProps) => { const isCurrent = column.id === sortColumn?.id const nextSortDirection = isCurrent ? 'asc' === sortDirection ? 'desc' : 'asc' : column.defaultSortDirection ?? 'asc' const classDirection = isCurrent ? sortDirection : 'asc' === nextSortDirection ? 'desc' : 'asc' const ariaSort = isCurrent ? 'asc' === sortDirection ? 'ascending' : 'descending' : undefined return ( ) } export interface ColumnHeadingsProps { items: T[] which: 'head' | 'foot' getKey: (item: T) => K columns: ListTableColumn[] selected: Set sortColumn: ListTableColumn | undefined setSelected: (selected: Set) => void sortDirection: ListTableSortDirection setSortColumn: (column: ListTableColumn | undefined) => void setSortDirection: (direction: ListTableSortDirection) => void } export const ColumnHeadings = ({ items, which, getKey, columns, sortColumn, selected, setSelected, setSortColumn, sortDirection, setSortDirection }: ColumnHeadingsProps) => selected.has(getKey(item)))} aria-label={__('Select All', 'code-snippets')} onChange={event => { setSelected(new Set(event.target.checked ? items.map(getKey) : [])) }} /> {columns.map(column => { const cellProps: ThHTMLAttributes = { id: 'head' === which ? column.id.toString() : undefined, scope: 'col', className: classnames( 'manage-column', `column-${column.id}`, `${column.id}-column`, { 'hidden': column.isHidden, 'column-primary': column.isPrimary } ) } return column.sortedValue ? : {column.title} })}