summaryrefslogtreecommitdiffstats
path: root/webapp/components/setting_item_max.jsx
blob: 1f0af181e81ae42ca483a8068be44565fb0908fe (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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import {FormattedMessage} from 'react-intl';
import * as Utils from 'utils/utils.jsx';
import Constants from 'utils/constants.jsx';

import PropTypes from 'prop-types';

import React from 'react';

export default class SettingItemMax extends React.Component {
    constructor(props) {
        super(props);

        this.onKeyDown = this.onKeyDown.bind(this);
    }

    onKeyDown(e) {
        if (e.keyCode === Constants.KeyCodes.ENTER && this.props.submit) {
            this.props.submit(e);
        }
    }

    componentDidMount() {
        document.addEventListener('keydown', this.onKeyDown);
    }

    componentWillUnmount() {
        document.removeEventListener('keydown', this.onKeyDown);
    }

    render() {
        var clientError = null;
        if (this.props.client_error) {
            clientError = (
                <div className='form-group'>
                    <label
                        id='clientError'
                        className='col-sm-12 has-error'
                    >
                        {this.props.client_error}
                    </label>
                </div>
            );
        }

        var serverError = null;
        if (this.props.server_error) {
            serverError = (
                <div className='form-group'>
                    <label
                        id='serverError'
                        className='col-sm-12 has-error'
                    >
                        {this.props.server_error}
                    </label>
                </div>
            );
        }

        var extraInfo = null;
        let hintClass = 'setting-list__hint';
        if (this.props.infoPosition === 'top') {
            hintClass = 'padding-bottom x2';
        }

        if (this.props.extraInfo) {
            extraInfo = (<div className={hintClass}>{this.props.extraInfo}</div>);
        }

        var submit = '';
        if (this.props.submit) {
            submit = (
                <input
                    id='saveSetting'
                    type='submit'
                    className='btn btn-sm btn-primary'
                    href='#'
                    onClick={this.props.submit}
                    value={Utils.localizeMessage('setting_item_max.save', 'Save')}
                />
            );
        }

        var inputs = this.props.inputs;
        var widthClass;
        if (this.props.width === 'full') {
            widthClass = 'col-sm-12';
        } else if (this.props.width === 'medium') {
            widthClass = 'col-sm-10 col-sm-offset-2';
        } else {
            widthClass = 'col-sm-9 col-sm-offset-3';
        }

        let title;
        let titleProp = 'unknownTitle';
        if (this.props.title) {
            title = <li className='col-sm-12 section-title'>{this.props.title}</li>;
            titleProp = this.props.title;
        }

        let listContent = (
            <li className='setting-list-item'>
                {inputs}
                {extraInfo}
            </li>
        );

        if (this.props.infoPosition === 'top') {
            listContent = (
                <li>
                    {extraInfo}
                    {inputs}
                </li>
            );
        }

        let cancelButtonText;
        if (this.props.cancelButtonText) {
            cancelButtonText = this.props.cancelButtonText;
        } else {
            cancelButtonText = (
                <FormattedMessage
                    id='setting_item_max.cancel'
                    defaultMessage='Cancel'
                />
            );
        }

        return (
            <ul className='section-max form-horizontal'>
                {title}
                <li className={widthClass}>
                    <ul className='setting-list'>
                        {listContent}
                        <li className='setting-list-item'>
                            <hr/>
                            {this.props.submitExtra}
                            {serverError}
                            {clientError}
                            {submit}
                            <a
                                id={Utils.createSafeId(titleProp) + 'Cancel'}
                                className='btn btn-sm'
                                href='#'
                                onClick={this.props.updateSection}
                            >
                                {cancelButtonText}
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        );
    }
}

SettingItemMax.propTypes = {
    inputs: PropTypes.array,
    client_error: PropTypes.string,
    server_error: PropTypes.string,
    extraInfo: PropTypes.element,
    infoPosition: PropTypes.string,
    updateSection: PropTypes.func,
    submit: PropTypes.func,
    title: PropTypes.node,
    width: PropTypes.string,
    submitExtra: PropTypes.node,
    cancelButtonText: PropTypes.node
};

SettingItemMax.defaultProps = {
    infoPosition: 'bottom'
};