summaryrefslogtreecommitdiffstats
path: root/webapp/components/admin_console/color_setting.jsx
blob: 483b585eefeacdcf5eaaeb932ad6634afe96e3b2 (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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import Setting from './setting.jsx';

import React from 'react';
import PropTypes from 'prop-types';
import {ChromePicker} from 'react-color';

export default class ColorSetting extends React.PureComponent {
    static propTypes = {

        /*
         * The unique identifer for the admin console setting
         */
        id: PropTypes.string.isRequired,

        /*
         * The text/jsx display name for the setting
         */
        label: PropTypes.node.isRequired,

        /*
         * The text/jsx help text to display underneath the setting
         */
        helpText: PropTypes.node,

        /*
         * The hex color value
         */
        value: PropTypes.string.isRequired,

        /*
         * Function called when the input changes
         */
        onChange: PropTypes.func,

        /*
         * Set to disable the setting
         */
        disabled: PropTypes.bool
    }

    constructor(props) {
        super(props);

        this.state = {
            showPicker: false
        };
    }

    componentDidMount() {
        document.addEventListener('click', this.closePicker);
    }

    componentWillUnmount() {
        document.removeEventListener('click', this.closePicker);
    }

    handleChange = (color) => {
        this.props.onChange(this.props.id, color.hex);
    }

    togglePicker = () => {
        if (this.props.disabled) {
            this.setState({showPicker: false});
        }
        this.setState({showPicker: !this.state.showPicker});
    }

    closePicker = (e) => {
        if (!e.target.closest('.picker-' + this.props.id)) {
            this.setState({showPicker: false});
        }
    }

    onTextInput = (e) => {
        this.props.onChange(this.props.id, e.target.value);
    }

    render() {
        let picker;
        if (this.state.showPicker) {
            picker = (
                <div className={'color-picker__popover picker-' + this.props.id}>
                    <ChromePicker
                        color={this.props.value}
                        onChange={this.handleChange}
                    />
                </div>
            );
        }

        return (
            <Setting
                label={this.props.label}
                helpText={this.props.helpText}
                inputId={this.props.id}
            >
                <div className='input-group color-picker colorpicker-element'>
                    <input
                        type='text'
                        className='form-control'
                        value={this.props.value}
                        onChange={this.onTextInput}
                        disabled={this.props.disabled}
                    />
                    <span
                        className={'input-group-addon picker-' + this.props.id}
                        onClick={this.togglePicker}
                    >
                        <i style={{backgroundColor: this.props.value}}/>
                    </span>
                    {picker}
                </div>
            </Setting>
        );
    }
}