cpui-components
Version:
187 lines (158 loc) • 6.77 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FormSelect = undefined;
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 _blacklist = require('blacklist');
var _blacklist2 = _interopRequireDefault(_blacklist);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _icons = require('./../../icons');
var _icons2 = _interopRequireDefault(_icons);
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 FormSelect = exports.FormSelect = function (_Component) {
_inherits(FormSelect, _Component);
function FormSelect() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, FormSelect);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = FormSelect.__proto__ || Object.getPrototypeOf(FormSelect)).call.apply(_ref, [this].concat(args))), _this), _this.getInitialState = function () {
return {
isValid: true,
validationIsActive: _this.props.alwaysValidate
};
}, _this.componentDidMount = function () {
if (_this.state.validationIsActive) {
_this.validateInput(_this.props.value);
}
}, _this.componentWillReceiveProps = function (newProps) {
if (_this.state.validationIsActive) {
if (newProps.value !== _this.props.value && newProps.value !== _this._lastChangeValue && !newProps.alwaysValidate) {
// reset validation state if the value was changed outside the component
return _this.setState({
isValid: true,
validationIsActive: false
});
}
_this.validateInput(newProps.value);
}
}, _this.handleChange = function (e) {
_this._lastChangeValue = e.target.value;
if (_this.props.onChange) {
_this.props.onChange(e.target.value);
}
}, _this.handleBlur = function () {
if (!_this.props.alwaysValidate) {
_this.setState({
validationIsActive: false
});
}
_this.validateInput(_this.props.value);
}, _this.validateInput = function (value) {
var newState = {
isValid: true
};
if (_this.props.required && (!value || value && !value.length)) {
newState.isValid = false;
}
if (!newState.isValid) {
newState.validationIsActive = true;
}
_this.setState(newState);
}, _this.renderIcon = function (icon) {
var iconClassname = (0, _classnames2.default)('FormSelect__arrows', {
'FormSelect__arrows--disabled': _this.props.disabled
});
return _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: icon }, className: iconClassname });
}, _this.render = function () {
// props
var props = (0, _blacklist2.default)(_this.props, 'prependEmptyOption', 'firstOption', 'alwaysValidate', 'htmlFor', 'id', 'label', 'onChange', 'options', 'required', 'requiredMessage', 'className');
// classes
var componentClass = (0, _classnames2.default)('FormField', {
'is-invalid': !_this.state.isValid
}, _this.props.className);
// validation message
var validationMessage = void 0;
if (!_this.state.isValid) {
validationMessage = _react2.default.createElement(
'div',
{ className: 'form-validation is-invalid' },
_this.props.requiredMessage
);
}
// dynamic elements
var forAndID = _this.props.htmlFor || _this.props.id;
var componentLabel = _this.props.label ? _react2.default.createElement(
'label',
{ className: 'FormLabel', htmlFor: forAndID },
_this.props.label
) : null;
// options
var options = _this.props.options.map(function (opt, i) {
return _react2.default.createElement(
'option',
{ key: 'option-' + i, value: opt.value, disabled: opt.disabled },
opt.label
);
});
if (_this.props.prependEmptyOption || _this.props.firstOption) {
options.unshift(_react2.default.createElement(
'option',
{ key: 'option-blank', value: '' },
_this.props.firstOption ? _this.props.firstOption : 'Select...'
));
}
return _react2.default.createElement(
'div',
{ className: componentClass },
componentLabel,
_react2.default.createElement(
'div',
{ className: 'u-pos-relative' },
_react2.default.createElement(
'select',
_extends({ className: 'FormInput FormSelect', id: forAndID, onChange: _this.handleChange, onBlur: _this.handleBlur }, props),
options
),
_this.renderIcon(_icons2.default.selectArrows)
),
validationMessage
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
return FormSelect;
}(_react.Component);
FormSelect.propTypes = {
alwaysValidate: _propTypes2.default.bool,
className: _propTypes2.default.string,
disabled: _propTypes2.default.bool,
firstOption: _propTypes2.default.string,
htmlFor: _propTypes2.default.string,
id: _propTypes2.default.string,
label: _propTypes2.default.string,
onChange: _propTypes2.default.func.isRequired,
options: _propTypes2.default.arrayOf(_propTypes2.default.shape({
label: _propTypes2.default.string,
value: _propTypes2.default.string,
disabled: _propTypes2.default.bool
})).isRequired,
prependEmptyOption: _propTypes2.default.bool,
required: _propTypes2.default.bool,
requiredMessage: _propTypes2.default.string,
value: _propTypes2.default.string
};
FormSelect.defaultProps = {
requiredMessage: 'This field is required'
};
exports.default = FormSelect;