UNPKG

4.17 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 Button = _require.Button,
16 ButtonGroup = _require.ButtonGroup;
17
18/**
19 * Represents a component that provides buttons to switch between
20 * view modes.
21 */
22
23
24var ViewSwitcher = function (_React$Component) {
25 _inherits(ViewSwitcher, _React$Component);
26
27 function ViewSwitcher() {
28 _classCallCheck(this, ViewSwitcher);
29
30 return _possibleConstructorReturn(this, (ViewSwitcher.__proto__ || Object.getPrototypeOf(ViewSwitcher)).apply(this, arguments));
31 }
32
33 _createClass(ViewSwitcher, [{
34 key: 'buttonFactory',
35
36
37 /**
38 * return array of button components based on buttonLabels and activeButton
39 *
40 * @return {React.Fragment} array of buttons
41 */
42 value: function buttonFactory() {
43 var _this2 = this;
44
45 return this.props.buttonLabels.map(function (label, i) {
46 var active = _this2.props.activeButton === label;
47 var dataTestId = _this2.props.dataTestId + '-' + label.toLowerCase().replace(/ /g, '-');
48 return React.createElement(
49 Button,
50 {
51 key: label,
52 active: active,
53 'data-test-id': dataTestId,
54 disabled: _this2.props.disabled,
55 onClick: _this2.props.onClick.bind(_this2, label),
56 bsSize: 'xsmall' },
57 _this2.renderIcon(i),
58 label
59 );
60 });
61 }
62 }, {
63 key: 'renderIcon',
64 value: function renderIcon(i) {
65 if (this.props.iconClassNames[i]) {
66 return React.createElement('i', { className: this.props.iconClassNames[i], 'aria-hidden': true });
67 }
68 }
69
70 /**
71 * Render view switcher component.
72 *
73 * @returns {React.Component} The component.
74 */
75
76 }, {
77 key: 'render',
78 value: function render() {
79 var buttons = this.buttonFactory();
80 return React.createElement(
81 'div',
82 { className: 'view-switcher' },
83 React.createElement(
84 'span',
85 { className: 'view-switcher-label' },
86 this.props.label
87 ),
88 React.createElement(
89 ButtonGroup,
90 null,
91 buttons
92 )
93 );
94 }
95 }]);
96
97 return ViewSwitcher;
98}(React.Component);
99
100ViewSwitcher.propTypes = {
101 label: PropTypes.string,
102 buttonLabels: PropTypes.arrayOf(PropTypes.string).isRequired,
103 activeButton: PropTypes.string,
104 disabled: PropTypes.bool,
105 dataTestId: PropTypes.string,
106 onClick: PropTypes.func,
107 iconClassNames: PropTypes.arrayOf(PropTypes.string)
108};
109
110ViewSwitcher.defaultProps = {
111 iconClassNames: []
112};
113
114ViewSwitcher.displayName = 'ViewSwitcher';
115
116module.exports = ViewSwitcher;
\No newline at end of file