-
Notifications
You must be signed in to change notification settings - Fork 12
/
Languages.jsx
46 lines (41 loc) · 1.44 KB
/
Languages.jsx
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
import React from 'react';
import { useTranslation } from "react-i18next";
import { ListItemButton, Button } from '@mui/material'
import LanguageIcon from '@mui/icons-material/Language';
import PopperGrow from '../common/PopperGrow';
const Languages = () => {
const { i18n } = useTranslation();
const [locale, setLocale] = React.useState(i18n.language || 'en')
const [open, setOpen] = React.useState(false)
const anchorRef = React.useRef(null);
const handleClick = _lang => {
setLocale(_lang)
i18n.changeLanguage(_lang)
setOpen(false)
};
const toggleMenu = () => setOpen(!open)
const handleClose = event => {
if (anchorRef.current && anchorRef.current.contains(event.target))
return;
setOpen(false);
};
return (
<React.Fragment>
<Button startIcon={<LanguageIcon fontSize='inherit'/>} onClick={toggleMenu} ref={anchorRef}>
{locale.toUpperCase()}
</Button>
<PopperGrow open={open} anchorRef={anchorRef} handleClose={handleClose} minWidth="100px">
<div>
{
[{locale: 'en', name: 'English'}, {locale: 'es', name: "Español"}].map(lang => (
<ListItemButton selected={lang.locale === locale} key={lang.locale} onClick={() => handleClick(lang.locale)}>
{lang.name} - {lang.locale.toUpperCase()}
</ListItemButton>
))
}
</div>
</PopperGrow>
</React.Fragment>
);
}
export default Languages;