import { ChangeEvent, Component } from 'react'; import { observer } from 'mobx-react'; import { Field } from 'mobx-react-form'; import classnames from 'classnames'; import { SliderPicker } from 'react-color'; import { DEFAULT_APP_SETTINGS } from '../../config'; interface IProps { field: Field; className?: string; focus?: boolean; }; interface IState { background: string; } class ColorPickerInput extends Component { static defaultProps = { className: null, focus: false, }; state = { background: DEFAULT_APP_SETTINGS.accentColor, }; inputElement: HTMLInputElement | null | undefined; componentDidMount() { if (this.props.focus) { this.focus(); } } onChange(e: ChangeEvent) { const { field } = this.props; field.onChange(e); } focus() { this.inputElement?.focus(); } handleChangeComplete = (color: { hex: string; }) => { const { field } = this.props; this.setState({ background: color.hex }); field.value = color.hex }; render() { const { field, className, } = this.props; let { type } = field; type = 'text'; return (
{ this.inputElement = element; }} disabled={field.disabled} />
this.onChange(e)} onBlur={field.onBlur} onFocus={field.onFocus} ref={element => { this.inputElement = element; }} disabled={field.disabled} />
); } } export default observer(ColorPickerInput);