This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
color_input.jsx
104 lines (90 loc) · 2.77 KB
/
color_input.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import ReactDom from 'react-dom';
import PropTypes from 'prop-types';
import {ChromePicker} from 'react-color';
class ColorInput extends React.Component {
static propTypes = {
/*
* Selected color
*/
color: PropTypes.string.isRequired,
/*
* Function called when color changed. Takes hex format of color Ex: #ffeec0
*/
onChange: PropTypes.func
};
constructor(props) {
super(props);
this.state = {
idOpened: false
};
}
componentDidUpdate(prevProps, prevState) {
const {isOpened: prevIsOpened} = prevState;
const {isOpened} = this.state;
if (isOpened !== prevIsOpened) {
if (isOpened) {
document.addEventListener('click', this.checkClick);
} else {
document.removeEventListener('click', this.checkClick);
}
}
}
checkClick = (e) => {
const colorPickerDOMNode = ReactDom.findDOMNode(this.colorPicker);
if (!colorPickerDOMNode.contains(e.target)) {
this.setState({isOpened: false});
}
};
togglePicker = () => {
this.setState({isOpened: !this.state.isOpened});
};
handleChange = (newColorData) => {
const {hex} = newColorData;
const {onChange: handleChange} = this.props;
if (handleChange) {
handleChange(hex);
}
};
render() {
const {color} = this.props;
const {isOpened} = this.state;
return (
<div className='color-input input-group'>
<input
className='form-control'
type='text'
value={color}
readOnly={true}
/>
<span
className='input-group-addon'
onClick={this.togglePicker}
>
<i
className='color-icon'
style={{
backgroundColor: color
}}
/>
</span>
{isOpened && (
<div
ref={(item) => {
this.colorPicker = item;
}}
className='color-popover'
>
<ChromePicker
color={color}
onChange={this.handleChange}
/>
</div>
)}
</div>
);
}
}
export default ColorInput;