-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtable-header.tsx
73 lines (66 loc) · 1.78 KB
/
table-header.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { ComponentProps, FC } from 'react'
import type { ColumnType, TableHeaderSortHandler, TableSort } from './types'
import EbayTableCell from './table-cell'
import { EbayIcon, Icon } from '../ebay-icon'
export type EbayTableHeaderProps = ComponentProps<'th'> & {
columnType?: ColumnType;
name?: string;
sort?: TableSort;
href?: string;
// rowHeader is only used to be passed in the EbayTableRow cells
// but for headers, this is a <th> so "rowHeader" is always true
rowHeader?: boolean;
onSort?: TableHeaderSortHandler
}
export const EbayTableHeader: FC<EbayTableHeaderProps> = ({
columnType,
name,
sort,
href,
children,
onSort,
...rest
}: EbayTableHeaderProps) => {
const ariaSortMap: Record<TableSort, ComponentProps<'th'>['aria-sort']> = {
asc: 'ascending',
desc: 'descending',
none: 'none'
}
const sortIconMap: Record<TableSort, Icon> = {
asc: 'sortDown12',
desc: 'sortUp12',
none: 'sort12'
}
const sortContent = sort ? (
<>
{' '}
<EbayIcon name={sortIconMap[sort]} />
</>
) : null
let content = children
if (href) {
content = (
<a href={href} onClick={onSort}>
{children}
{sortContent}
</a>
)
} else if (sort) {
content = (
<button type="button" onClick={onSort}>
{children}
{sortContent}
</button>
)
}
return (
<EbayTableCell
{...rest}
rowHeader
columnType={columnType}
aria-sort={sort ? ariaSortMap[sort] : undefined}>
{content}
</EbayTableCell>
)
}
export default EbayTableHeader