UNPKG

4.36 kBJavaScriptView Raw
1'use strict';
2
3var _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; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _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; }
8
9function _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; }
10
11var React = require('react');
12var PropTypes = require('prop-types');
13
14var _require = require('react-bootstrap'),
15 DropdownButton = _require.DropdownButton,
16 MenuItem = _require.MenuItem;
17
18/**
19 * An OptionSelector component is composed of a few components:
20 *
21 * 1. A subset of the options available in a React-Bootstrap dropdown:
22 * @see https://react-bootstrap.github.io/components.html#btn-dropdowns-props-dropdown-button
23 * 2. A label for the dropdown
24 * 3. An ordered object of key-value pairs, which populate the
25 * MenuItem list when the dropdown is activated.
26 */
27
28
29var OptionSelector = function (_React$Component) {
30 _inherits(OptionSelector, _React$Component);
31
32 function OptionSelector() {
33 _classCallCheck(this, OptionSelector);
34
35 return _possibleConstructorReturn(this, (OptionSelector.__proto__ || Object.getPrototypeOf(OptionSelector)).apply(this, arguments));
36 }
37
38 _createClass(OptionSelector, [{
39 key: 'render',
40
41
42 /**
43 * Renders the Option Selector component.
44 *
45 * @returns {React.Component} The component.
46 */
47 value: function render() {
48 var htmlLabel = this.constructor.renderLabel(this.props.label, this.props.id);
49
50 var menuItems = [];
51 for (var key in this.props.options) {
52 if (this.props.options.hasOwnProperty(key)) {
53 var label = this.props.options[key];
54 menuItems.push(React.createElement(
55 MenuItem,
56 { key: key, eventKey: key, href: '#' },
57 label
58 ));
59 }
60 }
61
62 return React.createElement(
63 'div',
64 { className: 'option-selector' },
65 htmlLabel,
66 React.createElement(
67 DropdownButton,
68 {
69 bsSize: this.props.bsSize,
70 className: this.props.className,
71 id: this.props.id,
72 onSelect: this.props.onSelect,
73 title: this.props.title,
74 disabled: this.props.disabled },
75 menuItems
76 )
77 );
78 }
79 }], [{
80 key: 'renderLabel',
81 value: function renderLabel(label, id) {
82 return label ? React.createElement(
83 'label',
84 { className: 'option-selector-label', htmlFor: id },
85 label
86 ) : null;
87 }
88 }]);
89
90 return OptionSelector;
91}(React.Component);
92
93OptionSelector.propTypes = {
94 id: PropTypes.string.isRequired,
95 className: PropTypes.string,
96 bsSize: PropTypes.string,
97 options: PropTypes.object.isRequired,
98 label: PropTypes.string,
99 // for titles with glyphicons, this has to accept string or object
100 title: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
101 onSelect: PropTypes.func,
102 disabled: PropTypes.bool
103};
104
105OptionSelector.defaultProps = {
106 label: '',
107 title: 'Select an option',
108 onSelect: function onSelect() {},
109 disabled: false
110};
111
112OptionSelector.displayName = 'OptionSelector';
113
114module.exports = OptionSelector;
\No newline at end of file