index.tsx 1.29 KB
Newer Older
1 2
import { Icon, Menu } from 'antd';
import { formatMessage, getLocale, setLocale } from 'umi-plugin-react/locale';
何乐's avatar
何乐 committed
3
import { ClickParam } from 'antd/es/menu';
4
import React from 'react';
5
import classNames from 'classnames';
duanledexianxianxian's avatar
duanledexianxianxian committed
6
import DefaultSettings, { localeProps } from 'config/defaultSettings';
7 8 9 10 11
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';

interface SelectLangProps {
  className?: string;
duanledexianxianxian's avatar
duanledexianxianxian committed
12
  locales: localeProps[];
13
}
duanledexianxianxian's avatar
duanledexianxianxian committed
14

何乐's avatar
何乐 committed
15
const SelectLang: React.FC<SelectLangProps> = props => {
duanledexianxianxian's avatar
duanledexianxianxian committed
16
  const { className, locales } = props;
17
  const selectedLang = getLocale();
18
  const changeLang = ({ key }: ClickParam): void => setLocale(key, false);
19 20
  const langMenu = (
    <Menu className={styles.menu} selectedKeys={[selectedLang]} onClick={changeLang}>
duanledexianxianxian's avatar
duanledexianxianxian committed
21 22 23 24
      {locales.map(({ key, icon, label }) => (
        <Menu.Item key={key}>
          <span role="img" aria-label={label}>
            {icon}
25
          </span>{' '}
duanledexianxianxian's avatar
duanledexianxianxian committed
26
          {label}
27 28 29 30
        </Menu.Item>
      ))}
    </Menu>
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
31
  return locales.length >= 1 ? (
32 33 34 35 36
    <HeaderDropdown overlay={langMenu} placement="bottomRight">
      <span className={classNames(styles.dropDown, className)}>
        <Icon type="global" title={formatMessage({ id: 'navBar.lang' })} />
      </span>
    </HeaderDropdown>
duanledexianxianxian's avatar
duanledexianxianxian committed
37
  ) : null;
38 39 40
};

export default SelectLang;