sc-react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
278 lines (231 loc) • 9.71 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SelectField = undefined;
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 _reactClickOutside = require('react-click-outside');
var _reactClickOutside2 = _interopRequireDefault(_reactClickOutside);
var _bind = require('classnames/bind');
var _bind2 = _interopRequireDefault(_bind);
var _style = require('./style.scss');
var _style2 = _interopRequireDefault(_style);
var _Icon = require('../Icon');
var _Icon2 = _interopRequireDefault(_Icon);
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 SelectField = exports.SelectField = function (_React$Component) {
_inherits(SelectField, _React$Component);
function SelectField(props) {
_classCallCheck(this, SelectField);
var _this = _possibleConstructorReturn(this, (SelectField.__proto__ || Object.getPrototypeOf(SelectField)).call(this, props));
_this.state = {
isOpen: false,
value: _this.props.value
};
_this.componentWillMount = function () {
// Select item
if (_this.state.value !== '' && _this.getIndex(_this.state.value, _this.props.options) > -1) {
_this.selectItem(_this.state.value, _this.props.options);
}
// No value is passed in
else {
_this.setState({ selected: '', value: '' });
}
};
_this.componentWillReceiveProps = function (nextProps) {
if (nextProps.value === _this.state.value) {
return;
}
var validIndex = _this.getIndex(nextProps.value, nextProps.options) > -1;
if (nextProps.value === undefined || nextProps.value === null || !validIndex) {
_this.setState({ selected: '', value: '' });
} else {
_this.selectItem(nextProps.value, nextProps.options);
}
};
_this.handleClickOutside = function () {
if (_this.state.isOpen) _this.toggleSelectField();
};
_this.toggleSelectField = function () {
if (_this.props.disabled) return;
_this.setState({ isOpen: !_this.state.isOpen });
};
_this.selectOption = function (option, triggerCallback) {
_this.setState({ selected: option, value: option[_this.props.valueProp], isOpen: false }, function () {
if (triggerCallback && typeof _this.props.changeCallback === 'function') {
_this.props.changeCallback({
target: {
name: _this.props.name,
value: option[_this.props.valueProp],
option: option
}
});
}
});
};
_this.selectItem = function (value, options) {
var index = _this.getIndex(value, options);
if (index >= 0) {
_this.selectOption(options[index], false);
}
};
_this.getIndex = function (value, options) {
var optionIndex = -1;
options.map(function (option, index) {
if (option[_this.props.valueProp] === value) {
optionIndex = index;
}
});
return optionIndex;
};
_this.getDisplayText = function () {
if (_this.state.selected !== '') {
return _this.state.selected[_this.props.displayProp];
} else if (typeof _this.props.placeholder !== 'undefined') {
return _this.props.placeholder;
}
return 'Please select an option';
};
_this.getDisplayIcon = function () {
if (_this.state.selected && _this.state.selected.icon) {
return _react2.default.createElement(_Icon2.default, { name: _this.state.selected.icon, fill: _this.state.selected.iconColor || null, className: _style2.default.icon, height: '16', width: '16' });
} else if (_this.props.icon) {
return _react2.default.createElement(_Icon2.default, { name: _this.props.icon, className: _style2.default.icon, height: '16', width: '16' });
}
return null;
};
return _this;
}
_createClass(SelectField, [{
key: 'render',
value: function render() {
var _this2 = this;
var cx = _bind2.default.bind(_style2.default);
var disabledClass = this.props.disabled ? _style2.default['selectfield-disabled'] : '';
var activeClass = this.state.isOpen ? _style2.default['active'] : '';
var selectFieldClass = cx(_style2.default['selectfield-component'], activeClass, disabledClass, this.props.optClass);
var _props = this.props,
valueProp = _props.valueProp,
hideProp = _props.hideProp,
label = _props.label;
var options = this.props.options.map(function (option, index) {
return _react2.default.createElement(
'li',
{
key: index,
onClick: _this2.selectOption.bind(null, option, true),
className: hideProp && option[hideProp] ? _style2.default['hidden'] : undefined },
option.icon ? _react2.default.createElement(_Icon2.default, {
name: option.icon,
fill: option.iconColor || null,
className: _style2.default.icon,
height: '16',
width: '16' }) : null,
option[_this2.props.displayProp]
);
});
if (options.length === 0) {
options.push(_react2.default.createElement(
'li',
{ key: 0, className: _style2.default['not-clickable'] },
'Nothing to select'
));
}
var value = '';
if (this.state.selected) {
value = this.state.selected[valueProp];
}
return _react2.default.createElement(
'div',
{ className: selectFieldClass, style: this.props.style },
_react2.default.createElement('input', { type: 'hidden', name: 'selectfield-value', value: value }),
label && _react2.default.createElement(
'label',
null,
label
),
_react2.default.createElement(
'div',
{ className: _style2.default['selectfield-value'], onClick: this.toggleSelectField },
this.getDisplayIcon(),
_react2.default.createElement(
'span',
{ className: _style2.default['display-text'] },
this.getDisplayText()
),
_react2.default.createElement(_Icon2.default, { name: 'mbsy-caret', width: '10', height: '10' })
),
_react2.default.createElement(
'ul',
null,
options
)
);
}
}]);
return SelectField;
}(_react2.default.Component);
SelectField.defaultProps = {
disabled: false,
options: [],
valueProp: '',
displayProp: ''
};
SelectField.propTypes = {
/**
* A string to display as the placeholder text.
*/
placeholder: _propTypes2.default.string,
/**
* An array of objects which will be used as the options for the select field.
*/
options: _propTypes2.default.array.isRequired,
/**
* The value of the option to be selected.
*/
value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
/**
* Which field in the option object will be used as the value of the select field.
*/
valueProp: _propTypes2.default.string.isRequired,
/**
* Which field in the option object will be used as the display of the select field.
*/
displayProp: _propTypes2.default.string.isRequired,
/**
* Which field in the option object will be used to determine whether the option should be hidden.
*/
hideProp: _propTypes2.default.string,
/**
* Whether the select field is disabled.
*/
disabled: _propTypes2.default.bool,
/**
* Optional styles to add to the select field.
*/
optClass: _propTypes2.default.string,
/**
* A callback function to be called when an option is selected.
*/
changeCallback: _propTypes2.default.func,
/**
* Icon to be displayed on the left
*/
icon: _propTypes2.default.string,
/**
* Text shown above the select field.
*/
label: _propTypes2.default.string,
/**
* An optional style object to render inline CSS.
*/
style: _propTypes2.default.object
};
exports.default = (0, _reactClickOutside2.default)(SelectField);