react-conventions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
176 lines (144 loc) • 6.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _bind = require('classnames/bind');
var _bind2 = _interopRequireDefault(_bind);
var _style = require('./style.scss');
var _style2 = _interopRequireDefault(_style);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
/**
* The Input component.
*/
var Input = function (_React$Component) {
_inherits(Input, _React$Component);
function Input(props) {
_classCallCheck(this, Input);
var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props));
_this.state = {
value: _this.props.value
};
_this.componentWillReceiveProps = function (nextProps) {
if (nextProps.value !== _this.props.value) {
_this.setState({ value: nextProps.value });
}
};
_this.handleChange = function (event) {
event.persist();
var value = _this.props.valueType === 'number' && event.target.value !== '' && !isNaN(event.target.value) ? parseFloat(event.target.value) : event.target.value;
_this.setState({ value: event.target.value }, function () {
if (typeof this.props.changeCallback === 'function') {
this.props.changeCallback({
target: {
name: this.props.name,
value: value
}
});
}
});
};
_this.handleFocus = function (event) {
if (typeof _this.props.focusCallback === 'function') {
_this.props.focusCallback(event);
}
};
_this.handleBlur = function (event) {
if (typeof _this.props.blurCallback === 'function') {
_this.props.blurCallback(event);
}
};
_this.focus = function () {
_this._input.focus();
};
return _this;
}
_createClass(Input, [{
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
label = _props.label,
value = _props.value,
optClass = _props.optClass,
other = _objectWithoutProperties(_props, ['label', 'value', 'optClass']);
var cx = _bind2.default.bind(_style2.default);
var disabledClass = this.props.disabled ? _style2.default['input-disabled'] : '';
var inputClass = cx(_style2.default['input-component'], this.props.optClass, disabledClass);
return _react2.default.createElement(
'div',
{ className: inputClass },
label ? _react2.default.createElement(
'label',
null,
label
) : null,
_react2.default.createElement('input', _extends({
ref: function ref(c) {
return _this2._input = c;
},
value: this.state.value,
onFocus: this.handleFocus,
onChange: this.handleChange,
onBlur: this.handleBlur
}, other))
);
}
}]);
return Input;
}(_react2.default.Component);
Input.defaultProps = {
disabled: false,
value: '',
valueType: 'string'
};
Input.propTypes = {
/**
* Whether the input is disabled.
*/
disabled: _react2.default.PropTypes.bool,
/**
* Text shown above the input.
*/
label: _react2.default.PropTypes.string,
/**
* Value of the input.
*/
value: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.number, _react2.default.PropTypes.string]),
/**
* Type of the value.
*/
valueType: _react2.default.PropTypes.oneOf(['string', 'number']),
/**
* Optional placeholder text.
*/
placeholder: _react2.default.PropTypes.string,
/**
* Name of the input.
*/
name: _react2.default.PropTypes.string,
/**
* Optional styles to add to the input.
*/
optClass: _react2.default.PropTypes.string,
/**
* A callback function to be called when the input changes.
*/
changeCallback: _react2.default.PropTypes.func,
/**
* A callback function to be called when the input is focused.
*/
focusCallback: _react2.default.PropTypes.func,
/**
* A callback function to be called when the input is blurred.
*/
blurCallback: _react2.default.PropTypes.func
};
exports.default = Input;