1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/pages/import-token/token-list/token-list.component.js

74 lines
2.3 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { checkExistingAddresses } from '../../../helpers/utils/util';
import TokenListPlaceholder from './token-list-placeholder';
2018-05-20 08:04:19 +02:00
export default class TokenList extends Component {
2018-05-20 08:04:19 +02:00
static contextTypes = {
t: PropTypes.func,
};
2018-05-20 08:04:19 +02:00
static propTypes = {
tokens: PropTypes.array,
results: PropTypes.array,
selectedTokens: PropTypes.object,
onToggleToken: PropTypes.func,
};
2018-05-20 08:04:19 +02:00
2020-11-03 00:41:28 +01:00
render() {
const {
results = [],
selectedTokens = {},
onToggleToken,
tokens = [],
} = this.props;
2018-05-20 08:04:19 +02:00
2020-11-03 00:41:28 +01:00
return results.length === 0 ? (
<TokenListPlaceholder />
) : (
<div className="token-list">
<div className="token-list__title">
{this.context.t('searchResults')}
</div>
<div className="token-list__tokens-container">
{Array(6)
.fill(undefined)
.map((_, i) => {
const { symbol, name, address } = results[i] || {};
const tokenAlreadyAdded = checkExistingAddresses(address, tokens);
const onClick = () =>
!tokenAlreadyAdded && onToggleToken(results[i]);
2018-05-20 08:04:19 +02:00
2020-11-03 00:41:28 +01:00
return (
Boolean(results[i]?.iconUrl || symbol || name) && (
2020-11-03 00:41:28 +01:00
<div
className={classnames('token-list__token', {
'token-list__token--selected': selectedTokens[address],
'token-list__token--disabled': tokenAlreadyAdded,
})}
onClick={onClick}
onKeyPress={(event) => event.key === 'Enter' && onClick()}
2020-11-03 00:41:28 +01:00
key={i}
tabIndex="0"
2020-11-03 00:41:28 +01:00
>
2018-05-20 08:04:19 +02:00
<div
2020-11-03 00:41:28 +01:00
className="token-list__token-icon"
style={{
backgroundImage:
results[i]?.iconUrl && `url(${results[i]?.iconUrl})`,
2020-11-03 00:41:28 +01:00
}}
/>
<div className="token-list__token-data">
<span className="token-list__token-name">{`${name} (${symbol})`}</span>
2018-05-20 08:04:19 +02:00
</div>
2020-11-03 00:41:28 +01:00
</div>
)
);
2020-11-03 00:41:28 +01:00
})}
2018-05-20 08:04:19 +02:00
</div>
2020-11-03 00:41:28 +01:00
</div>
);
2018-05-20 08:04:19 +02:00
}
}