Skip to content

Commit

Permalink
feat: language select
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed Jul 19, 2024
1 parent 8c41462 commit d66a10f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/components/BasicSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useState } from 'react';

interface BasicSelectProps {
value: string;
setValue: (explorer: string) => void;
list: string[];
disabled?: boolean;
dataTest?: string;
}

export const BasicSelect = ({ list, value, setValue }: BasicSelectProps) => {
const [isOpen, setIsOpen] = useState(false);

const handleClick = () => {
setIsOpen(!isOpen);
};

const selectItem = (explorer?: string) => {
setIsOpen(false);
if (!explorer) return;
setValue(explorer);
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLLIElement>, explorer?: string) => {
if (event.key === 'Enter' || event.key === ' ') {
selectItem(explorer);
}
};

return (
<div>
<button onClick={handleClick}>{value}</button>
{isOpen && (
<ul role='listbox'>
{list.map((item) => (
<li
key={item}
role='option'
tabIndex={0}
aria-selected={item === value}
onClick={() => selectItem(item)}
onKeyDown={(event) => handleKeyDown(event, item)}
>
{item}
</li>
))}
</ul>
)}
</div>
);
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './SearchBar';
export * from './TotalValueLocked';
export * from './Title';
export * from './TitleBanner';
export * from './BasicSelect';
3 changes: 2 additions & 1 deletion src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Constants } from '~/types';

const constants: Constants = {
// Put your project constants here
SUPPORTED_LANGUAGES: ['en', 'es'],
DEFAULT_LANG: 'en',
};

export const getConstants = (): Constants => {
Expand Down
24 changes: 21 additions & 3 deletions src/containers/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { ConnectButton } from '@rainbow-me/rainbowkit';
import { styled } from '@mui/material/styles';
import { IconButton } from '@mui/material';
import LightModeIcon from '@mui/icons-material/LightMode';
import DarkModeIcon from '@mui/icons-material/DarkMode';
import { useRouter } from 'next/router';
import { useTranslation } from 'next-i18next';

import { BasicSelect } from '~/components';
import { useCustomTheme } from '~/hooks/useContext/useTheme';
import { zIndex, HEADER_HEIGHT } from '~/utils';
import { getConfig } from '~/config';

const { DEFAULT_LANG } = getConfig();

export const Header = () => {
const { changeTheme, theme } = useCustomTheme();
const {
t,
i18n: { changeLanguage, language },
} = useTranslation();
const { locales, replace } = useRouter();

const localesMap = locales ? Object.fromEntries(locales.map((locale) => [locale, t(`LOCALES.${locale}`)])) : {};

const handleChangeLanguage = (value: string) => {
const locale = Object.keys(localesMap).find((key) => localesMap[key] === value) || DEFAULT_LANG;
replace('/', undefined, { locale: locale });
changeLanguage(locale);
};

return (
<StyledHeader>
<Logo>Logo</Logo>
<Logo>ZKchainHub</Logo>
<SIconButton onClick={changeTheme}>{theme === 'dark' ? <LightModeIcon /> : <DarkModeIcon />}</SIconButton>
<ConnectButton />
<BasicSelect value={t(`LOCALES.${language}`)} setValue={handleChangeLanguage} list={Object.values(localesMap)} />
</StyledHeader>
);
};
Expand Down
5 changes: 4 additions & 1 deletion src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export interface Env {
API_URL: string;
}

export type SupportedLanguage = 'en' | 'es';

export interface Constants {
// ...
SUPPORTED_LANGUAGES: SupportedLanguage[];
DEFAULT_LANG: SupportedLanguage;
}

export interface Config extends Env, Constants {}

0 comments on commit d66a10f

Please sign in to comment.