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

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-10-28 07:31:45 +00:00
import { nextTick } from 'vue';
import { getOptionProps } from './props-util';
2018-01-12 08:10:41 +00:00
export default {
methods: {
2020-10-02 07:44:10 +00:00
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();
}
2020-10-28 07:31:45 +00:00
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() {
2020-07-24 06:30:52 +00:00
// 直接调用事件底层组件不需要vueTool记录events
2019-01-12 03:33:27 +00:00
const args = [].slice.call(arguments, 0);
2020-06-10 10:21:16 +00:00
let eventName = args[0];
2020-07-24 06:30:52 +00:00
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
};