import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React from 'react'; import arrayTreeFilter from 'array-tree-filter'; import MultiPicker from 'rmc-picker/es/MultiPicker'; import Picker from 'rmc-picker/lib/Picker'; var Cascader = function (_React$Component) { _inherits(Cascader, _React$Component); function Cascader() { _classCallCheck(this, Cascader); var _this = _possibleConstructorReturn(this, (Cascader.__proto__ || Object.getPrototypeOf(Cascader)).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 = void 0; 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; _this.setState({ value: value }); if (_this.props.onChange) { _this.props.onChange(value); } }; return _this; } _createClass(Cascader, [{ 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 _props = this.props, data = _props.data, cols = _props.cols, pickerPrefixCls = _props.pickerPrefixCls, disabled = _props.disabled, pickerItemStyle = _props.pickerItemStyle, indicatorStyle = _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; }); 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[1]; return React.createElement(Picker, { key: level, prefixCls: pickerPrefixCls, style: { flex: 1 }, disabled: disabled, itemStyle: pickerItemStyle, indicatorStyle: indicatorStyle }, children.map(function (item) { return React.createElement(Picker.Item, { value: item.value, key: item.value }, item.label); })); }); } }, { key: 'render', value: function render() { var props = this.props; var prefixCls = props.prefixCls, className = props.className, rootNativeProps = props.rootNativeProps, style = props.style; var cols = this.getCols(); var multiStyle = _extends({ flexDirection: 'row', alignItems: 'center' }, style); return React.createElement(MultiPicker, { style: multiStyle, prefixCls: prefixCls, className: className, selectedValue: this.state.value, rootNativeProps: rootNativeProps, onValueChange: this.onValueChange, onScrollChange: props.onScrollChange }, cols); } }]); return Cascader; }(React.Component); Cascader.defaultProps = { cols: 3, prefixCls: 'rmc-cascader', pickerPrefixCls: 'rmc-picker', data: [], disabled: false }; export default Cascader;