summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/LanguageSelector.tsx
blob: 17e17d8ac126e4e1ffb30507e471406b207e1fcd (plain)
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
import React, { useMemo } from "react";
import { Selector, SelectorProps } from "../components";

interface Props {
  options: readonly Language.Info[];
}

type RemovedSelectorProps<M extends boolean> = Omit<
  SelectorProps<Language.Info, M>,
  "label" | "placeholder"
>;

export type LanguageSelectorProps<M extends boolean> = Override<
  Props,
  RemovedSelectorProps<M>
>;

function getLabel(lang: Language.Info) {
  return lang.name;
}

export function LanguageSelector<M extends boolean = false>(
  props: LanguageSelectorProps<M>
) {
  const { options, ...selector } = props;

  const items = useMemo<SelectorOption<Language.Info>[]>(
    () =>
      options.map((v) => ({
        label: v.name,
        value: v,
      })),
    [options]
  );

  return (
    <Selector
      placeholder="Language..."
      options={items}
      label={getLabel}
      {...selector}
    ></Selector>
  );
}