import { ChangeEvent, ChangeEventHandler, Component, createRef, RefObject, } from 'react'; import { observer } from 'mobx-react'; import classnames from 'classnames'; import { SliderPicker } from 'react-color'; import { noop } from 'lodash'; import { Field } from '../../@types/mobx-form.types'; interface IProps { field: Field; className?: string; focus?: boolean; onChange: ChangeEventHandler; } // TODO - [TS DEBT] check if field can be spread instead of having it single field attribute in interface @observer class ColorPickerInput extends Component { private inputElement: RefObject = createRef(); componentDidMount() { const { focus = false } = this.props; if (focus) { this.focus(); } } onChange(e: ChangeEvent) { const { field, onChange = noop } = this.props; onChange(e); if (field.onChange) { field.onChange(e); } } focus() { if (this.inputElement && this.inputElement.current) { this.inputElement.current.focus(); } } handleChangeComplete = (color: { hex: string }) => { const { field } = this.props; field.value = color.hex; }; render() { const { field, className = null } = this.props; let { type } = field; type = 'text'; return (
this.onChange(e)} onBlur={field.onBlur} onFocus={field.onFocus} ref={this.inputElement} disabled={field.disabled} />
); } } export default ColorPickerInput;