dh-c
Version:
The front-end development engineers jimberton gulp react component
113 lines (93 loc) • 4.08 kB
JavaScript
;
exports.__esModule = true;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
var Input = function (_React$Component) {
_inherits(Input, _React$Component);
function Input(props) {
_classCallCheck(this, Input);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.state = {
value: ''
};
_this.handleChange = _this.handleChange.bind(_this);
_this.handleBlur = _this.handleBlur.bind(_this);
return _this;
}
Input.prototype.componentWillMount = function componentWillMount() {
if (this.props.value) {
this.state.value = this.props.value;
}
};
Input.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.state.value = nextProps.value;
}
};
Input.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
return !nextProps.value !== nextState.value;
};
Input.prototype.handleChange = function handleChange(e) {
var value = e.target.value;
if (this.props.onChange) {
this.props.onChange(value);
}
this.setState({ value: value });
};
Input.prototype.handleBlur = function handleBlur(e) {
var value = e.target.value;
if (this.props.onBlur) {
this.props.onBlur(e.target.value);
}
};
Input.prototype.render = function render() {
var _props = this.props,
width = _props.width,
placeholder = _props.placeholder;
return _react2.default.createElement(
'div',
{ className: 'dh-input', style: { width: width } },
this.props.addonBefore ? _react2.default.createElement(
'div',
{ className: 'dh-input-before' },
this.props.addonBefore
) : null,
_react2.default.createElement(
'div',
{ className: 'dh-input-warp' },
_react2.default.createElement('input', {
type: this.props.type || 'text',
className: 'dh-input-warp-inner',
onChange: this.handleChange,
onBlur: this.handleBlur,
placeholder: placeholder,
value: this.state.value,
disabled: this.props.disabled
})
),
this.props.addonAfter ? _react2.default.createElement(
'div',
{ className: 'dh-input-after' },
this.props.addonAfter
) : null
);
};
return Input;
}(_react2.default.Component);
Input.defaultProps = {
placeholder: '',
value: ''
};
Input.propsTypes = {
width: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.number, _react2.default.PropTypes.string]),
placeholder: _react2.default.PropTypes.string,
value: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.number]),
disabled: _react2.default.PropTypes.bool,
addonBefore: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.element]),
addonAfter: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.element])
};
exports.default = Input;