@ohu-mobile/core
Version:
118 lines (117 loc) • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineComponent = require("../_utils/defineComponent");
var _RadioList = _interopRequireDefault(require("../RadioList"));
var _excluded = ["children"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function transformToFlattenData(option) {
var children = option.children,
flattenOption = _objectWithoutProperties(option, _excluded);
var others = {
name: option.label
};
if (option.useCollapse) {
others.children = option.children;
}
return Object.assign(flattenOption, others);
}
function getFlattenData(selectedValues, options) {
// 若value为空,默认加载一层
var flattenData = [];
var currentOptions = options;
var valueIndex = 0;
var _loop = function _loop() {
var currentValue = selectedValues[valueIndex];
flattenData.push(currentOptions.map(transformToFlattenData));
if (currentValue === undefined) {
currentOptions = null;
} else {
var result = currentOptions.find(function (option) {
return option.value === currentValue;
});
if (!(result !== null && result !== void 0 && result.useCollapse)) {
currentOptions = result === null || result === void 0 ? void 0 : result.children;
} else {
currentOptions = null;
}
}
valueIndex += 1;
};
while (currentOptions) {
_loop();
}
return flattenData;
}
var _default = exports.default = (0, _defineComponent.defineComponent)('cascader').create({
props: {
value: _defineComponent.props.ofArray().default(function () {
return [];
}),
options: (0, _defineComponent.props)(Array).default(function () {
return [];
}),
loadData: (0, _defineComponent.props)(Function).optional,
shouldLoadData: (0, _defineComponent.props)(Function).optional,
max: (0, _defineComponent.props)(Number).default(Infinity),
columns: (0, _defineComponent.props)(Number).default(2)
},
watch: {
value: function value(current) {
this.selectedValues = current;
},
selectedValues: function selectedValues(current) {
this.currentFlattenData = getFlattenData(current, this.options);
}
},
data: function data() {
return {
selectedValues: this.value,
currentFlattenData: getFlattenData(this.value, this.options)
};
},
methods: {
handleChange: function handleChange(value, index) {
if (this.selectedValues[index] === undefined) {
this.selectedValues.push(value);
} else {
this.selectedValues.splice(index + 1);
this.$set(this.selectedValues, index, value);
}
}
},
render: function render() {
var _this = this;
var h = arguments[0];
var root = this.$rootCls();
var panel = root.element('panel');
var panelStyle = {
width: "".concat(100 / this.columns, "%")
};
return h("div", {
"class": root
}, [this.currentFlattenData.map(function (options, index) {
return h("div", {
"class": panel,
"style": panelStyle
}, [h(_RadioList.default, {
"attrs": {
"name": "".concat(index),
"unCheckedIcon": null,
"value": _this.selectedValues[index],
"options": options,
"paddingDivider": false
},
"on": {
"change": function change(value) {
_this.handleChange(value, index);
}
}
})]);
})]);
}
});