LangListComponent.tsx 3.84 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1
import * as React from 'react';
Matija Obreza's avatar
Matija Obreza committed
2
import {withStyles} from '@material-ui/core/styles';
Maxym Borodenko's avatar
Maxym Borodenko committed
3

4
5
import languages from 'data/Languages';
import { withRouter } from 'react-router-dom';
Matija Obreza's avatar
Matija Obreza committed
6
7
8
9
import Button from '@material-ui/core/Button';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import ArrowDropDown from '@material-ui/icons/ArrowDropDown';
Maxym Borodenko's avatar
Maxym Borodenko committed
10
11
12
import { translate } from 'react-i18next';

interface ILangListComponentProps extends React.ClassAttributes<any> {
Matija Obreza's avatar
Matija Obreza committed
13
    classes?: any;
Maxym Borodenko's avatar
Maxym Borodenko committed
14
15
16
17
18
19
20
21
22
23
    i18n: any;
    location: any;
}

const styles = {
    /* tslint:disable */
    langMenuBtn: {
        minWidth: '48px',
        fontSize: '1.14rem',
        paddingRight: 0,
Matija Obreza's avatar
Matija Obreza committed
24
        textTransform: 'capitalize' as 'capitalize',
Maxym Borodenko's avatar
Maxym Borodenko committed
25
26
27
28
29
30
31
32
33
34
35

        '&:hover': {
            background: 'inherit',
            color: '#88ba42',
            textDecoration: 'none',
        },
    },

    menu: {
        '& > div+div': {
            top: '3.57rem !important',
Maxym Borodenko's avatar
Maxym Borodenko committed
36
37
38
39
40
41
42
43
44

            'html[dir="ltr"]' : {
                left: 'auto !important',
                right: '1.43rem',
            },
            'html[dir="rtl"]' : {
                right: 'auto !important',
                left: '1.43rem',
            },
Maxym Borodenko's avatar
Maxym Borodenko committed
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        },

        '& ul': {
            padding: '0.71rem 0',

            '& a:focus': {
                outline: 'none',
                textDecoration: 'none',
                background: '#e7e5df',
            },

            '& li': {
                paddingLeft: '1.43rem',
                fontFamily: 'Roboto-Regular',
                fontSize: '1rem',
                color: '#2B2924',

                '&:hover, &:focus': {
                    background: '#e7e5df',
                },
            },
        },
    },
};

class LangListComponent extends React.Component<ILangListComponentProps, any> {

    public constructor(props: any) {
        super(props);

        this.state = {
            anchorEl: null,
            open: false,
        };
    }

    public handleClick = (event) => {
        this.setState({ open: true, anchorEl: event.currentTarget });
    }


    public handleRequestClose = () => {
        this.setState({ open: false });
    }

    public render() {
91
92
93
94
95
        if (languages.length <= 1) {
            // No dropdown for a single language
            return null;
        }

Maxym Borodenko's avatar
Maxym Borodenko committed
96
97
        const { classes, i18n } = this.props;
        let currentLang = i18n.language;
98

Maxym Borodenko's avatar
Maxym Borodenko committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        languages.some((item, i, arr) => {
            if (item.short === i18n.language) {
                currentLang = item.label;
                return true;
            }
        });

        const arrow = {
            verticalAlign: 'middle',
        };

        return (
            <div>
                <Button
                    className={ classes.langMenuBtn }
Matija Obreza's avatar
Matija Obreza committed
114
                    color="secondary"
Maxym Borodenko's avatar
Maxym Borodenko committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                    aria-owns={ this.state.open ? 'lang-list' : null }
                    aria-haspopup="true"
                    onClick={ this.handleClick }
                >
                    { currentLang }
                    <ArrowDropDown style={ arrow } viewBox="0 0 16 24"/>
                </Button>
                <Menu
                    className={ classes.menu }
                    id="lang-list"
                    anchorEl={ this.state.anchorEl }
                    open={ this.state.open }
                    onClose={ this.handleRequestClose }
                >
                    {
130
                        // Using <a href="ru/" will reload app from server
Maxym Borodenko's avatar
Maxym Borodenko committed
131
                        languages.map((lang, index) => (
Matija Obreza's avatar
Matija Obreza committed
132
                            <a key={ (lang.short) } href={ `${lang.short === 'en' ? '' : lang.short}/` }><MenuItem>{ `${lang.label}` }</MenuItem></a>
Maxym Borodenko's avatar
Maxym Borodenko committed
133
134
135
136
137
138
139
140
141
142
143
                        ))
                    }
                </Menu>
            </div>
        );
    }
}

const styled = withStyles(styles)(LangListComponent);

export default translate()(withRouter(styled));