This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
32 changed files
with
5,873 additions
and
2,745 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,157 @@ | ||
import React, { useState, useRef, useEffect } from 'react' | ||
import { useCombobox } from 'downshift' | ||
import { noop } from '../../utilities/other' | ||
import { | ||
itemToString, | ||
isItemSelected, | ||
renderListContents, | ||
setInitialSelection, | ||
} from './DropList.utils' | ||
import { | ||
getA11ySelectionMessageCommon, | ||
onIsOpenChangeCommon, | ||
onStateChangeCommon, | ||
stateReducerCommon, | ||
} from './DropList.downshift.common' | ||
import { | ||
DropListWrapperUI, | ||
InputSearchHolderUI, | ||
MenuListUI, | ||
} from './DropList.css' | ||
import ListItem, { generateListItemKey } from './DropList.ListItem' | ||
import { VARIANTS } from './DropList.constants' | ||
|
||
function Combobox({ | ||
closeOnSelection = true, | ||
customEmptyList = null, | ||
'data-cy': dataCy = `DropList.${VARIANTS.COMBOBOX}`, | ||
initialSelectedItem, | ||
isOpen = false, | ||
items = [], | ||
onSelectionChange = noop, | ||
renderCustomListItem = null, | ||
toggleOpenedState = noop, | ||
withMultipleSelection = false, | ||
}) { | ||
const initialSelectedItemsArr = setInitialSelection({ | ||
initialSelectedItem, | ||
withMultipleSelection, | ||
}) | ||
const [inputItems, setInputItems] = useState(items) | ||
const [selectedItems, setSelectedItems] = useState(initialSelectedItemsArr) | ||
const inputEl = useRef(null) | ||
|
||
const { | ||
getComboboxProps, | ||
getInputProps, | ||
getItemProps, | ||
getMenuProps, | ||
highlightedIndex, | ||
inputValue, | ||
selectItem, | ||
selectedItem, | ||
} = useCombobox({ | ||
initialInputValue: '', | ||
initialIsOpen: isOpen, | ||
initialSelectedItem, | ||
isOpen, | ||
items: inputItems, | ||
itemToString, | ||
|
||
getA11ySelectionMessage: ({ selectedItem }) => { | ||
return getA11ySelectionMessageCommon({ | ||
selectedItem, | ||
selectedItems, | ||
withMultipleSelection, | ||
}) | ||
}, | ||
|
||
onInputValueChange({ inputValue }) { | ||
setInputItems( | ||
items.filter(item => | ||
itemToString(item).toLowerCase().startsWith(inputValue.toLowerCase()) | ||
) | ||
) | ||
}, | ||
|
||
onIsOpenChange(changes) { | ||
onIsOpenChangeCommon({ | ||
closeOnSelection, | ||
toggleOpenedState, | ||
type: `${VARIANTS.COMBOBOX}.${changes.type}`, | ||
}) | ||
}, | ||
|
||
onStateChange(changes) { | ||
onStateChangeCommon({ | ||
changes, | ||
onSelectionChange, | ||
selectItem, | ||
selectedItems, | ||
setSelectedItems, | ||
type: `${VARIANTS.COMBOBOX}.${changes.type}`, | ||
withMultipleSelection, | ||
}) | ||
}, | ||
|
||
stateReducer(state, actionAndChanges) { | ||
const { changes, type } = actionAndChanges | ||
|
||
return stateReducerCommon({ | ||
changes, | ||
closeOnSelection, | ||
selectedItems, | ||
state, | ||
type: `${VARIANTS.COMBOBOX}.${type}`, | ||
withMultipleSelection, | ||
}) | ||
}, | ||
}) | ||
|
||
useEffect(() => { | ||
isOpen && inputEl.current.focus() | ||
}, [isOpen]) | ||
|
||
function renderListItem(item, index) { | ||
const itemProps = { | ||
highlightedIndex, | ||
index, | ||
isSelected: isItemSelected({ | ||
item, | ||
selectedItem, | ||
selectedItems, | ||
}), | ||
item, | ||
key: generateListItemKey(item, index), | ||
withMultipleSelection, | ||
renderCustomListItem, | ||
...getItemProps({ item, index }), | ||
} | ||
|
||
return <ListItem {...itemProps} /> | ||
} | ||
|
||
return ( | ||
<DropListWrapperUI | ||
className="DropList DropList__Combobox" | ||
data-cy={dataCy} | ||
variant="combobox" | ||
{...getComboboxProps()} | ||
> | ||
<InputSearchHolderUI show={items.length > 0}> | ||
<input {...getInputProps({ ref: inputEl })} placeholder="Search" /> | ||
</InputSearchHolderUI> | ||
<MenuListUI className="MenuList MenuList-Combobox" {...getMenuProps()}> | ||
{renderListContents({ | ||
customEmptyList, | ||
emptyList: items.length === 0, | ||
inputValue, | ||
items: inputItems, | ||
renderListItem, | ||
})} | ||
</MenuListUI> | ||
</DropListWrapperUI> | ||
) | ||
} | ||
|
||
export default Combobox |
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,93 @@ | ||
import React, { forwardRef } from 'react' | ||
import { classNames } from '../../utilities/classNames' | ||
import { isFunction, isObject, isString } from '../../utilities/is' | ||
import { isItemADivider, isItemAGroupLabel } from './DropList.utils' | ||
import { | ||
DividerUI, | ||
GroupLabelUI, | ||
ListItemUI, | ||
SelectedBadge, | ||
} from './DropList.css' | ||
|
||
const ListItem = forwardRef( | ||
( | ||
{ | ||
item, | ||
index, | ||
highlightedIndex, | ||
withMultipleSelection, | ||
isSelected, | ||
renderCustomListItem, | ||
...itemProps | ||
}, | ||
ref | ||
) => { | ||
if (isItemADivider(item)) { | ||
return ( | ||
<DividerUI className="DropListItem--divider" key={`divider_${index}`} /> | ||
) | ||
} | ||
|
||
if (isItemAGroupLabel(item)) { | ||
return ( | ||
<GroupLabelUI | ||
className="DropListItem--groupLabel" | ||
key={`group_label_${index}`} | ||
> | ||
{item.label} | ||
</GroupLabelUI> | ||
) | ||
} | ||
|
||
function getListItemClassNames(extraClassNames) { | ||
return classNames( | ||
'DropListItem', | ||
isSelected && 'is-selected', | ||
highlightedIndex === index && 'is-highlighted', | ||
withMultipleSelection && 'with-multiple-selection', | ||
isString(extraClassNames) && extraClassNames | ||
) | ||
} | ||
|
||
if (renderCustomListItem != null && isFunction(renderCustomListItem)) { | ||
return ( | ||
<li | ||
className={getListItemClassNames('DropListItem--custom')} | ||
ref={ref} | ||
{...itemProps} | ||
> | ||
{renderCustomListItem({ | ||
item, | ||
isSelected, | ||
isHighlighted: highlightedIndex === index, | ||
withMultipleSelection, | ||
})} | ||
</li> | ||
) | ||
} | ||
|
||
return ( | ||
<ListItemUI | ||
className={getListItemClassNames()} | ||
highlighted={highlightedIndex === index} | ||
ref={ref} | ||
selected={isSelected} | ||
withMultipleSelection={withMultipleSelection} | ||
{...itemProps} | ||
> | ||
<span>{isObject(item) ? item.label : item}</span> | ||
{withMultipleSelection ? ( | ||
<SelectedBadge isSelected={isSelected} /> | ||
) : null} | ||
</ListItemUI> | ||
) | ||
} | ||
) | ||
|
||
export function generateListItemKey(item, index) { | ||
return isObject(item) | ||
? item.id || `${item.label}_${index}` | ||
: `${item}_${index}` | ||
} | ||
|
||
export default ListItem |
Oops, something went wrong.