-
Notifications
You must be signed in to change notification settings - Fork 295
/
AssetsDropdown.tsx
78 lines (75 loc) · 2.16 KB
/
AssetsDropdown.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// @flow
import React from 'react';
import { omit, filter, escapeRegExp } from 'lodash';
import { discreetWalletTokenAmount } from '../../../features/discreet-mode/replacers/discreetWalletTokenAmount';
import ItemsDropdown from './ItemsDropdown';
import type { AssetToken } from '../../../api/assets/types';
import { useDiscreetModeFeature } from '../../../features/discreet-mode';
import Asset from '../../assets/Asset';
import styles from './AssetsDropdown.scss';
/**
*
* This component extends the ItemDropdownProps component
* which is based on React Polymorph's Select
* Any prop from it can be used
* Reference:
* https://github.com/input-output-hk/react-polymorph/blob/develop/source/components/Select.js
*
*/
type Props = {
assets?: Array<$Shape<AssetToken>>,
onSearch?: Function,
};
export const onSearchAssetsDropdown = (
searchValue: string,
options: Array<any>
) => {
return filter(options, ({ asset }) => {
if (searchValue.length < 3) {
return true;
}
const { policyId, assetName, fingerprint, metadata } = asset;
const { name, ticker, description } = metadata || {};
const checkList = [
policyId,
assetName,
fingerprint,
metadata,
name,
ticker,
description,
];
const regex = new RegExp(escapeRegExp(searchValue), 'i');
return checkList.some((item) => regex.test(item));
});
};
export default function AssetsDropdown({
assets = [],
onSearch = onSearchAssetsDropdown,
...props
}: Props) {
const discreetModeFeature = useDiscreetModeFeature();
const ItemsDropdownProps = {
...omit(props, ['wallets', 'options']),
onSearch,
};
const formattedOptions = assets.map((asset) => {
const { uniqueId: value, metadata, quantity, decimals } = asset;
const detail = discreetModeFeature.discreetValue({
replacer: discreetWalletTokenAmount({
amount: quantity,
metadata,
decimals,
}),
});
return {
label: (
<Asset asset={asset} className={styles.assetToken} hidePopOver small />
),
detail,
value,
asset,
};
});
return <ItemsDropdown options={formattedOptions} {...ItemsDropdownProps} />;
}