yylib-quick-mobile
Version:
yylib-quick-mobile
154 lines (121 loc) • 6.58 kB
JavaScript
var _segmentedControl = require('antd-mobile/lib/segmented-control');
var _segmentedControl2 = _interopRequireDefault(_segmentedControl);
var _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; };
var _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; }; }();
require('antd-mobile/lib/segmented-control/style/css');
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
require('./YYSegmentedControls.less');
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _YYSegmentedControl = require('./YYSegmentedControl');
var _YYSegmentedControl2 = _interopRequireDefault(_YYSegmentedControl);
var _FunctionUtil = require('../../utils/FunctionUtil');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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; }
var YYSegmentedControls = function (_React$Component) {
_inherits(YYSegmentedControls, _React$Component);
function YYSegmentedControls(props) {
_classCallCheck(this, YYSegmentedControls);
var _this2 = _possibleConstructorReturn(this, (YYSegmentedControls.__proto__ || Object.getPrototypeOf(YYSegmentedControls)).call(this, props));
_this2.onChange = function (e) {
var _this = _this2;
_this2.setState({ selectSegment: e.nativeEvent.selectedSegmentIndex }, function () {
if ((0, _FunctionUtil.isFunction)(_this.props.onChange)) {
_this2.props.onChange(_this.state.selectSegment);
}
});
};
_this2.onValueChange = function (value) {
if ((0, _FunctionUtil.isFunction)(_this2.props.onValueChange)) {
_this2.props.onValueChange(value);
}
};
_this2.state = {
selectSegment: 0
};
return _this2;
}
_createClass(YYSegmentedControls, [{
key: 'getSegment',
value: function getSegment() {
var children = this.props.children;
if (children) {
Array.prototype.slice.call(children);
return children.map(function (item, index) {
return item.props.uititle;
});
}
return [];
}
}, {
key: 'getChildren',
value: function getChildren() {
var children = this.props.children;
children = children && children.constructor == Array ? children : [children];
var selectSegment = this.state.selectSegment;
if (children && children.length) {
Array.prototype.slice.call(children);
return children.map(function (child, index) {
var _child$props = child.props,
uititle = _child$props.uititle,
badge = _child$props.badge,
icon = _child$props.icon,
selectedIcon = _child$props.selectedIcon,
iconColor = _child$props.iconColor,
selectedIconColor = _child$props.selectedIconColor,
dot = _child$props.dot,
selected = _child$props.selected,
nid = _child$props.nid,
restProps = _objectWithoutProperties(_child$props, ['uititle', 'badge', 'icon', 'selectedIcon', 'iconColor', 'selectedIconColor', 'dot', 'selected', 'nid']);
var SegmentedControls = [];
if (selectSegment === index) {
SegmentedControls.push(_react2.default.createElement(_YYSegmentedControl2.default, {
key: index,
children: child.props.children }));
} else {
SegmentedControls.push(_react2.default.createElement(_YYSegmentedControl2.default, { className: 'hidden',
key: index,
children: child.props.children }));
}
return SegmentedControls;
});
}
return [];
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
tintColor = _props.tintColor,
children = _props.children,
visible = _props.visible,
restProps = _objectWithoutProperties(_props, ['tintColor', 'children', 'visible']);
var segments = this.getSegment();
var selectSegment = this.state.selectSegment;
var wrapClz = (0, _classnames2.default)('yy-segmented-control', !visible && 'hidden', this.props.className);
return _react2.default.createElement(
'div',
null,
_react2.default.createElement(_segmentedControl2.default, _extends({}, restProps, {
className: wrapClz,
values: segments,
tintColor: tintColor,
onChange: this.onChange,
onValueChange: this.onValueChange,
selectedIndex: selectSegment
})),
this.getChildren()
);
}
}]);
return YYSegmentedControls;
}(_react2.default.Component);
YYSegmentedControls.defaultProps = {
visible: true
};
module.exports = YYSegmentedControls;
;