forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicker.tsx
76 lines (71 loc) · 1.85 KB
/
Picker.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
76
import { ReactNode, useState } from 'react'
import { ActionMenu } from '@primer/react'
import { AnchorAlignment } from '@primer/behaviors'
import { Fields } from './Fields'
interface Props {
items: PickerItem[]
onSelect?: (item: PickerItem) => void
buttonBorder?: boolean
pickerLabel?: string
dataTestId: string
defaultText: string
ariaLabel: string
alignment: AnchorAlignment
descriptionFontSize?: number
renderItem?: (item: PickerItem) => ReactNode | string
}
export interface PickerItem {
href: string
text: string
selected: boolean
extra?: {
[key: string]: any
}
divider?: boolean
}
export const Picker = ({
items,
ariaLabel,
pickerLabel,
buttonBorder,
dataTestId,
defaultText,
onSelect,
alignment,
descriptionFontSize,
renderItem,
}: Props) => {
const [open, setOpen] = useState(false)
const selectedOption = items.find((item) => item.selected === true)
return (
<ActionMenu open={open} onOpenChange={setOpen}>
<ActionMenu.Button
aria-label={ariaLabel}
variant={buttonBorder ? 'default' : 'invisible'}
className="color-fg-default width-full p-1 pl-2 pr-2"
sx={{
height: 'auto',
textAlign: 'left',
'span:first-child': { display: 'inline' },
}}
>
{pickerLabel && <span style={{ whiteSpace: 'pre-wrap' }}>{`${pickerLabel}`}</span>}
<span
className={`f${descriptionFontSize} color-fg-muted text-normal`}
data-testid={dataTestId}
>
{selectedOption?.text || defaultText}
</span>
</ActionMenu.Button>
<ActionMenu.Overlay width="auto" align={alignment}>
<Fields
open={open}
setOpen={setOpen}
items={items}
onSelect={onSelect}
renderItem={renderItem}
/>
</ActionMenu.Overlay>
</ActionMenu>
)
}