UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

170 lines (147 loc) 5.72 kB
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 arrayTreeFilter from '../_utils/arrayTreeFilter'; import MultiPicker from '../picker-view/MultiPicker'; import Picker from '../picker-view/Picker'; var Cascade = /*#__PURE__*/function (_React$Component) { _inherits(Cascade, _React$Component); var _super = _createSuper(Cascade); function Cascade() { var _this; _classCallCheck(this, Cascade); _this = _super.apply(this, arguments); _this.state = { value: _this.getValue(_this.props.data, _this.props.defaultValue || _this.props.value) }; _this.onValueChange = function (value, index) { var children = arrayTreeFilter(_this.props.data, function (c, level) { return level <= index && c.value === value[level]; }); var data = children[index]; var i; for (i = index + 1; data && data.children && data.children.length && i < _this.props.cols; i++) { data = data.children[0]; value[i] = data.value; } value.length = i; if (!('value' in _this.props)) { _this.setState({ value: value }); } if (_this.props.onChange) { _this.props.onChange(value); } }; return _this; } _createClass(Cascade, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps) { this.setState({ value: this.getValue(nextProps.data, nextProps.value) }); } } }, { key: "getValue", value: function getValue(d, val) { var data = d || this.props.data; var value = val || this.props.value || this.props.defaultValue; if (!value || !value.length || value.indexOf(undefined) > -1) { value = []; for (var i = 0; i < this.props.cols; i++) { if (data && data.length) { value[i] = data[0].value; data = data[0].children; } } } return value; } }, { key: "getCols", value: function getCols() { var _this$props = this.props, data = _this$props.data, cols = _this$props.cols, pickerPrefixCls = _this$props.pickerPrefixCls, disabled = _this$props.disabled, pickerItemStyle = _this$props.pickerItemStyle, indicatorStyle = _this$props.indicatorStyle; var value = this.state.value; var childrenTree = arrayTreeFilter(data, function (c, level) { return c.value === value[level]; }).map(function (c) { return c.children; }); // in case the users data is async get when select change var needPad = cols - childrenTree.length; if (needPad > 0) { for (var i = 0; i < needPad; i++) { childrenTree.push([]); } } childrenTree.length = cols - 1; childrenTree.unshift(data); return childrenTree.map(function () { var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var level = arguments.length > 1 ? arguments[1] : undefined; return /*#__PURE__*/React.createElement(Picker, { key: level, prefixCls: pickerPrefixCls, style: { flex: 1 }, disabled: disabled, itemStyle: pickerItemStyle, indicatorStyle: indicatorStyle }, children.map(function (item) { return /*#__PURE__*/React.createElement(Picker.Item, { value: item.value, key: item.value }, item.label); })); }); } }, { key: "render", value: function render() { var _this$props2 = this.props, prefixCls = _this$props2.prefixCls, className = _this$props2.className, rootNativeProps = _this$props2.rootNativeProps, style = _this$props2.style, onScrollChange = _this$props2.onScrollChange; var cols = this.getCols(); var multiStyle = Object.assign({ flexDirection: 'row', alignItems: 'center' }, style); return /*#__PURE__*/React.createElement(MultiPicker, { style: multiStyle, prefixCls: prefixCls, className: className, selectedValue: this.state.value, rootNativeProps: rootNativeProps, onValueChange: this.onValueChange, onScrollChange: onScrollChange }, cols); } }]); return Cascade; }(React.Component); Cascade.defaultProps = { cols: 3, prefixCls: 'Yep-cascader', pickerPrefixCls: 'Yep-picker', data: [], disabled: false }; export default Cascade;