UNPKG

5.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
18
19function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
20
21function _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; }
22
23function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
25function _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; }
26
27function _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) : _defaults(subClass, superClass); }
28
29var propTypes = {
30 clsfix: _propTypes2["default"].string,
31 onChange: _propTypes2["default"].func,
32 value: _propTypes2["default"].string
33};
34var defaultProps = {
35 clsfix: 'u-search-tabs',
36 onChange: function onChange() {}
37};
38
39var SearchTabs = function (_Component) {
40 _inherits(SearchTabs, _Component);
41
42 function SearchTabs(props) {
43 _classCallCheck(this, SearchTabs);
44
45 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
46
47 _this.getChildren = function () {
48 var _this$props = _this.props,
49 children = _this$props.children,
50 clsfix = _this$props.clsfix;
51
52 var childs = [];
53 if (children.length > 1) {
54 _react2["default"].Children.map(children, function (child, index) {
55 childs.push(_react2["default"].cloneElement(child, {
56 tabClick: _this.childClick,
57 active: child.props.value == _this.state.activeValue,
58 clsfix: clsfix
59 }));
60 if (index < children.length - 1) {
61 childs.push(_react2["default"].createElement(
62 'span',
63 { className: clsfix + '-split', key: 'split-' + index },
64 '|'
65 ));
66 }
67 });
68 } else {
69 childs.push(_react2["default"].cloneElement(children, {
70 tabClick: _this.childClick,
71 active: children.props.value == _this.state.activeValue,
72 clsfix: clsfix
73 }));
74 }
75 return childs;
76 };
77
78 _this.childClick = function (activeValue) {
79 _this.setState({
80 activeValue: activeValue
81 });
82 _this.props.onChange(activeValue);
83 };
84
85 _this.state = {
86 activeValue: props.value
87 };
88 return _this;
89 }
90
91 SearchTabs.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
92 if ('value' in nextProps && nextProps.value != this.state.activeValue) {
93 this.setState({
94 activeValue: nextProps.value
95 });
96 }
97 };
98
99 SearchTabs.prototype.render = function render() {
100 var _props = this.props,
101 clsfix = _props.clsfix,
102 children = _props.children,
103 onChange = _props.onChange,
104 value = _props.value,
105 others = _objectWithoutProperties(_props, ['clsfix', 'children', 'onChange', 'value']);
106
107 return _react2["default"].createElement(
108 'div',
109 _extends({ className: '' + clsfix }, others),
110 this.getChildren()
111 );
112 };
113
114 return SearchTabs;
115}(_react.Component);
116
117;
118SearchTabs.propTypes = propTypes;
119SearchTabs.defaultProps = defaultProps;
120exports["default"] = SearchTabs;
121module.exports = exports['default'];
\No newline at end of file