// 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 = (
); } return (
{picker}
); } }