ant-design-vue/components/_util/BaseMixin.js
2020-06-18 18:51:56 +08:00

46 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getOptionProps } from './props-util';
import { isOn } from './util';
export default {
methods: {
setState(state = {}, callback) {
let newState = typeof state === 'function' ? state(this.$data, this.$props) : state;
if (this.getDerivedStateFromProps) {
const s = this.getDerivedStateFromProps(getOptionProps(this), {
...this.$data,
...newState,
});
if (s === null) {
return;
} else {
newState = { ...newState, ...(s || {}) };
}
}
Object.assign(this.$data, newState);
this.$forceUpdate();
this.$nextTick(() => {
callback && callback();
});
},
__emit() {
// 直接调用listeners底层组件不需要vueTool记录events
const args = [].slice.call(arguments, 0);
let eventName = args[0];
// TODO: 后续统一改成onXxxx不在运行时转提升性能
eventName = isOn(eventName)
? eventName
: `on${eventName[0].toUpperCase()}${eventName.substring(1)}`;
const event = this.$props[eventName] || this.$attrs[eventName];
if (args.length && event) {
if (Array.isArray(event)) {
for (let i = 0, l = event.length; i < l; i++) {
event[i](...args.slice(1));
}
} else {
event(...args.slice(1));
}
}
},
},
};