UNPKG

10.2 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 _templateObject = _taggedTemplateLiteral(['\n .state {\n position: absolute;\n left: -10000px;\n }\n .flex-tabs {\n display: flex;\n justify-content: space-between;\n flex-wrap: wrap;\n }\n .flex-tabs .tab {\n flex-grow: 1;\n max-height: 40px;\n }\n .flex-tabs .panel {\n background-color: #e5e9f2;\n padding: 2px;\n min-height: 300px;\n display: none;\n width: 100%;\n flex-basis: auto;\n }\n .tab {\n display: inline-block;\n padding: 10px;\n vertical-align: top;\n background-color: #eee;\n cursor: hand;\n cursor: pointer;\n border-left: 10px solid #ccc;\n }\n .tab:hover {\n background-color: #fff;\n }\n ', ' @media (max-width: 600px) {\n .flex-tabs {\n flex-direction: column;\n }\n .flex-tabs .tab {\n background: #fff;\n border-bottom: 1px solid #ccc;\n }\n .flex-tabs .tab:last-of-type {\n border-bottom: none;\n }\n ', ' ', ';\n }\n'], ['\n .state {\n position: absolute;\n left: -10000px;\n }\n .flex-tabs {\n display: flex;\n justify-content: space-between;\n flex-wrap: wrap;\n }\n .flex-tabs .tab {\n flex-grow: 1;\n max-height: 40px;\n }\n .flex-tabs .panel {\n background-color: #e5e9f2;\n padding: 2px;\n min-height: 300px;\n display: none;\n width: 100%;\n flex-basis: auto;\n }\n .tab {\n display: inline-block;\n padding: 10px;\n vertical-align: top;\n background-color: #eee;\n cursor: hand;\n cursor: pointer;\n border-left: 10px solid #ccc;\n }\n .tab:hover {\n background-color: #fff;\n }\n ', ' @media (max-width: 600px) {\n .flex-tabs {\n flex-direction: column;\n }\n .flex-tabs .tab {\n background: #fff;\n border-bottom: 1px solid #ccc;\n }\n .flex-tabs .tab:last-of-type {\n border-bottom: none;\n }\n ', ' ', ';\n }\n']);
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _styledComponents = require('styled-components');
16
17var _styledComponents2 = _interopRequireDefault(_styledComponents);
18
19var _reactRedux = require('react-redux');
20
21var _redux = require('redux');
22
23var _propTypes = require('prop-types');
24
25var _propTypes2 = _interopRequireDefault(_propTypes);
26
27var _if = require('./operator/if');
28
29var _if2 = _interopRequireDefault(_if);
30
31var _tabActions = require('./tabActions');
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
36
37function _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; }
38
39function _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; }
40
41function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } /* Tab pen from https://codepen.io/josh_vogt/pen/EaaZbP */
42
43
44var TabStyle = _styledComponents2.default.div(_templateObject, function (props) {
45 var tabs = props.tabs || [];
46 return tabs.map(function (value) {
47 return '\n #' + value.id + ':checked ~ .tabs #' + value.id + '-label {\n background-color: #fff;\n cursor: default;\n border-left-color: #69be28;\n }\n #' + value.id + ':checked ~ .tabs #' + value.id + '-panel {\n display: block;\n }\n ';
48 });
49}, function (props) {
50 var tabs = props.tabs || [];
51 return tabs.map(function (value, index) {
52 if (Math.abs((index + 1) % 2) === 1) {
53 return '\n .flex-tabs #' + value.id + '-label {\n order: ' + (index + 1) + ';\n }';
54 }
55 return '\n .flex-tabs #' + value.id + '-panel {\n order: ' + (index + 1) + ';\n }';
56 });
57}, function (props) {
58 var tabs = props.tabs || [];
59 return tabs.map(function (value) {
60 return '\n #' + value.id + ':checked ~ .tabs #' + value.id + '-label {\n border-bottom: none;\n }\n #' + value.id + ':checked ~ .tabs #' + value.id + '-panel {\n border-bottom: 1px solid #ccc;\n }';
61 });
62});
63
64var Tab = function (_PureComponent) {
65 _inherits(Tab, _PureComponent);
66
67 function Tab(props) {
68 _classCallCheck(this, Tab);
69
70 var _this = _possibleConstructorReturn(this, (Tab.__proto__ || Object.getPrototypeOf(Tab)).call(this, props));
71
72 var propsTabs = _this.props.tabs || [];
73 var tabVisible = _this.props.tabVisible || {};
74
75 _this.updateTabs = function (pTabs, pTabVisible) {
76 var tabsAlterKeys = [];
77 var newValue = [];
78 var itens = {};
79
80 if (Object.keys(pTabVisible).length >= 1) {
81 pTabs.map(function (value) {
82 return newValue.push(pTabVisible[value.id] || false);
83 });
84 }
85
86 var tabsAlter = {
87 visible: {
88 newValue: newValue
89 }
90 };
91
92 var newTabs = pTabs.map(function (oKey, index) {
93 tabsAlterKeys = Object.keys(tabsAlter);
94 tabsAlterKeys.map(function (tabsKey) {
95 return itens[tabsKey] = oKey[tabsKey] !== tabsAlter[tabsKey].newValue[index] ? tabsAlter[tabsKey].newValue[index] : oKey[tabsKey];
96 });
97 return Object.assign({}, oKey, itens);
98 });
99
100 return newTabs;
101 };
102
103 _this.state = { newTabs: _this.updateTabs(propsTabs, tabVisible) };
104
105 _this.handleInputChange = _this.handleInputChange.bind(_this);
106 _this.renderInputs = _this.renderInputs.bind(_this);
107 _this.renderLabels = _this.renderLabels.bind(_this);
108 return _this;
109 }
110
111 _createClass(Tab, [{
112 key: 'componentWillReceiveProps',
113 value: function componentWillReceiveProps(nextProps) {
114 if (!Object.is(this.props, nextProps)) {
115 this.setState({ newTabs: this.updateTabs(nextProps.tabs, nextProps.tabVisible) });
116 }
117 }
118 }, {
119 key: 'setRadioChecked',
120 value: function setRadioChecked(value) {
121 var selected = this.props.selected || this.props.tabs.filter(function (key) {
122 return key.active === true;
123 })[0].id;
124
125 return selected === value.id;
126 }
127 }, {
128 key: 'handleInputChange',
129 value: function handleInputChange(event) {
130 this.props.selectTab(event.target.id);
131 }
132 }, {
133 key: 'renderInputs',
134 value: function renderInputs() {
135 var _this2 = this;
136
137 return this.state.newTabs.map(function (value) {
138 return _react2.default.createElement(
139 _if2.default,
140 { key: 'if-input-' + value.id, test: value.visible },
141 _react2.default.createElement('input', {
142 key: value.id,
143 className: 'state',
144 type: 'radio',
145 title: value.id,
146 name: 'tabs-state',
147 id: value.id,
148 checked: _this2.setRadioChecked(value),
149 onChange: _this2.handleInputChange
150 })
151 );
152 });
153 }
154 }, {
155 key: 'renderLabels',
156 value: function renderLabels() {
157 return this.state.newTabs.map(function (value) {
158 return _react2.default.createElement(
159 _if2.default,
160 { key: 'if-label-' + value.id, test: value.visible },
161 _react2.default.createElement(
162 'label',
163 {
164 key: 'label-' + value.id,
165 htmlFor: value.id,
166 id: value.id + '-label',
167 className: 'tab'
168 },
169 value.tabCaption
170 )
171 );
172 });
173 }
174 }, {
175 key: 'render',
176 value: function render() {
177 return _react2.default.createElement(
178 TabStyle,
179 { tabs: this.state.newTabs },
180 this.renderInputs(),
181 _react2.default.createElement(
182 'div',
183 { className: 'tabs flex-tabs' },
184 this.renderLabels(),
185 this.state.newTabs.map(function (value) {
186 return _react2.default.createElement(
187 _if2.default,
188 { key: 'if-panel-' + value.id, test: value.visible },
189 _react2.default.createElement(
190 'div',
191 {
192 key: 'panel-' + value.id,
193 id: value.id + '-panel',
194 className: 'flex-tabs panel active'
195 },
196 value.content
197 )
198 );
199 })
200 )
201 );
202 }
203 }]);
204
205 return Tab;
206}(_react.PureComponent);
207
208Tab.defaultProps = {
209 tabs: [{}],
210 selected: '',
211 tabVisible: {}
212};
213
214Tab.propTypes = {
215 tabs: _propTypes2.default.arrayOf(_propTypes2.default.object),
216 selected: _propTypes2.default.string,
217 tabVisible: _propTypes2.default.object
218};
219
220var mapStateToProps = function mapStateToProps(state) {
221 return {
222 selected: state.tab.selected,
223 tabVisible: state.tab.tabVisible
224 };
225};
226var mapDispatchToProps = function mapDispatchToProps(dispatch) {
227 return (0, _redux.bindActionCreators)({ selectTab: _tabActions.selectTab }, dispatch);
228};
229
230exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(Tab);
\No newline at end of file