@@ -13,6 +13,52 @@ export interface TableHeadingsProps<T, K extends Key> extends Pick<ListTableProp
1313 setSortDirection : Dispatch < SetStateAction < ListTableSortDirection > >
1414}
1515
16+ interface SortableColumnHeadingProps < T > {
17+ column : ListTableColumn < T >
18+ cellProps : ThHTMLAttributes < HTMLTableCellElement >
19+ sortColumn : ListTableColumn < T > | undefined
20+ sortDirection : ListTableSortDirection
21+ setSortColumn : Dispatch < SetStateAction < ListTableColumn < T > | undefined > >
22+ setSortDirection : Dispatch < SetStateAction < ListTableSortDirection > >
23+ }
24+
25+ const SortableColumnHeading = < T , > ( {
26+ column, cellProps, sortColumn, sortDirection, setSortColumn, setSortDirection
27+ } : SortableColumnHeadingProps < T > ) => {
28+ const isCurrent = column . id === sortColumn ?. id
29+
30+ const nextSortDirection = isCurrent
31+ ? 'asc' === sortDirection ? 'desc' : 'asc'
32+ : column . defaultSortDirection ?? 'asc'
33+ const classDirection = isCurrent ? sortDirection : 'asc' === nextSortDirection ? 'desc' : 'asc'
34+ const ariaSort = isCurrent ? 'asc' === sortDirection ? 'ascending' : 'descending' : undefined
35+
36+ return (
37+ < th
38+ { ...cellProps }
39+ aria-sort = { ariaSort }
40+ className = { classnames ( cellProps . className , isCurrent ? 'sorted' : 'sortable' , classDirection ) }
41+ >
42+ < a href = "#" onClick = { event => {
43+ event . preventDefault ( )
44+ setSortColumn ( column )
45+ setSortDirection ( nextSortDirection )
46+ } } >
47+ < span > { column . title } </ span >
48+ < span className = "sorting-indicators" >
49+ < span className = "sorting-indicator asc" aria-hidden = "true" > </ span >
50+ < span className = "sorting-indicator desc" aria-hidden = "true" > </ span >
51+ </ span >
52+ { isCurrent ? null
53+ : < span className = "screen-reader-text" >
54+ { /* translators: Hidden accessibility text. */ }
55+ { 'asc' === nextSortDirection ? __ ( 'Sort ascending.' , 'code-snippets' ) : __ ( 'Sort descending.' , 'code-snippets' ) }
56+ </ span > }
57+ </ a >
58+ </ th >
59+ )
60+ }
61+
1662export const TableHeadings = < T , K extends Key > ( {
1763 items,
1864 which,
@@ -41,6 +87,7 @@ export const TableHeadings = <T, K extends Key>({
4187 { columns . map ( column => {
4288 const cellProps : ThHTMLAttributes < HTMLTableCellElement > = {
4389 id : 'head' === which ? column . id . toString ( ) : undefined ,
90+ key : column . id ,
4491 scope : 'col' ,
4592 className : classnames (
4693 'manage-column' ,
@@ -50,42 +97,8 @@ export const TableHeadings = <T, K extends Key>({
5097 )
5198 }
5299
53- if ( ! column . sortedValue ) {
54- return < th key = { column . id } { ...cellProps } > { column . title } </ th >
55- }
56-
57- const isCurrent = column . id === sortColumn ?. id
58-
59- const nextSortDirection = isCurrent
60- ? 'asc' === sortDirection ? 'desc' : 'asc'
61- : column . defaultSortDirection ?? 'asc'
62- const classDirection = isCurrent ? sortDirection : 'asc' === nextSortDirection ? 'desc' : 'asc'
63- const ariaSort = isCurrent ? 'asc' === sortDirection ? 'ascending' : 'descending' : undefined
64-
65- return (
66- < th
67- key = { column . id }
68- { ...cellProps }
69- aria-sort = { ariaSort }
70- className = { classnames ( cellProps . className , isCurrent ? 'sorted' : 'sortable' , classDirection ) }
71- >
72- < a href = "#" onClick = { event => {
73- event . preventDefault ( )
74- setSortColumn ( column )
75- setSortDirection ( nextSortDirection )
76- } } >
77- < span > { column . title } </ span >
78- < span className = "sorting-indicators" >
79- < span className = "sorting-indicator asc" aria-hidden = "true" > </ span >
80- < span className = "sorting-indicator desc" aria-hidden = "true" > </ span >
81- </ span >
82- { isCurrent ? null
83- : < span className = "screen-reader-text" >
84- { /* translators: Hidden accessibility text. */ }
85- { 'asc' === nextSortDirection ? __ ( 'Sort ascending.' , 'code-snippets' ) : __ ( 'Sort descending.' , 'code-snippets' ) }
86- </ span > }
87- </ a >
88- </ th >
89- )
100+ return column . sortedValue
101+ ? < SortableColumnHeading { ...{ column, cellProps, sortColumn, sortDirection, setSortColumn, setSortDirection } } />
102+ : < th { ...cellProps } > { column . title } </ th >
90103 } ) }
91104 </ tr >
0 commit comments