UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

200 lines (168 loc) 6.63 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; } } var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import Picker from '../picker-view/Picker'; import MultiPicker from '../picker-view/MultiPicker'; import noop from '../_utils/noop'; import treeFilter from '../_utils/arrayTreeFilter'; import Cascade from './Cascade'; import PickerPopup from './PickerPopup'; var PopupPicker = /*#__PURE__*/function (_React$PureComponent) { _inherits(PopupPicker, _React$PureComponent); var _super = _createSuper(PopupPicker); function PopupPicker() { var _this; _classCallCheck(this, PopupPicker); _this = _super.apply(this, arguments); _this.getSel = function (v) { var value = _this.scrollValue || v || []; console.log('---' + value); var treeChildren; var data = _this.props.data; if (_this.props.cascade) { treeChildren = treeFilter(data, function (c, level) { return c.value === value[level]; }); } else { // @ts-ignore treeChildren = value.map(function (v, i) { return data[i].filter(function (d) { return d.value === v; })[0]; }); } return _this.props.format ? _this.props.format(treeChildren.map(function (v) { return v.label; })) : treeChildren.map(function (v) { return v.label; }); }; _this.getPickerCol = function () { var _this$props = _this.props, data = _this$props.data, pickerPrefixCls = _this$props.pickerPrefixCls, itemStyle = _this$props.itemStyle, indicatorStyle = _this$props.indicatorStyle; return data.map(function (col, index) { return /*#__PURE__*/React.createElement(Picker, { key: index, prefixCls: pickerPrefixCls, style: { flex: 1 }, itemStyle: itemStyle, indicatorStyle: indicatorStyle }, col.map(function (item) { return /*#__PURE__*/React.createElement(Picker.Item, { key: item.value, value: item.value }, item.label); })); }); }; _this.onOk = function (v) { if (_this.scrollValue !== undefined) { v = _this.scrollValue; } if (_this.props.onChange) { _this.props.onChange(v); } if (_this.props.onLabelChange) { _this.props.onLabelChange(_this.getSel(v)); } if (_this.props.onOk) { _this.props.onOk(v); } }; _this.setScrollValue = function (v) { _this.scrollValue = v; }; _this.setCascadeScrollValue = function (v) { // 级联情况下保证数据正确性,滚动过程中只有当最后一级变化时才变更数据 if (v && _this.scrollValue) { var length = _this.scrollValue.length; if (length === v.length && _this.scrollValue[length - 1] === v[length - 1]) { return; } } _this.setScrollValue(v); }; _this.onPickerChange = function (v) { _this.setScrollValue(v); if (_this.props.onPickerChange) { _this.props.onPickerChange(v); } }; _this.onVisibleChange = function (visible) { _this.setScrollValue(undefined); if (_this.props.onVisibleChange) { _this.props.onVisibleChange(visible); } }; return _this; } _createClass(PopupPicker, [{ key: "render", value: function render() { var _a = this.props, show = _a.show, onCancel = _a.onCancel, pickerPrefixCls = _a.pickerPrefixCls, prefixCls = _a.prefixCls, cascade = _a.cascade, data = _a.data, cols = _a.cols, itemStyle = _a.itemStyle, indicatorStyle = _a.indicatorStyle, defaultSelectedValue = _a.defaultSelectedValue, restProps = __rest(_a, ["show", "onCancel", "pickerPrefixCls", "prefixCls", "cascade", "data", "cols", "itemStyle", "indicatorStyle", "defaultSelectedValue"]); var picker = cascade ? /*#__PURE__*/React.createElement(Cascade, { prefixCls: prefixCls, pickerPrefixCls: pickerPrefixCls, data: data, cols: cols, onChange: this.onPickerChange, onScrollChange: this.setCascadeScrollValue, pickerItemStyle: itemStyle, indicatorStyle: indicatorStyle, defaultValue: defaultSelectedValue }) : /*#__PURE__*/React.createElement(MultiPicker, _extends({}, restProps, { selectedValue: defaultSelectedValue, prefixCls: pickerPrefixCls, onScrollChange: this.setScrollValue }), this.getPickerCol()); return /*#__PURE__*/React.createElement(PickerPopup, { onCancel: onCancel, show: show, onOk: this.onOk, picker: picker }); } }]); return PopupPicker; }(React.PureComponent); export { PopupPicker as default }; PopupPicker.defaultProps = { show: false, onCancel: noop, prefixCls: 'Yep-picker', pickerPrefixCls: 'Yep-picker-col', data: [], cascade: true };