1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-21 08:53:20 +01:00
metamask-extension/ui/app/pages/swaps/searchable-item-list/item-list/item-list.component.js

131 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-10-06 20:28:38 +02:00
import React from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import Identicon from '../../../../components/ui/identicon'
import UrlIcon from '../../../../components/ui/url-icon'
2020-11-03 00:41:28 +01:00
export default function ItemList({
2020-10-06 20:28:38 +02:00
results = [],
onClickItem,
Placeholder,
listTitle,
maxListItems = 6,
searchQuery = '',
containerRef,
hideRightLabels,
hideItemIf,
listContainerClassName,
}) {
2020-11-03 00:41:28 +01:00
return results.length === 0 ? (
Placeholder && <Placeholder searchQuery={searchQuery} />
) : (
<div className="searchable-item-list">
{listTitle && (
<div className="searchable-item-list__title">{listTitle}</div>
)}
<div
className={classnames(
'searchable-item-list__list-container',
listContainerClassName,
2020-10-06 20:28:38 +02:00
)}
2020-11-03 00:41:28 +01:00
ref={containerRef}
>
{results.slice(0, maxListItems).map((result, i) => {
2020-11-05 16:35:58 +01:00
if (hideItemIf?.(result)) {
2020-11-03 00:41:28 +01:00
return null
}
2020-10-06 20:28:38 +02:00
2020-11-05 16:35:58 +01:00
const onClick = () => onClickItem?.(result)
2020-11-03 00:41:28 +01:00
const {
iconUrl,
identiconAddress,
selected,
disabled,
primaryLabel,
secondaryLabel,
rightPrimaryLabel,
rightSecondaryLabel,
IconComponent,
} = result
return (
<div
tabIndex="0"
className={classnames('searchable-item-list__item', {
'searchable-item-list__item--selected': selected,
'searchable-item-list__item--disabled': disabled,
})}
onClick={onClick}
onKeyUp={(e) => e.key === 'Enter' && onClick()}
key={`searchable-item-list-item-${i}`}
>
{(iconUrl || primaryLabel) && (
<UrlIcon url={iconUrl} name={primaryLabel} />
)}
{!(iconUrl || primaryLabel) && identiconAddress && (
<div className="searchable-item-list__identicon">
<Identicon address={identiconAddress} diameter={24} />
</div>
)}
{IconComponent && <IconComponent />}
<div className="searchable-item-list__labels">
<div className="searchable-item-list__item-labels">
{primaryLabel && (
<span className="searchable-item-list__primary-label">
{primaryLabel}
</span>
)}
{secondaryLabel && (
<span className="searchable-item-list__secondary-label">
{secondaryLabel}
</span>
)}
</div>
{!hideRightLabels &&
(rightPrimaryLabel || rightSecondaryLabel) && (
<div className="searchable-item-list__right-labels">
{rightPrimaryLabel && (
<span className="searchable-item-list__right-primary-label">
{rightPrimaryLabel}
</span>
)}
{rightSecondaryLabel && (
<span className="searchable-item-list__right-secondary-label">
{rightSecondaryLabel}
</span>
2020-10-06 20:28:38 +02:00
)}
</div>
2020-11-03 00:41:28 +01:00
)}
</div>
</div>
)
})}
2020-10-06 20:28:38 +02:00
</div>
2020-11-03 00:41:28 +01:00
</div>
)
2020-10-06 20:28:38 +02:00
}
ItemList.propTypes = {
2020-11-03 00:41:28 +01:00
results: PropTypes.arrayOf(
PropTypes.shape({
iconUrl: PropTypes.string,
selected: PropTypes.bool,
disabled: PropTypes.bool,
primaryLabel: PropTypes.string,
secondaryLabel: PropTypes.string,
rightPrimaryLabel: PropTypes.string,
rightSecondaryLabel: PropTypes.string,
}),
),
2020-10-06 20:28:38 +02:00
onClickItem: PropTypes.func,
Placeholder: PropTypes.func,
listTitle: PropTypes.string,
maxListItems: PropTypes.number,
searchQuery: PropTypes.string,
2020-11-03 00:41:28 +01:00
containerRef: PropTypes.shape({
current: PropTypes.instanceOf(window.Element),
}),
2020-10-06 20:28:38 +02:00
hideRightLabels: PropTypes.bool,
hideItemIf: PropTypes.func,
listContainerClassName: PropTypes.string,
}