2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Fuse from 'fuse.js';
|
2023-08-14 18:08:59 +02:00
|
|
|
import { isEqualCaseInsensitive } from '../../../../../shared/modules/string-utils';
|
|
|
|
import { TextFieldSearch } from '../../../component-library';
|
|
|
|
import { BlockSize } from '../../../../helpers/constants/design-system';
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
export default class TokenSearch extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
error: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
onSearch: PropTypes.func,
|
|
|
|
error: PropTypes.string,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: PropTypes.object,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2019-12-04 03:21:55 +01:00
|
|
|
state = {
|
|
|
|
searchQuery: '',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
const { tokenList } = this.props;
|
|
|
|
this.tokenList = Object.values(tokenList);
|
|
|
|
this.tokenSearchFuse = new Fuse(this.tokenList, {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.45,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
|
|
|
keys: [
|
|
|
|
{ name: 'name', weight: 0.5 },
|
|
|
|
{ name: 'symbol', weight: 0.5 },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
handleSearch(searchQuery) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ searchQuery });
|
2021-09-09 22:56:27 +02:00
|
|
|
const fuseSearchResult = this.tokenSearchFuse.search(searchQuery);
|
|
|
|
const addressSearchResult = this.tokenList.filter((token) => {
|
|
|
|
return (
|
|
|
|
token.address &&
|
|
|
|
searchQuery &&
|
|
|
|
isEqualCaseInsensitive(token.address, searchQuery)
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const results = [...addressSearchResult, ...fuseSearchResult];
|
|
|
|
this.props.onSearch({ searchQuery, results });
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { error } = this.props;
|
|
|
|
const { searchQuery } = this.state;
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
return (
|
2023-08-14 18:08:59 +02:00
|
|
|
<TextFieldSearch
|
|
|
|
placeholder={this.context.t('search')}
|
2018-05-20 08:04:19 +02:00
|
|
|
value={searchQuery}
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange={(e) => this.handleSearch(e.target.value)}
|
2018-05-20 08:04:19 +02:00
|
|
|
error={error}
|
2020-10-12 23:10:51 +02:00
|
|
|
autoFocus
|
2023-08-14 18:08:59 +02:00
|
|
|
autoComplete={false}
|
|
|
|
width={BlockSize.Full}
|
2018-05-20 08:04:19 +02:00
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
}
|