feat: use radix ui for dropdown

This commit is contained in:
TrèsAbhi committed 2023-10-01 16:05:51 -05:00
1 parent ec5dfd7727
commit 4d1dfc1ae5
1 file changed
+21 -59
+21 -59
View File
@@ -1,7 +1,7 @@
'use client'; 'use client';
import { CopyIcon, PersonIcon, ReloadIcon } from '@radix-ui/react-icons'; import { CopyIcon, PersonIcon, ReloadIcon } from '@radix-ui/react-icons';
import { Button, TextField } from '@radix-ui/themes'; import { Button, DropdownMenu, TextField } from '@radix-ui/themes';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { ChangeEvent, useRef, useState } from 'react'; import { ChangeEvent, useRef, useState } from 'react';
import PageWrapper from '../../../components/PageWrapper'; import PageWrapper from '../../../components/PageWrapper';
@@ -11,7 +11,6 @@ import getWargamingResponse from '../../../core/blitz/getWargamingResponse';
import listPlayers, { import listPlayers, {
AccountListWithServer, AccountListWithServer,
} from '../../../core/blitz/listPlayers'; } from '../../../core/blitz/listPlayers';
import { theme } from '../../../stitches.config';
import { useSession } from '../../../stores/session'; import { useSession } from '../../../stores/session';
import { NormalizedTankStats, TanksStats } from '../../../types/tanksStats'; import { NormalizedTankStats, TanksStats } from '../../../types/tanksStats';
import SessionPage from '../../embeds/session/page'; import SessionPage from '../../embeds/session/page';
@@ -77,6 +76,8 @@ export default function Page() {
defaultValue={session.isTracking ? session.nickname : undefined} defaultValue={session.isTracking ? session.nickname : undefined}
ref={input} ref={input}
onChange={(event) => { onChange={(event) => {
event.stopPropagation();
if (event.target.value) { if (event.target.value) {
setShowSearchResults(true); setShowSearchResults(true);
setSearchResults(undefined); setSearchResults(undefined);
@@ -86,79 +87,40 @@ export default function Page() {
handleChange(event); handleChange(event);
}} }}
onBlur={() => setShowSearchResults(false)} onBlur={(event) => {
// TODO: remove this hack when https://github.com/radix-ui/primitives/issues/2193 is fixed
event.target.value.length === 1 && event.target.focus();
}}
placeholder="Search for a player..." placeholder="Search for a player..."
/> />
</TextField.Root> </TextField.Root>
{showSearchResults && ( <DropdownMenu.Root open={showSearchResults} modal={false}>
<div <DropdownMenu.Trigger>
style={{ <div />
display: 'flex', </DropdownMenu.Trigger>
position: 'absolute',
width: '100%', <DropdownMenu.Content>
top: '100%',
marginTop: 8,
flexDirection: 'column',
backgroundColor: theme.colors.componentNonInteractive,
borderRadius: 4,
padding: 8,
boxSizing: 'border-box',
}}
>
{searchResults === undefined ? ( {searchResults === undefined ? (
<div <DropdownMenu.Item disabled>Searching...</DropdownMenu.Item>
style={{
display: 'flex',
alignContent: 'center',
justifyContent: 'center',
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
Searching...
</div>
) : searchResults.length === 0 ? ( ) : searchResults.length === 0 ? (
<div <DropdownMenu.Item disabled>No results</DropdownMenu.Item>
style={{
display: 'flex',
alignContent: 'center',
justifyContent: 'center',
fontSize: 16,
color: theme.colors.textLowContrast,
}}
>
No results
</div>
) : ( ) : (
searchResults?.map(({ account_id: id, nickname, region }) => ( searchResults?.map(({ account_id: id, nickname, region }) => (
<button <DropdownMenu.Item
key={id} key={id}
className={styles.searchButton}
onClick={() => { onClick={() => {
setShowSearchResults(false); setShowSearchResults(false);
setSession(region, id, nickname); setSession(region, id, nickname);
}} }}
shortcut={REGION_NAMES[region]}
> >
<span {nickname}
style={{ </DropdownMenu.Item>
color: theme.colors.textHighContrast,
}}
>
{nickname}
</span>
<span
style={{
color: theme.colors.textLowContrast,
}}
>
{REGION_NAMES[region]}
</span>
</button>
)) ))
)} )}
</div> </DropdownMenu.Content>
)} </DropdownMenu.Root>
</div> </div>
<div style={{ display: 'flex', gap: 8 }}> <div style={{ display: 'flex', gap: 8 }}>