dragon-mobile-ui
Version:
UI for react.js
155 lines (126 loc) • 5.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _arrayTreeFilter = require('./array-tree-filter');
var _MultiPicker = require('./MultiPicker');
var _MultiPicker2 = _interopRequireDefault(_MultiPicker);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 Cascader = function (_Component) {
_inherits(Cascader, _Component);
function Cascader(props) {
_classCallCheck(this, Cascader);
var _this = _possibleConstructorReturn(this, (Cascader.__proto__ || Object.getPrototypeOf(Cascader)).call(this, props));
_this.state = {
value: _this.getValue(_this.props.data, _this.props.defaultValue || _this.props.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: 'onValueChange',
value: function onValueChange(value, index) {
var children = (0, _arrayTreeFilter.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 += 1) {
data = data.children[0];
value[i] = data.value;
}
value.length = i;
if (!('value' in this.props)) {
this.setState({
value: value
});
}
this.props.onChange(value);
}
}, {
key: 'getCols',
value: function getCols() {
var _props = this.props,
data = _props.data,
cols = _props.cols;
var value = this.state.value;
var childrenTree = (0, _arrayTreeFilter.arrayTreeFilter)(data, function (c, level) {
return c.value === value[level];
}).map(function (c) {
return c.children;
});
childrenTree.length = cols - 1;
childrenTree.unshift(data);
return childrenTree.map(function (children) {
return {
props: {
children: children || []
}
};
});
}
}, {
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 = [];
for (var i = 0; i < this.props.cols; i += 1) {
if (data && data.length) {
value[i] = data[0].value;
data = data[0].children;
}
}
}
return value;
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = this.props;
var prefixCls = props.prefixCls,
pickerPrefixCls = props.pickerPrefixCls,
className = props.className,
pickerItemStyle = props.pickerItemStyle;
return _react2.default.createElement(
_MultiPicker2.default,
{
prefixCls: prefixCls,
pickerPrefixCls: pickerPrefixCls,
className: className,
selectedValue: this.state.value,
pickerItemStyle: pickerItemStyle,
onValueChange: function onValueChange(value, index) {
return _this2.onValueChange(value, index);
} },
this.getCols()
);
}
}]);
return Cascader;
}(_react.Component);
// 判断类型要加
Cascader.defaultProps = {
cols: 3,
prefixCls: 'ui-multi-picker',
pickerPrefixCls: 'ui-cascaderpicker',
data: [],
disabled: false
};
exports.default = Cascader;