forked from Testaustime/testaustime-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageSelector.tsx
More file actions
75 lines (66 loc) · 1.44 KB
/
LanguageSelector.tsx
File metadata and controls
75 lines (66 loc) · 1.44 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use client";
import {
Group,
MantineSize,
SegmentedControl,
Select,
Text,
} from "@mantine/core";
import { useSettings } from "../../hooks/useSettings";
import { Locales } from "../../i18next";
import { useTranslation } from "react-i18next";
import { useId } from "react";
type LanguageSelectorType = "segmented" | "dropdown";
export type LanguageSelectorProps = {
locale: string;
type?: LanguageSelectorType;
showLabel?: boolean;
size?: MantineSize;
};
export const LanguageSelector = ({
locale,
type = "segmented",
showLabel = true,
size,
}: LanguageSelectorProps) => {
const { setLanguage } = useSettings();
const { t } = useTranslation();
const id = useId();
const data = [
{ label: "English", value: "en" },
{ label: "Suomi", value: "fi" },
];
const onChange = (value: string | null) => {
if (!value) return;
setLanguage(value as Locales);
};
const Component =
type === "segmented" ? (
<SegmentedControl
id={id}
size={size}
data={data}
value={locale}
onChange={onChange}
/>
) : (
<Select
id={id}
size={size}
data={data}
value={locale}
onChange={onChange}
style={{ width: 150 }}
/>
);
return showLabel ? (
<Group>
<label htmlFor={id}>
<Text>{t("profile.settings.language")}</Text>
</label>
{Component}
</Group>
) : (
Component
);
};