UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

115 lines (92 loc) 4.12 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from 'react'; import classNames from 'classnames'; import noop from '../_utils/noop'; var MultiPicker = /*#__PURE__*/function (_React$PureComponent) { _inherits(MultiPicker, _React$PureComponent); var _super = _createSuper(MultiPicker); function MultiPicker() { var _this; _classCallCheck(this, MultiPicker); _this = _super.apply(this, arguments); _this.getValue = function () { var _this$props = _this.props, children = _this$props.children, selectedValue = _this$props.selectedValue; if (selectedValue && selectedValue.length) { return selectedValue; } else { if (!children) { return []; } return React.Children.map(children, function (c) { var cc = React.Children.toArray(c.children || c.props.children); return cc && cc[0] && cc[0].props.value; }); } }; _this.onChange = function (i, v, cb) { var value = _this.getValue().concat(); value[i] = v; if (cb) { cb(value, i); } }; _this.onValueChange = function (i, v) { _this.onChange(i, v, _this.props.onValueChange); }; _this.onScrollChange = function (i, v) { _this.onChange(i, v, _this.props.onScrollChange); }; return _this; } _createClass(MultiPicker, [{ key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, prefixCls = _this$props2.prefixCls, className = _this$props2.className, rootNativeProps = _this$props2.rootNativeProps, children = _this$props2.children, style = _this$props2.style; var selectedValue = this.getValue(); var colElements = React.Children.map(children, function (col, i) { return /*#__PURE__*/React.cloneElement(col, { selectedValue: selectedValue[i], onValueChange: function onValueChange() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _this2.onValueChange.apply(_this2, [i].concat(args)); }, onScrollChange: _this2.onScrollChange && function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return _this2.onScrollChange.apply(_this2, [i].concat(args)); } }); }); return /*#__PURE__*/React.createElement("div", _extends({}, rootNativeProps, { style: style, className: classNames(className, prefixCls) }), colElements); } }]); return MultiPicker; }(React.PureComponent); MultiPicker.defaultProps = { prefixCls: 'Yep-multi-picker', style: {}, onValueChange: noop, onScrollChange: noop }; export default MultiPicker;