hbp-react-ui
Version:
A library of useful user-interface components built with React and MobX
86 lines (63 loc) • 21.3 kB
JavaScript
;Object.defineProperty(exports, "__esModule", { value: true });exports.default = 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 _get = function get(object, property, receiver) {if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {var parent = Object.getPrototypeOf(object);if (parent === null) {return undefined;} else {return get(parent, property, receiver);}} else if ("value" in desc) {return desc.value;} else {var getter = desc.get;if (getter === undefined) {return undefined;}return getter.call(receiver);}};var _class, _desc, _value, _class2, _descriptor, _descriptor2;var _react = require('react');var _react2 = _interopRequireDefault(_react);
var _mobx = require('mobx');
var _mobxReact = require('mobx-react');
var _hbpReactUi = require('hbp-react-ui');
var _SelectWithOptions = require('./SelectWithOptions');var _SelectWithOptions2 = _interopRequireDefault(_SelectWithOptions);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _initDefineProp(target, property, descriptor, context) {if (!descriptor) return;Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 });}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;}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}function _initializerWarningHelper(descriptor, context) {throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');}
/**
* A combination of DynamicList and Select, each item selected from the dropdown list is added to the list of items
* @param {String} path - Location for the notification
* @param {Function} onUpdateValue - Called when the list of items changes
* @param {Array<String>} items - An array of strings representing the initial items
* @param {Array<Object>} options - An array of objects representing the possible options
*/var
DynamicListSelect = (0, _mobxReact.observer)(_class = (_class2 = function (_React$Component) {_inherits(DynamicListSelect, _React$Component);
function DynamicListSelect(props) {_classCallCheck(this, DynamicListSelect);var _this = _possibleConstructorReturn(this, (DynamicListSelect.__proto__ || Object.getPrototypeOf(DynamicListSelect)).call(this,
props));_initDefineProp(_this, 'items', _descriptor, _this);_initDefineProp(_this, 'option', _descriptor2, _this);return _this;
}_createClass(DynamicListSelect, [{ key: 'componentDidMount', value: function componentDidMount()
{
} }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(
nextProps) {
_get(DynamicListSelect.prototype.__proto__ || Object.getPrototypeOf(DynamicListSelect.prototype), 'componentWillReceiveProps', this).call(this, nextProps);
}
/**
* Render a dropdown list followed by a list of items that have been selected
*/ }, { key: 'render', value: function render()
{var _this2 = this;
if (typeof _hbp_debug_ != 'undefined') console.log('DynamicListSelect.render: ' + this.props.path);
var header =
_react2.default.createElement('div', null);
return (
_react2.default.createElement('div', null,
_react2.default.createElement(_SelectWithOptions2.default, {
path: this.props.path,
onSelect: this.onSelect.bind(this),
description: this.props.description,
options: this.props.options,
ref: function ref(component) {_this2.componentSelect = component;} }),
_react2.default.createElement(_hbpReactUi.DynamicList, {
path: this.props.path,
onUpdateList: this.props.onUpdateValue,
header: header,
items: this.items,
onAddItem: this.onAddItem.bind(this),
ref: function ref(component) {_this2.componentDynamicList = component;} })));
}
/**
* Notify the child when the selection changes
*/ }, { key: 'onSelect', value: function onSelect(
path, option) {
if (typeof _hbp_debug_ != 'undefined') console.log('DynamicListSelect.onSelect');
this.option = option;
if (this.option && this.option != '') {
this.componentDynamicList.addToList();
this.componentSelect.clearSelection();
}
}
/**
* Ask the child to add the current selection to the list
*/ }, { key: 'onAddItem', value: function onAddItem(
onAddItemAction) {
if (typeof _hbp_debug_ != 'undefined') console.log('DynamicListSelect.onAddItem');
if (this.option)
onAddItemAction(this.option.name);
} }]);return DynamicListSelect;}(_react2.default.Component), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'items', [_mobx.observable], { enumerable: true, initializer: function initializer() {return this.props.items || [];} }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'option', [_mobx.observable], { enumerable: true, initializer: null })), _class2)) || _class;exports.default = DynamicListSelect;