ant-design-vue/components/_util/BaseMixin.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

import { getOptionProps } from './props-util';
2020-06-18 10:51:56 +00:00
import { isOn } from './util';
2018-01-12 08:10:41 +00:00
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 || {}) };
}
}
2019-01-12 03:33:27 +00:00
Object.assign(this.$data, newState);
2020-07-20 08:29:46 +00:00
if (this._.isMounted) {
this.$forceUpdate();
}
2018-01-12 08:10:41 +00:00
this.$nextTick(() => {
2019-01-12 03:33:27 +00:00
callback && callback();
});
2018-01-12 08:10:41 +00:00
},
2019-01-12 03:33:27 +00:00
__emit() {
// 直接调用listeners底层组件不需要vueTool记录events
const args = [].slice.call(arguments, 0);
2020-06-10 10:21:16 +00:00
let eventName = args[0];
// TODO: 后续统一改成onXxxx不在运行时转提升性能
2020-06-18 10:51:56 +00:00
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));
}
2018-01-17 08:12:53 +00:00
} else {
event(...args.slice(1));
2018-01-17 08:12:53 +00:00
}
2018-01-12 08:10:41 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
};