Skip to content

Commit 3bc792b

Browse files
committed
fix: improve chain dropdown UX
1 parent 1c88661 commit 3bc792b

File tree

2 files changed

+44
-36
lines changed
  • examples/vote-proposal/components/wallet
  • templates/connect-multi-chain/components/wallet

2 files changed

+44
-36
lines changed

examples/vote-proposal/components/wallet/Chain.tsx

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useEffect, useMemo, useState } from "react";
2-
import { Chain } from "@chain-registry/v2-types";
3-
import { matchSorter } from "match-sorter";
1+
import { useEffect, useMemo, useState } from 'react';
2+
import { Chain } from '@chain-registry/v2-types';
3+
import { matchSorter } from 'match-sorter';
44
import {
55
Avatar,
66
Box,
@@ -10,7 +10,7 @@ import {
1010
Text,
1111
ThemeProvider,
1212
useTheme,
13-
} from "@interchain-ui/react";
13+
} from '@interchain-ui/react';
1414

1515
export type ChainSelectProps = {
1616
chains: Chain[];
@@ -21,33 +21,36 @@ export type ChainSelectProps = {
2121
export function ChainSelect({
2222
chainName,
2323
chains = [],
24-
onChange = () => { },
24+
onChange = () => {},
2525
}: ChainSelectProps) {
2626
const { themeClass } = useTheme();
2727
const [value, setValue] = useState<string>();
28-
const [input, setInput] = useState<string>("");
28+
const [input, setInput] = useState<string>('');
2929

3030
const cache = useMemo(
3131
() =>
3232
chains.reduce(
33-
(cache, chain) => (cache[chain.chainName] = chain, cache),
34-
{} as Record<string, Chain[][number]>,
33+
(cache, chain) => ((cache[chain.chainName] = chain), cache),
34+
{} as Record<string, Chain[][number]>
3535
),
36-
[chains],
36+
[chains]
3737
);
3838

39-
const options = useMemo(() =>
40-
matchSorter(
41-
chains
42-
.map((chain) => ({
43-
logo: chain.logoURIs?.png || chain.logoURIs?.svg || "",
44-
value: chain.chainName,
45-
label: chain.prettyName,
46-
}))
47-
.filter((chain) => chain.value && chain.label),
48-
input,
49-
{ keys: ["value", "label"] },
50-
), [chains, input]);
39+
const options = useMemo(
40+
() =>
41+
matchSorter(
42+
chains
43+
.map((chain) => ({
44+
logo: chain.logoURIs?.png || chain.logoURIs?.svg || '',
45+
value: chain.chainName,
46+
label: chain.prettyName,
47+
}))
48+
.filter((chain) => chain.value && chain.label),
49+
input,
50+
{ keys: ['value', 'label'] }
51+
),
52+
[chains, input]
53+
);
5154

5255
useEffect(() => {
5356
if (!chainName) setValue(undefined);
@@ -85,23 +88,23 @@ export function ChainSelect({
8588
setValue(name);
8689
if (cache[name]) {
8790
onChange(cache[name].chainName);
91+
setInput(cache[name].prettyName || '');
8892
}
8993
}
9094
}}
91-
inputAddonStart={value && avatar
92-
? (
95+
inputAddonStart={
96+
value && avatar ? (
9397
<Avatar
9498
name={value as string}
9599
getInitials={(name) => name[0]}
96100
size="xs"
97101
src={avatar}
98102
fallbackMode="bg"
99103
attributes={{
100-
paddingX: "$4",
104+
paddingX: '$4',
101105
}}
102106
/>
103-
)
104-
: (
107+
) : (
105108
<Box
106109
display="flex"
107110
justifyContent="center"
@@ -110,19 +113,20 @@ export function ChainSelect({
110113
>
111114
<Skeleton width="24px" height="24px" borderRadius="$full" />
112115
</Box>
113-
)}
116+
)
117+
}
114118
styleProps={{
115119
width: {
116-
mobile: "100%",
117-
mdMobile: "350px",
120+
mobile: '100%',
121+
mdMobile: '350px',
118122
},
119123
}}
120124
>
121125
{options.map((option) => (
122126
<Combobox.Item key={option.value} textValue={option.label}>
123127
<ChainOption
124-
logo={option.logo ?? ""}
125-
label={option.label ?? ""}
128+
logo={option.logo ?? ''}
129+
label={option.label ?? ''}
126130
/>
127131
</Combobox.Item>
128132
))}
@@ -137,14 +141,14 @@ function ChainOption({ logo, label }: { logo: string; label: string }) {
137141
<Stack
138142
direction="horizontal"
139143
space="$4"
140-
attributes={{ alignItems: "center" }}
144+
attributes={{ alignItems: 'center' }}
141145
>
142146
<Avatar
143147
name={label}
144148
getInitials={(name) => name[0]}
145149
size="xs"
146150
src={logo}
147-
fallbackMode="bg"
151+
fallbackMode="initials"
148152
/>
149153

150154
<Text fontSize="$md" fontWeight="$normal" color="$text">

templates/connect-multi-chain/components/wallet/Chain.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type ChainSelectProps = {
2121
export function ChainSelect({
2222
chainName,
2323
chains = [],
24-
onChange = () => { },
24+
onChange = () => {},
2525
}: ChainSelectProps) {
2626
const { themeClass } = useTheme();
2727
const [value, setValue] = useState<string>();
@@ -88,6 +88,7 @@ export function ChainSelect({
8888
setValue(name);
8989
if (cache[name]) {
9090
onChange(cache[name].chainName);
91+
setInput(cache[name].prettyName || '');
9192
}
9293
}
9394
}}
@@ -123,7 +124,10 @@ export function ChainSelect({
123124
>
124125
{options.map((option) => (
125126
<Combobox.Item key={option.value} textValue={option.label}>
126-
<ChainOption logo={option.logo ?? ''} label={option.label ?? ''} />
127+
<ChainOption
128+
logo={option.logo ?? ''}
129+
label={option.label ?? ''}
130+
/>
127131
</Combobox.Item>
128132
))}
129133
</Combobox>
@@ -144,7 +148,7 @@ function ChainOption({ logo, label }: { logo: string; label: string }) {
144148
getInitials={(name) => name[0]}
145149
size="xs"
146150
src={logo}
147-
fallbackMode="bg"
151+
fallbackMode="initials"
148152
/>
149153

150154
<Text fontSize="$md" fontWeight="$normal" color="$text">

0 commit comments

Comments
 (0)