-
Notifications
You must be signed in to change notification settings - Fork 68.3k
Expand file tree
/
Copy pathPicker.tsx
More file actions
75 lines (70 loc) · 1.84 KB
/
Copy pathPicker.tsx
File metadata and controls
75 lines (70 loc) · 1.84 KB
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 { ReactNode, useState } from 'react'
import { ActionMenu } from '@primer/react'
import { AnchorAlignment } from '@primer/behaviors'
import { Fields } from './Fields'
import styles from './Picker.module.scss'
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?: {
arrow?: boolean
info?: boolean
version?: string
currentDate?: string
}
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 ${styles.menuButton}`}
>
{pickerLabel && <span className={styles.pickerLabel}>{`${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>
)
}