throttle Color

This commit is contained in:
Daniel Duan 2018-04-12 13:21:41 -04:00
parent ff15cba310
commit 575d56bda7

View File

@ -2,6 +2,7 @@ import { document } from 'global';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import { SketchPicker } from 'react-color'; import { SketchPicker } from 'react-color';
import debounce from 'lodash.debounce';
const conditionalRender = (condition, positive, negative) => (condition ? positive() : negative()); const conditionalRender = (condition, positive, negative) => (condition ? positive() : negative());
@ -29,40 +30,50 @@ const styles = {
}; };
class ColorType extends React.Component { class ColorType extends React.Component {
constructor(props) { constructor(props, context) {
super(props); super(props, context);
this.handleClick = this.handleClick.bind(this);
this.onWindowMouseDown = this.onWindowMouseDown.bind(this);
this.state = { this.state = {
displayColorPicker: false, displayColorPicker: false,
value: props.knob.value,
}; };
this.onChange = debounce(props.onChange, 200);
} }
componentDidMount() { componentDidMount() {
document.addEventListener('mousedown', this.onWindowMouseDown); document.addEventListener('mousedown', this.handleWindowMouseDown);
} }
componentWillUnmount() { componentWillUnmount() {
document.removeEventListener('mousedown', this.onWindowMouseDown); document.removeEventListener('mousedown', this.handleWindowMouseDown);
} }
onWindowMouseDown(e) { handleWindowMouseDown = e => {
if (!this.state.displayColorPicker) return; if (!this.state.displayColorPicker) return;
if (this.popover.contains(e.target)) return; if (this.popover.contains(e.target)) return;
this.setState({ this.setState({
displayColorPicker: false, displayColorPicker: false,
}); });
} };
handleClick() { handleClick = () => {
this.setState({ this.setState({
displayColorPicker: !this.state.displayColorPicker, displayColorPicker: !this.state.displayColorPicker,
}); });
} };
handleChange = color => {
this.setState({
value: color,
});
this.onChange(`rgba(${color.rgb.r},${color.rgb.g},${color.rgb.b},${color.rgb.a})`);
};
render() { render() {
const { knob, onChange } = this.props; const { knob } = this.props;
const { displayColorPicker } = this.state; const { displayColorPicker, value } = this.state;
const colorStyle = { const colorStyle = {
width: 'auto', width: 'auto',
height: '20px', height: '20px',
@ -84,12 +95,7 @@ class ColorType extends React.Component {
this.popover = e; this.popover = e;
}} }}
> >
<SketchPicker <SketchPicker color={value} onChange={this.handleChange} />
color={knob.value}
onChange={color =>
onChange(`rgba(${color.rgb.r},${color.rgb.g},${color.rgb.b},${color.rgb.a})`)
}
/>
</div> </div>
), ),
() => null () => null