-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
966d190
commit 116da55
Showing
6 changed files
with
266 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
polaris-react/playground/components/Options/Options.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React, {useState, useCallback, useMemo} from 'react'; | ||
|
||
import {Tag, Listbox, Combobox, InlineStack} from '../../../src'; | ||
|
||
export function Options() { | ||
const deselectedOptions = useMemo( | ||
() => [ | ||
{value: 'rustic', label: 'Rustic'}, | ||
{value: 'antique', label: 'Antique'}, | ||
{value: 'vinyl', label: 'Vinyl'}, | ||
{value: 'vintage', label: 'Vintage'}, | ||
{value: 'refurbished', label: 'Refurbished'}, | ||
], | ||
[], | ||
); | ||
|
||
const [selectedOptions, setSelectedOptions] = useState<string[]>([]); | ||
const [inputValue, setInputValue] = useState(''); | ||
const [options, setOptions] = useState(deselectedOptions); | ||
|
||
const updateText = useCallback( | ||
(value: string) => { | ||
setInputValue(value); | ||
|
||
if (value === '') { | ||
setOptions(deselectedOptions); | ||
return; | ||
} | ||
|
||
const filterRegex = new RegExp(value, 'i'); | ||
const resultOptions = deselectedOptions.filter((option) => | ||
option.label.match(filterRegex), | ||
); | ||
setOptions(resultOptions); | ||
}, | ||
[deselectedOptions], | ||
); | ||
|
||
const updateSelection = useCallback( | ||
(selected: string) => { | ||
if (selectedOptions.includes(selected)) { | ||
setSelectedOptions( | ||
selectedOptions.filter((option) => option !== selected), | ||
); | ||
} else { | ||
setSelectedOptions([...selectedOptions, selected]); | ||
} | ||
|
||
updateText(''); | ||
}, | ||
[selectedOptions, updateText], | ||
); | ||
|
||
const removeTag = useCallback( | ||
(tag: string) => () => { | ||
const options = [...selectedOptions]; | ||
options.splice(options.indexOf(tag), 1); | ||
setSelectedOptions(options); | ||
}, | ||
[selectedOptions], | ||
); | ||
|
||
const tagsMarkup = selectedOptions.map((option) => ( | ||
<Tag key={`option-${option}`} onRemove={removeTag(option)}> | ||
{option} | ||
</Tag> | ||
)); | ||
|
||
const optionsMarkup = | ||
options.length > 0 | ||
? options.map((option) => { | ||
const {label, value} = option; | ||
|
||
return ( | ||
<Listbox.Option | ||
key={`${value}`} | ||
value={value} | ||
selected={selectedOptions.includes(value)} | ||
accessibilityLabel={label} | ||
> | ||
{label} | ||
</Listbox.Option> | ||
); | ||
}) | ||
: null; | ||
|
||
return ( | ||
<div style={{height: '225px'}}> | ||
<Combobox | ||
allowMultiple | ||
persistent | ||
activator={ | ||
<Combobox.TextField | ||
onChange={updateText} | ||
label="Search tags" | ||
labelHidden | ||
value={inputValue} | ||
placeholder="Search tags" | ||
autoComplete="off" | ||
/> | ||
} | ||
> | ||
{optionsMarkup ? ( | ||
<Listbox onSelect={updateSelection}>{optionsMarkup}</Listbox> | ||
) : null} | ||
</Combobox> | ||
|
||
<InlineStack gap="100">{tagsMarkup}</InlineStack> | ||
</div> | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
polaris-react/playground/components/Select/Select.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.Select { | ||
padding: calc(var(--p-space-200) + var(--p-space-050)) var(--p-space-200) | ||
var(--p-space-200) var(--p-space-300); | ||
border-radius: var(--p-border-radius-200); | ||
border: var(--p-border-width-0165) solid var(--p-color-input-border); | ||
background-color: var(--Color-input-bg-surface); | ||
width: 100%; | ||
|
||
&:focus:not(:active) { | ||
outline: var(--p-border-width-050) solid var(--p-color-border-focus); | ||
outline-offset: var(--p-border-width-025); | ||
} | ||
|
||
&:hover { | ||
border-color: var(--p-color-input-border-hover); | ||
background-color: var(--Color-input-bg-surface-hover); | ||
|
||
svg { | ||
fill: var(--p-color-icon-hover); | ||
} | ||
} | ||
|
||
&:active { | ||
border-color: var(--p-color-input-border-active); | ||
background-color: var(--Color-input-bg-surface-active); | ||
|
||
svg { | ||
fill: var(--p-color-icon-active); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react'; | ||
import {SelectMinor} from '@shopify/polaris-icons'; | ||
|
||
import type {OptionListProps} from '../../../src'; | ||
import { | ||
BlockStack, | ||
Icon, | ||
Text, | ||
InlineGrid, | ||
UnstyledButton, | ||
Popover, | ||
} from '../../../src'; | ||
import {Options} from '../Options/Options'; | ||
|
||
import styles from './Select.module.scss'; | ||
|
||
interface Props { | ||
title: string; | ||
defaultSelected?: string[]; | ||
} | ||
|
||
export function Select({ | ||
title, | ||
defaultSelected, | ||
options, | ||
}: Props & Pick<OptionListProps, 'options'>) { | ||
const [active, setActive] = React.useState(false); | ||
// const [selected, setSelected] = React.useState(defaultSelected ?? []); | ||
|
||
const firstSelected = options?.find( | ||
(option) => option.value === defaultSelected?.[0], | ||
)?.label; | ||
|
||
const activator = ( | ||
<UnstyledButton | ||
onClick={() => setActive((active) => !active)} | ||
className={styles.Select} | ||
> | ||
<InlineGrid columns="1fr auto" alignItems="center"> | ||
<BlockStack gap="100" inlineAlign="start"> | ||
<Text as="h3" variant="bodySm" tone="subdued"> | ||
{title} | ||
</Text> | ||
{defaultSelected?.length && ( | ||
<Text as="span" variant="bodyMd"> | ||
{firstSelected} | ||
</Text> | ||
)} | ||
</BlockStack> | ||
<Icon source={SelectMinor} tone="subdued" /> | ||
</InlineGrid> | ||
</UnstyledButton> | ||
); | ||
|
||
return ( | ||
<Popover | ||
fullWidth | ||
active={active} | ||
onClose={() => setActive(false)} | ||
// TODO: Allow coverage of activator | ||
// preferredAlignment="cover" | ||
activator={activator} | ||
> | ||
<Options /> | ||
</Popover> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Select/Select'; | ||
export * from './Options/Options'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters