forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterList.tsx
75 lines (65 loc) · 2.1 KB
/
FilterList.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
74
75
import React from 'react'
import styled from 'styled-components'
import {get} from './constants'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'
const FilterListBase = styled.ul<SxProp>`
list-style-type: none;
margin: 0;
padding: 0;
${sx};
`
export type FilterListProps = ComponentProps<typeof FilterListBase>
const FilterList = ({children, ...rest}: React.PropsWithChildren<FilterListProps>) => {
const items = React.Children.map(children, child => {
return <li>{child}</li>
})
return <FilterListBase {...rest}>{items}</FilterListBase>
}
type StyledFilterListItemBaseProps = {
small?: boolean
selected?: boolean
} & SxProp
const FilterListItemBase = styled.a<StyledFilterListItemBaseProps>`
position: relative;
display: block;
padding: ${props => (props.small ? `${get('space.1')(props)} 10px` : `${get('space.2')(props)} 11px`)};
margin: ${props => (props.small ? '0 0 2px' : '0 0 5px 0')};
overflow: hidden;
font-size: ${get('fontSizes.1')};
color: ${props => (props.selected ? get('colors.fg.onEmphasis') : get('colors.fg.muted'))};
background-color: ${props => (props.selected ? get('colors.accent.emphasis') : '')}!important;
text-decoration: none;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
border-radius: ${get('radii.1')};
&:hover {
text-decoration: none;
background-color: ${get('colors.canvas.subtle')};
}
&:active {
color: ${get('colors.fg.onEmphasis')};
background-color: ${get('colors.accent.emphasis')};
}
.count {
float: right;
font-weight: ${get('fontWeights.bold')};
}
${sx};
`
export type FilterListItemProps = {count?: number} & ComponentProps<typeof FilterListItemBase>
function FilterListItem({children, count, ...rest}: React.PropsWithChildren<FilterListItemProps>) {
return (
<FilterListItemBase {...rest}>
{count && (
<span title="results" className="count">
{count}
</span>
)}
{children}
</FilterListItemBase>
)
}
FilterListItem.displayName = 'FilterList.Item'
export default Object.assign(FilterList, {Item: FilterListItem})