You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
5.8 KiB
201 lines
5.8 KiB
4 years ago
|
import React, {
|
||
|
useState,
|
||
|
useCallback,
|
||
|
useEffect,
|
||
|
useContext,
|
||
|
useRef,
|
||
4 years ago
|
} from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import classnames from 'classnames';
|
||
|
import { isEqual } from 'lodash';
|
||
|
import { I18nContext } from '../../../contexts/i18n';
|
||
|
import SearchableItemList from '../searchable-item-list';
|
||
|
import PulseLoader from '../../../components/ui/pulse-loader';
|
||
|
import UrlIcon from '../../../components/ui/url-icon';
|
||
4 years ago
|
|
||
4 years ago
|
export default function DropdownSearchList({
|
||
4 years ago
|
searchListClassName,
|
||
|
itemsToSearch,
|
||
|
selectPlaceHolderText,
|
||
|
fuseSearchKeys,
|
||
|
defaultToAll,
|
||
|
maxListItems,
|
||
|
onSelect,
|
||
|
startingItem,
|
||
|
onOpen,
|
||
|
onClose,
|
||
|
className = '',
|
||
|
externallySelectedItem,
|
||
|
selectorClosedClassName,
|
||
|
loading,
|
||
|
hideRightLabels,
|
||
|
hideItemIf,
|
||
|
listContainerClassName,
|
||
|
}) {
|
||
4 years ago
|
const t = useContext(I18nContext);
|
||
|
const [isOpen, setIsOpen] = useState(false);
|
||
|
const [selectedItem, setSelectedItem] = useState(startingItem);
|
||
4 years ago
|
const close = useCallback(() => {
|
||
4 years ago
|
setIsOpen(false);
|
||
|
onClose?.();
|
||
|
}, [onClose]);
|
||
4 years ago
|
|
||
4 years ago
|
const onClickItem = useCallback(
|
||
|
(item) => {
|
||
4 years ago
|
onSelect?.(item);
|
||
|
setSelectedItem(item);
|
||
|
close();
|
||
4 years ago
|
},
|
||
|
[onSelect, close],
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
|
const onClickSelector = useCallback(() => {
|
||
|
if (!isOpen) {
|
||
4 years ago
|
setIsOpen(true);
|
||
|
onOpen?.();
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [isOpen, onOpen]);
|
||
4 years ago
|
|
||
4 years ago
|
const prevExternallySelectedItemRef = useRef();
|
||
4 years ago
|
useEffect(() => {
|
||
4 years ago
|
prevExternallySelectedItemRef.current = externallySelectedItem;
|
||
|
});
|
||
|
const prevExternallySelectedItem = prevExternallySelectedItemRef.current;
|
||
4 years ago
|
|
||
|
useEffect(() => {
|
||
4 years ago
|
if (
|
||
|
externallySelectedItem &&
|
||
|
!isEqual(externallySelectedItem, selectedItem)
|
||
|
) {
|
||
4 years ago
|
setSelectedItem(externallySelectedItem);
|
||
4 years ago
|
} else if (prevExternallySelectedItem && !externallySelectedItem) {
|
||
4 years ago
|
setSelectedItem(null);
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [externallySelectedItem, selectedItem, prevExternallySelectedItem]);
|
||
4 years ago
|
|
||
4 years ago
|
const onKeyUp = (e) => {
|
||
|
if (e.key === 'Escape') {
|
||
4 years ago
|
close();
|
||
4 years ago
|
} else if (e.key === 'Enter') {
|
||
4 years ago
|
onClickSelector(e);
|
||
4 years ago
|
}
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
4 years ago
|
return (
|
||
4 years ago
|
<div
|
||
4 years ago
|
className={classnames('dropdown-search-list', className)}
|
||
|
onClick={onClickSelector}
|
||
4 years ago
|
onKeyUp={onKeyUp}
|
||
|
tabIndex="0"
|
||
4 years ago
|
>
|
||
|
{!isOpen && (
|
||
|
<div
|
||
4 years ago
|
className={classnames(
|
||
|
'dropdown-search-list__selector-closed-container',
|
||
|
selectorClosedClassName,
|
||
|
)}
|
||
4 years ago
|
>
|
||
|
<div className="dropdown-search-list__selector-closed">
|
||
4 years ago
|
{selectedItem?.iconUrl && (
|
||
|
<UrlIcon
|
||
|
url={selectedItem.iconUrl}
|
||
|
className="dropdown-search-list__selector-closed-icon"
|
||
|
name={selectedItem?.symbol}
|
||
|
/>
|
||
|
)}
|
||
|
{!selectedItem?.iconUrl && (
|
||
|
<div className="dropdown-search-list__default-dropdown-icon" />
|
||
|
)}
|
||
4 years ago
|
<div className="dropdown-search-list__labels">
|
||
|
<div className="dropdown-search-list__item-labels">
|
||
|
<span
|
||
4 years ago
|
className={classnames(
|
||
|
'dropdown-search-list__closed-primary-label',
|
||
|
{
|
||
|
'dropdown-search-list__select-default': !selectedItem?.symbol,
|
||
|
},
|
||
|
)}
|
||
|
>
|
||
|
{selectedItem?.symbol || selectPlaceHolderText}
|
||
4 years ago
|
</span>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<i className="fa fa-caret-down fa-lg dropdown-search-list__caret" />
|
||
|
</div>
|
||
|
)}
|
||
|
{isOpen && (
|
||
|
<>
|
||
|
<SearchableItemList
|
||
|
itemsToSearch={loading ? [] : itemsToSearch}
|
||
4 years ago
|
Placeholder={({ searchQuery }) =>
|
||
|
loading ? (
|
||
4 years ago
|
<div className="dropdown-search-list__loading-item">
|
||
|
<PulseLoader />
|
||
|
<div className="dropdown-search-list__loading-item-text-container">
|
||
4 years ago
|
<span className="dropdown-search-list__loading-item-text">
|
||
|
{t('swapFetchingTokens')}
|
||
|
</span>
|
||
4 years ago
|
</div>
|
||
|
</div>
|
||
4 years ago
|
) : (
|
||
4 years ago
|
<div className="dropdown-search-list__placeholder">
|
||
|
{t('swapBuildQuotePlaceHolderText', [searchQuery])}
|
||
|
</div>
|
||
|
)
|
||
4 years ago
|
}
|
||
4 years ago
|
searchPlaceholderText={t('swapSearchForAToken')}
|
||
|
fuseSearchKeys={fuseSearchKeys}
|
||
|
defaultToAll={defaultToAll}
|
||
|
onClickItem={onClickItem}
|
||
|
maxListItems={maxListItems}
|
||
4 years ago
|
className={classnames(
|
||
|
'dropdown-search-list__token-container',
|
||
|
searchListClassName,
|
||
|
{
|
||
|
'dropdown-search-list--open': isOpen,
|
||
|
},
|
||
|
)}
|
||
4 years ago
|
hideRightLabels={hideRightLabels}
|
||
|
hideItemIf={hideItemIf}
|
||
|
listContainerClassName={listContainerClassName}
|
||
|
/>
|
||
|
<div
|
||
4 years ago
|
className="dropdown-search-list__close-area"
|
||
4 years ago
|
onClick={(event) => {
|
||
4 years ago
|
event.stopPropagation();
|
||
|
setIsOpen(false);
|
||
|
onClose?.();
|
||
4 years ago
|
}}
|
||
|
/>
|
||
|
</>
|
||
|
)}
|
||
4 years ago
|
</div>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
DropdownSearchList.propTypes = {
|
||
|
itemsToSearch: PropTypes.array,
|
||
|
onSelect: PropTypes.func,
|
||
|
searchListClassName: PropTypes.string,
|
||
4 years ago
|
fuseSearchKeys: PropTypes.arrayOf(
|
||
|
PropTypes.shape({
|
||
|
name: PropTypes.string,
|
||
|
weight: PropTypes.number,
|
||
|
}),
|
||
|
),
|
||
4 years ago
|
defaultToAll: PropTypes.bool,
|
||
|
maxListItems: PropTypes.number,
|
||
|
startingItem: PropTypes.object,
|
||
|
onOpen: PropTypes.func,
|
||
|
onClose: PropTypes.func,
|
||
|
className: PropTypes.string,
|
||
|
externallySelectedItem: PropTypes.object,
|
||
|
loading: PropTypes.bool,
|
||
|
selectPlaceHolderText: PropTypes.string,
|
||
|
selectorClosedClassName: PropTypes.string,
|
||
|
hideRightLabels: PropTypes.bool,
|
||
|
hideItemIf: PropTypes.func,
|
||
|
listContainerClassName: PropTypes.string,
|
||
4 years ago
|
};
|