1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/components/ui/Picker/Picker.tsx
Grace Park 822fe2926b Megabranch: Upgrade primer/react (#28458)
* upgrade primer/react

* upgrade

* using deprecated

* remove lib"

* Upgrade primer/react: Upgrade Label (#28502)

update Label to primer/react 35.2.2

* fix merge conflicts

* primer/react v35: update ActionList (#28467)

* Update to v35 ActionList for LearningTrack

* Update to v35 ActionList for ArticleList

* Update to v35 ActionList for ProductArticleList

* Update to v35 ActionList for TableOfContents

* Update to v35 ActionList for ProductCollapsibleSection

* Update to v35 ActionList for RestCollapsibleSection

* Update to v35 ActionList for SidebarHomepage

* Update to v35 ActionList for MiniTocs

* Update to v35 ActionList for Search

* Extra div for rendering test

* One less div for rendering test

* All the style updates for v35 ActionList

* Works without setting as an li which is already the default (didn't before for some reason)

* Use deprecated ItemInput for now

* Picker update for primer/react (#28485)

* update picker

* inline picker for mobile

* set width to auto

* Update components/ui/Picker/Picker.tsx

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>

* update

* Update Picker.tsx

* update onselect

* checking language code

* move language cookie setting to language picker

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>

* Resolve package merge conflicts

* fresh npm install

* Primer update UnderlineNav (#28582)

* update underlinenav for primer/react update

* update tests

* update switches test

* update one last label

* update header test"

* remove href in underlinenav

* update rendering tests

* update cursor

* primer/react v35: update DropDownMenu to ActionMenu (#28576)

* Update to v35 ActionMenu for ArticleCards

* Update to v35 ActionMenu for Search

* Set button to inline-block

* Put the props on the overlay

* Update test for ActionMenu markup

* update package

* update package lock

* primer/react v35: CodeLanguagePicker update from SelectMenu to ActionMenu (#28625)

* Use octicon for menu down arrow

* Update to v35 ActionMenu for CodeLanguagePicker

* update to SubNav

Co-authored-by: Grace Park <gracepark@github.com>

* update package-lock

Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com>
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
2022-07-11 11:51:18 -07:00

96 lines
2.9 KiB
TypeScript

import React, { useState } from 'react'
import { ActionList, ActionMenu, Box, Details, Text, useDetails } from '@primer/react'
import { ArrowRightIcon, ChevronDownIcon, InfoIcon, LinkExternalIcon } from '@primer/octicons-react'
import cx from 'classnames'
import { Link } from 'components/Link'
export type PickerOptionsTypeT = {
text: string
href: string
locale?: string
external?: boolean
arrow?: boolean
info?: boolean
selected?: boolean
onselect?: Function | void
}
export type PickerPropsT = {
variant?: 'inline'
defaultText: string
options: Array<PickerOptionsTypeT>
}
export function Picker({ variant, defaultText, options }: PickerPropsT) {
const [open, setOpen] = useState(false)
const { getDetailsProps } = useDetails({ closeOnOutsideClick: true })
const selectedOption = options.find((opt) => opt.selected === true)
function getFields() {
return (
<ActionList selectionVariant="single">
{options.map((option) => (
<ActionList.LinkItem
as={Link}
className={option.arrow || option.info ? 'f6' : ''}
locale={option.locale}
key={option.text}
href={option.href}
onClick={() => {
if (option.onselect) option.onselect(option.locale)
setOpen(!open)
}}
>
{option.text}
{option.external && <LinkExternalIcon size="small" className="ml-1" />}
{option.info && <InfoIcon verticalAlign="middle" size={15} className="ml-1" />}
{option.arrow && <ArrowRightIcon verticalAlign="middle" size={15} className="ml-1" />}
</ActionList.LinkItem>
))}
</ActionList>
)
}
function getInlinePicker() {
return (
<Details {...getDetailsProps()} className={cx('position-relative details-reset', 'd-block')}>
<summary
className="d-block btn btn-invisible color-fg-default"
aria-haspopup="true"
aria-label={selectedOption?.text || defaultText}
>
<div className="d-flex flex-items-center flex-justify-between">
<Text>{selectedOption?.text || defaultText}</Text>
<ChevronDownIcon size={24} className="arrow ml-md-1" />
</div>
</summary>
<Box>
<ul>{getFields()}</ul>
</Box>
</Details>
)
}
return (
<React.Fragment>
{variant === 'inline' ? (
getInlinePicker()
) : (
<ActionMenu open={open} onOpenChange={setOpen}>
<ActionMenu.Button
aria-label="Select field type"
variant="invisible"
sx={{ color: `var(--color-fg-default)` }}
>
{selectedOption?.text || defaultText}
</ActionMenu.Button>
<ActionMenu.Overlay width="auto" align="end">
{getFields()}
</ActionMenu.Overlay>
</ActionMenu>
)}
</React.Fragment>
)
}