UNPKG

4.31 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _style = require('./style.scss');
18
19var _style2 = _interopRequireDefault(_style);
20
21var _bind = require('classnames/bind');
22
23var _bind2 = _interopRequireDefault(_bind);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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; }
30
31function _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; }
32
33var Tab = function (_React$Component) {
34 _inherits(Tab, _React$Component);
35
36 function Tab(props) {
37 _classCallCheck(this, Tab);
38
39 var _this = _possibleConstructorReturn(this, (Tab.__proto__ || Object.getPrototypeOf(Tab)).call(this, props));
40
41 _this.formatCount = function () {
42 // Add thousands separator (',')
43 return _this.props.count.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
44 };
45
46 _this.handleClick = function (event) {
47 if (_this.props.onClick && !_this.props.disabled) {
48 _this.props.onClick(event, _this);
49 }
50 };
51
52 return _this;
53 }
54
55 _createClass(Tab, [{
56 key: 'render',
57 value: function render() {
58 var cx = _bind2.default.bind(_style2.default);
59 var tabActiveClass = this.props.active ? 'active' : null;
60 var tabDisabledClass = this.props.disabled ? 'disabled' : null;
61 var tabClasses = cx(_style2.default.tab, this.props.optClass, tabActiveClass, tabDisabledClass);
62
63 return _react2.default.createElement(
64 'div',
65 { className: tabClasses, onClick: this.handleClick, 'aria-selected': this.props.active },
66 this.props.titlePrefix,
67 this.props.title,
68 ' ',
69 this.props.count ? _react2.default.createElement(
70 'span',
71 { className: _style2.default.count },
72 '(',
73 this.formatCount(),
74 ')'
75 ) : null,
76 this.props.titleSuffix
77 );
78 }
79 }]);
80
81 return Tab;
82}(_react2.default.Component);
83
84Tab.defaultProps = {};
85Tab.propTypes = {
86 /**
87 * Whether the tab is active. Set by the tab wrapper component.
88 */
89 active: _propTypes2.default.bool,
90 /**
91 * Whether the tab is disabled.
92 */
93 disabled: _propTypes2.default.bool,
94 /**
95 * The tab count.
96 */
97 count: _propTypes2.default.number,
98 /**
99 * Optional styles to add to the tab header.
100 */
101 optClass: _propTypes2.default.string,
102 /**
103 * Optional styles to add to the tab content.
104 */
105 optTabContentClass: _propTypes2.default.string,
106 /**
107 * The tab title.
108 */
109 title: _propTypes2.default.string.isRequired,
110 /**
111 * Optional title prefix renders in front of the title.
112 */
113 titlePrefix: _propTypes2.default.node,
114 /**
115 * Optional title suffix renders after the title
116 */
117 titleSuffix: _propTypes2.default.node
118};
119exports.default = Tab;
\No newline at end of file