d2-ui
Version:
102 lines (76 loc) • 5.08 kB
JavaScript
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; }; }();
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; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import CircularProgress from '../circular-progress/CircularProgress';
import DropDown from '../form-fields/DropDown.component';
var DropDownForSchemaReference = function (_Component) {
_inherits(DropDownForSchemaReference, _Component);
function DropDownForSchemaReference() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, DropDownForSchemaReference);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DropDownForSchemaReference.__proto__ || Object.getPrototypeOf(DropDownForSchemaReference)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
isLoading: true,
options: []
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(DropDownForSchemaReference, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
var schema = this.getSchema(); // getSchema returns a d2.schema (modelDefinition object)
schema.list({ paging: false, fields: 'displayName,id' }).then(function (collection) {
return collection.toArray();
}).then(function (options) {
return _this2.setState({ options: options, isLoading: false });
}).catch(function () {
return _this2.setState({ isLoading: false });
});
}
/**
* Gets a d2 modelDefinition for the `schema` prop.
*
* @returns {ModelDefinition}
* @throws When the `schema` is not a valid schema on the `d2.models` object.
*/
}, {
key: 'getSchema',
value: function getSchema() {
var _this3 = this;
var d2 = this.context.d2;
var isSchemaAvailable = function isSchemaAvailable() {
return _this3.props.schema && d2.models[_this3.props.schema];
};
if (isSchemaAvailable()) {
return d2.models[this.props.schema];
}
throw new Error(this.props.schema + ' is not a valid schema name on the d2.models object. Perhaps you forgot to load the schema or the schema does not exist.');
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
schema = _props.schema,
selectProps = _objectWithoutProperties(_props, ['schema']);
return this.isLoading ? React.createElement(CircularProgress, null) : React.createElement(DropDown, _extends({
menuItems: this.state.options
}, selectProps));
}
}]);
return DropDownForSchemaReference;
}(Component);
DropDownForSchemaReference.propTypes = {
schema: PropTypes.string.isRequired
};
DropDownForSchemaReference.contextTypes = {
d2: PropTypes.object
};
export default DropDownForSchemaReference;