ep-react-ui
Version:
EP react UI elements.
224 lines (185 loc) • 10.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _isEmpty2 = require('lodash/isEmpty');
var _isEmpty3 = _interopRequireDefault(_isEmpty2);
var _isNull2 = require('lodash/isNull');
var _isNull3 = _interopRequireDefault(_isNull2);
var _omitBy2 = require('lodash/omitBy');
var _omitBy3 = _interopRequireDefault(_omitBy2);
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 _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; } /**
* Normal input text field
*/
var InputField = function (_PureComponent) {
_inherits(InputField, _PureComponent);
function InputField(props) {
_classCallCheck(this, InputField);
var _this = _possibleConstructorReturn(this, (InputField.__proto__ || Object.getPrototypeOf(InputField)).call(this, props));
_this.state = {
isFocus: false
};
// actions
_this._onFocus = _this._onFocus.bind(_this);
_this._onBlur = _this._onBlur.bind(_this);
return _this;
}
_createClass(InputField, [{
key: 'componentDidUpdate',
value: function componentDidUpdate() {
var multiline = this.props.multiline;
// For textarea only
if (multiline > 0) {
this.__inputEl.style.height = '';
this.__inputEl.style.height = Math.max(this.__inputEl.clientHeight, this.__inputEl.scrollHeight) + 'px';
}
}
}, {
key: 'render',
value: function render() {
var _classNames,
_this2 = this;
var _props = this.props,
children = _props.children,
className = _props.className,
value = _props.value,
type = _props.type,
name = _props.name,
label = _props.label,
placeholder = _props.placeholder,
theme = _props.theme,
icon = _props.icon,
hint = _props.hint,
disabled = _props.disabled,
readOnly = _props.readOnly,
inputProps = _props.inputProps,
errorText = _props.errorText,
multiline = _props.multiline,
maxlength = _props.maxlength,
onChange = _props.onChange;
var isFocus = this.state.isFocus;
var inputPropsObj = (0, _omitBy3.default)(_extends({}, inputProps, {
className: 'input',
value: value || '',
name: name || '',
disabled: disabled || null,
readOnly: readOnly || null,
rows: multiline || null,
type: multiline === 0 ? type : null,
onChange: onChange || null
}), _isNull3.default);
var TagName = multiline > 0 ? 'textarea' : 'input';
var $input = _react2.default.createElement(
'div',
{ className: (0, _classnames2.default)('input-field', className, (_classNames = {}, _defineProperty(_classNames, 'theme-' + theme, theme !== undefined), _defineProperty(_classNames, 'has-hint', errorText || hint || maxlength), _defineProperty(_classNames, '__is-focus', isFocus), _defineProperty(_classNames, '__is-active', value.length !== 0), _defineProperty(_classNames, '__is-error', !(0, _isEmpty3.default)(errorText) || maxlength && value.length > maxlength), _defineProperty(_classNames, '__is-disabled', disabled || false), _defineProperty(_classNames, '__is-readonly', readOnly || false), _classNames)) },
label && _react2.default.createElement(
'label',
{ className: 'input-label' },
label
),
placeholder && _react2.default.createElement(
'div',
{ className: (0, _classnames2.default)('input-placeholder', {
'__is-visible': (isFocus || !label) && value.length === 0
}) },
placeholder
),
_react2.default.createElement(TagName, _extends({}, inputPropsObj, {
ref: function ref(el) {
return _this2.__inputEl = el;
},
onFocus: this._onFocus,
onBlur: this._onBlur
})),
_react2.default.createElement('div', { className: 'input-line' }),
(errorText || hint) && _react2.default.createElement(
'div',
{ className: 'input-hint' },
errorText || hint
),
maxlength > 0 && _react2.default.createElement(
'div',
{ className: 'input-count' },
value.length,
' / ',
maxlength
),
children
);
if (!icon) {
return $input;
}
// Wrap with 'input-icon wrapper'
else {
return _react2.default.createElement(
'div',
{ className: 'input-icon' },
_react2.default.createElement('i', { className: icon }),
_react2.default.createElement(
'div',
{ className: 'input-icon-content' },
$input
)
);
}
}
}, {
key: '_onFocus',
value: function _onFocus(e) {
this.setState({
isFocus: true
});
if (this.props.onFocus) {
this.props.onFocus(e);
}
}
}, {
key: '_onBlur',
value: function _onBlur(e) {
this.setState({
isFocus: false
});
if (this.props.onBlur) {
this.props.onBlur(e);
}
}
}]);
return InputField;
}(_react.PureComponent);
InputField.propTypes = {
value: _propTypes2.default.string, // input value
type: _propTypes2.default.string, // input type
theme: _propTypes2.default.string, // input theme color
name: _propTypes2.default.string, // input name
label: _propTypes2.default.string, // input label
icon: _propTypes2.default.string, // input icon classes
placeholder: _propTypes2.default.string, // input placeholder text
hint: _propTypes2.default.string, // input hint
disabled: _propTypes2.default.bool, // input disabled
readOnly: _propTypes2.default.bool, // input readonly prop
inputProps: _propTypes2.default.object, // input props
maxlength: _propTypes2.default.number, // input max length
errorText: _propTypes2.default.string, // show error message (doesn't stack with hint)
multiline: _propTypes2.default.number, // convert to textarea rows if > 0
onChange: _propTypes2.default.func // onChange event
};
InputField.defaultProps = {
className: '',
value: '',
type: 'text',
multiline: 0,
maxlength: 0
};
exports.default = InputField;