UNPKG

@bigfishtv/cockpit

Version:

78 lines (59 loc) 3.48 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _class, _class2, _temp; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { withFormValue, createValue } from '@bigfishtv/react-forms'; import classnames from 'classnames'; import _debounce from 'lodash/debounce'; import _get from 'lodash/get'; var FocusField = withFormValue(_class = (_temp = _class2 = function (_Component) { _inherits(FocusField, _Component); function FocusField(props) { _classCallCheck(this, FocusField); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _this.updateFormValue = _debounce(_this.updateFormValue, 100); _this.state = { value: _get(props, 'formValue.value', '') }; return _this; } FocusField.prototype.updateFormValue = function updateFormValue(value) { this.props.formValue.update(value); }; FocusField.prototype.updateValue = function updateValue(value) { this.setState({ value: value }); this.updateFormValue(value); }; FocusField.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { if (nextProps.formValue.value != this.props.formValue.value) this.setState({ value: nextProps.formValue.value }); }; FocusField.prototype.render = function render() { var _this2 = this; var value = this.state.value; var _props = this.props, formValue = _props.formValue, className = _props.className, select = _props.select, props = _objectWithoutProperties(_props, ['formValue', 'className', 'select']); return React.createElement('input', _extends({ value: value != formValue.value ? value : formValue.value || '', onChange: function onChange(event) { return _this2.updateValue(event.target.value); }, className: classnames(className, 'input-focus', formValue.errorList.length && 'error') }, props)); }; return FocusField; }(Component), _class2.propTypes = { formValue: PropTypes.object.isRequired, className: PropTypes.string }, _class2.defaultProps = { formValue: createValue({ schema: null, value: '' }), className: null }, _temp)) || _class; export { FocusField as default };