-
Notifications
You must be signed in to change notification settings - Fork 830
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'stage' into production
- Loading branch information
Showing
20 changed files
with
565 additions
and
145 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
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
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
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
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
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
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
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,153 @@ | ||
import * as React from 'react' | ||
import { useCombobox, useMultipleSelection } from 'downshift' | ||
import { Localized, useLocalization } from '@fluent/react' | ||
|
||
import { LabeledInput } from '../ui/ui' | ||
import { SelectedItemsList } from './selected-items-list' | ||
|
||
import './multiple-combobox.css' | ||
|
||
const Input = LabeledInput | ||
|
||
type Props = { | ||
items: string[] | ||
maxNumberOfSelectedElements?: number | ||
selectedItems: string[] | ||
setInputValue: React.Dispatch<React.SetStateAction<string>> | ||
setSelectedItems: (items: string[]) => void | ||
inputValue: string | ||
} | ||
|
||
export const MultipleCombobox: React.FC<Props> = ({ | ||
items, | ||
selectedItems, | ||
setSelectedItems, | ||
maxNumberOfSelectedElements, | ||
inputValue, | ||
setInputValue, | ||
}) => { | ||
const { l10n } = useLocalization() | ||
|
||
const { getDropdownProps, removeSelectedItem } = useMultipleSelection({ | ||
selectedItems, | ||
onStateChange({ selectedItems: newSelectedItems, type }) { | ||
switch (type) { | ||
case useMultipleSelection.stateChangeTypes.SelectedItemKeyDownBackspace: | ||
case useMultipleSelection.stateChangeTypes.SelectedItemKeyDownDelete: | ||
case useMultipleSelection.stateChangeTypes.DropdownKeyDownBackspace: | ||
case useMultipleSelection.stateChangeTypes.FunctionRemoveSelectedItem: | ||
setSelectedItems(newSelectedItems) | ||
break | ||
default: | ||
break | ||
} | ||
}, | ||
}) | ||
|
||
const { | ||
isOpen, | ||
getMenuProps, | ||
getInputProps, | ||
highlightedIndex, | ||
getItemProps, | ||
openMenu, | ||
} = useCombobox({ | ||
items, | ||
inputValue, | ||
selectedItem: null, | ||
stateReducer(_, actionAndChanges) { | ||
const { changes, type } = actionAndChanges | ||
|
||
switch (type) { | ||
case useCombobox.stateChangeTypes.InputKeyDownEnter: | ||
case useCombobox.stateChangeTypes.ItemClick: | ||
case useCombobox.stateChangeTypes.InputBlur: | ||
return { | ||
...changes, | ||
inputValue: '', | ||
} | ||
case useCombobox.stateChangeTypes.InputClick: | ||
return { | ||
...changes, | ||
isOpen: true, | ||
} | ||
default: | ||
return changes | ||
} | ||
}, | ||
onStateChange({ | ||
inputValue: newInputValue, | ||
type, | ||
selectedItem: newSelectedItem, | ||
}) { | ||
switch (type) { | ||
case useCombobox.stateChangeTypes.InputKeyDownEnter: | ||
case useCombobox.stateChangeTypes.ItemClick: | ||
case useCombobox.stateChangeTypes.InputBlur: | ||
if (newSelectedItem) { | ||
setSelectedItems([...selectedItems, newSelectedItem]) | ||
setInputValue('') | ||
} | ||
break | ||
case useCombobox.stateChangeTypes.InputChange: | ||
setInputValue(newInputValue) | ||
break | ||
default: | ||
break | ||
} | ||
}, | ||
}) | ||
|
||
const handleFocus: React.FocusEventHandler<HTMLInputElement> = () => { | ||
openMenu() | ||
} | ||
|
||
const handleClick: React.MouseEventHandler<HTMLInputElement> = () => { | ||
openMenu() | ||
} | ||
|
||
return ( | ||
<div className="multiple-sentence-domain-select"> | ||
<div> | ||
<Localized id="sentence-domain-select" attrs={{ label: true }}> | ||
<Input | ||
{...getInputProps( | ||
getDropdownProps({ | ||
preventKeyAction: isOpen, | ||
onFocus: handleFocus, | ||
onClick: handleClick, | ||
disabled: selectedItems.length === maxNumberOfSelectedElements, | ||
}) | ||
)} | ||
placeholder={l10n.getString('sentence-domain-select-placeholder')} | ||
dataTestId="sentence-domain-select" | ||
/> | ||
</Localized> | ||
</div> | ||
|
||
<ul {...getMenuProps()} className={isOpen ? 'downshift-open' : ''}> | ||
{isOpen && | ||
items.map((item, index) => ( | ||
<li | ||
style={ | ||
highlightedIndex === index | ||
? { backgroundColor: 'var(--light-grey)' } | ||
: {} | ||
} | ||
key={`${item}${index}`} | ||
{...getItemProps({ item, index })} | ||
data-testid={item}> | ||
{l10n.getString(item)} | ||
</li> | ||
))} | ||
</ul> | ||
|
||
{selectedItems && ( | ||
<SelectedItemsList | ||
selectedItems={selectedItems} | ||
removeItem={removeSelectedItem} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} |
70 changes: 70 additions & 0 deletions
70
web/src/components/multiple-combobox/multiple-combobox.css
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,70 @@ | ||
@import url('../media.css'); | ||
|
||
.multiple-sentence-domain-select { | ||
ul { | ||
background: #fff; | ||
margin: 0px 0 30px; | ||
position: absolute; | ||
z-index: var(--override-z-index); | ||
|
||
@media (--xl-up) { | ||
width: 700px; | ||
} | ||
|
||
@media (--md-down) { | ||
width: 90vw; | ||
} | ||
|
||
&.downshift-open { | ||
border: 1px solid var(--light-grey); | ||
box-shadow: 0 2px 5px 0 var(--light-grey); | ||
overflow: auto; | ||
max-height: 500px; | ||
} | ||
|
||
li { | ||
border-bottom: 1px solid var(--dark-grey); | ||
cursor: pointer; | ||
list-style: none; | ||
padding: 15px; | ||
|
||
&:last-of-type { | ||
border-bottom: none; | ||
} | ||
} | ||
|
||
@media (--xs-down) { | ||
font-size: var(--font-size-xs); | ||
} | ||
} | ||
|
||
input[type='text'] { | ||
@media (--xs-down) { | ||
font-size: var(--font-size-xs); | ||
|
||
&::placeholder { | ||
font-size: var(--font-size-xs); | ||
} | ||
} | ||
} | ||
} | ||
|
||
.selected-domains-list { | ||
margin-block-start: 30px; | ||
|
||
.selected-domain { | ||
display: inline-flex; | ||
align-items: center; | ||
padding-inline-start: 1.5rem; | ||
margin-inline-end: 1rem; | ||
margin-bottom: 1.5rem; | ||
background: var(--light-grey); | ||
border-radius: 15rem; | ||
} | ||
|
||
.selected-domain--button { | ||
border: none; | ||
padding: 0.8rem 1.5rem; | ||
padding-inline-start: 1rem; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
web/src/components/multiple-combobox/selected-items-list.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,34 @@ | ||
import * as React from 'react' | ||
import { useLocalization } from '@fluent/react' | ||
|
||
import { CloseIcon } from '../ui/icons' | ||
import VisuallyHidden from '../visually-hidden/visually-hidden' | ||
|
||
type Props = { | ||
selectedItems: string[] | ||
removeItem: (item: string) => void | ||
} | ||
|
||
export const SelectedItemsList: React.FC<Props> = ({ | ||
selectedItems, | ||
removeItem, | ||
}) => { | ||
const { l10n } = useLocalization() | ||
|
||
return ( | ||
<div className="selected-domains-list"> | ||
{selectedItems.map(item => ( | ||
<span key={`domain-${item}`} className="selected-domain"> | ||
{l10n.getString(item)} | ||
<button | ||
className="selected-domain--button" | ||
onClick={() => removeItem(item)} | ||
type="button"> | ||
<VisuallyHidden>Remove {l10n.getString(item)}</VisuallyHidden> | ||
<CloseIcon black /> | ||
</button> | ||
</span> | ||
))} | ||
</div> | ||
) | ||
} |
42 changes: 42 additions & 0 deletions
42
web/src/components/multiple-combobox/use-multiple-combox.ts
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,42 @@ | ||
import { useMemo, useState } from 'react' | ||
|
||
type UseMultipleComboBoxParams = { | ||
items: readonly string[] | ||
selectedItems: string[] | ||
} | ||
|
||
export const useMultipleComboBox = ({ | ||
items, | ||
selectedItems, | ||
}: UseMultipleComboBoxParams) => { | ||
const [inputValue, setInputValue] = useState('') | ||
|
||
const getFilteredItems = ({ | ||
elements, | ||
selectedItems, | ||
inputValue, | ||
}: { | ||
elements: readonly string[] | ||
selectedItems: string[] | ||
inputValue: string | ||
}) => { | ||
const lowerCasedInputValue = inputValue.toLowerCase() | ||
|
||
return elements.filter( | ||
element => | ||
!selectedItems.includes(element) && | ||
element.toLowerCase().startsWith(lowerCasedInputValue) | ||
) | ||
} | ||
|
||
const multipleComboBoxItems = useMemo( | ||
() => getFilteredItems({ elements: items, selectedItems, inputValue }), | ||
[selectedItems, inputValue] | ||
) | ||
|
||
return { | ||
multipleComboBoxItems, | ||
inputValue, | ||
setInputValue, | ||
} | ||
} |
Oops, something went wrong.