Skip to content

Commit

Permalink
feat: TokenSelect component introduce Token type
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioJames committed May 23, 2024
1 parent 7cf07ad commit 6cc22db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
11 changes: 10 additions & 1 deletion packages/web3/src/token-select/demos/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ const App: React.FC = () => {
name: 'Ethereum',
symbol: 'ETH',
icon: <EthereumColorful />,
contract: '0x',
decimal: 18,
availableChains: [
{
chain: {
name: 'Ethereum',
id: 1,
},
contract: '0x',
},
],
},
]}
/>
Expand Down
11 changes: 10 additions & 1 deletion packages/web3/src/token-select/demos/clear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ const App: React.FC = () => {
name: 'Ethereum',
symbol: 'ETH',
icon: <EthereumColorful />,
contract: '0x',
decimal: 18,
availableChains: [
{
chain: {
name: 'Ethereum',
id: 1,
},
contract: '0x',
},
],
},
]}
/>
Expand Down
55 changes: 12 additions & 43 deletions packages/web3/src/token-select/index.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,34 @@
import React, { Suspense, useCallback, useDeferredValue, useEffect, useRef, useState } from 'react';
import React, { useDeferredValue, useEffect, useState } from 'react';
import { CloseCircleOutlined, DownOutlined, SearchOutlined } from '@ant-design/icons';
import { Token } from '@ant-design/web3-common';
import { Dropdown, Flex, Input, InputRef, Spin } from 'antd';
import { debounce } from 'lodash';

import { useTokenSelectStyle } from './style';

export type TokenType = {
/**
* token fullname
*/
name: string;
/**
* token symbol
*/
symbol: string;
/**
* token icon
*/
icon: React.ReactNode;
/**
* token decimal
*/
decimal?: number;
/**
* contract address
*/
contract: string;
/**
* chain
*/
chain?: {
name: string;
icon: React.ReactNode;
};
};

export interface TokenSelectProps {
/**
* selected token
*/
value?: TokenType;
value?: Token;
/**
* change selected token callback
* @param token
* @returns
*/
onChange?: (token?: TokenType) => void;
onChange?: (token?: Token) => void;
/**
* controlled token list
*/
tokenList?: TokenType[];
tokenList?: Token[];
/**
* default token list
*/
defaultTokenList?: TokenType[];
defaultTokenList?: Token[];
/**
* query allow select token list
* @returns token list
*/
queryTokenList?: () => Promise<TokenType[] | undefined>;
queryTokenList?: () => Promise<Token[] | undefined>;
/**
* allow clear
*/
Expand All @@ -73,7 +43,7 @@ const SingleToken = ({
onSelect,
className,
}: {
token?: TokenType;
token?: Token;
onSelect?: TokenSelectProps['onChange'];
className?: string;
}) => {
Expand All @@ -86,7 +56,6 @@ const SingleToken = ({
return (
<Flex
gap={12}
key={token.contract}
className={getClsName('token-profile', className)}
onClick={() => onSelect?.(token)}
>
Expand All @@ -107,7 +76,7 @@ export const TokenSelect = ({
const { wrapSSR, getClsName } = useTokenSelectStyle();

// full Token List
const [fullTokenList, setFullTokenList] = useState<TokenType[] | undefined>(defaultTokenList);
const [fullTokenList, setFullTokenList] = useState<Token[] | undefined>(defaultTokenList);

// filter token keyword
const [keyword, setKeyword] = useState<string>();
Expand All @@ -123,14 +92,14 @@ export const TokenSelect = ({
// deferred calculate show token list
const showTokenList = useDeferredValue(
keyword
? fullTokenList?.filter(({ name, symbol, contract }) => {
? fullTokenList?.filter(({ name, symbol, availableChains }) => {
const nameLower = name.toLowerCase();

const symbolLower = symbol.toLowerCase();

const keywordLower = keyword.toLowerCase();

return [nameLower, symbolLower, contract].some((content) =>
return [nameLower, symbolLower, availableChains[0]?.contract].some((content) =>
content.includes(keywordLower),
);
})
Expand Down Expand Up @@ -186,7 +155,7 @@ export const TokenSelect = ({

setOpen(false);
}}
key={token.contract}
key={token.availableChains?.[0]?.contract}
/>
);
})}
Expand Down

0 comments on commit 6cc22db

Please sign in to comment.