ant-design-vue/components/vc-m-feedback/src/TouchFeedback.jsx

109 lines
2.7 KiB
Vue
Raw Normal View History

2020-08-31 08:53:19 +00:00
import classNames from '../../_util/classNames';
2020-06-27 14:17:48 +00:00
import { initDefaultProps, getSlot } from '../../_util/props-util';
2019-01-12 03:33:27 +00:00
import { cloneElement } from '../../_util/vnode';
import warning from '../../_util/warning';
import BaseMixin from '../../_util/BaseMixin';
import { ITouchProps } from './PropTypes';
2020-10-12 05:27:16 +00:00
import { defineComponent } from 'vue';
2018-04-04 01:44:19 +00:00
2020-10-12 05:27:16 +00:00
export default defineComponent({
2018-04-04 01:44:19 +00:00
name: 'TouchFeedback',
mixins: [BaseMixin],
2020-06-27 14:17:48 +00:00
inheritAttrs: false,
2018-04-04 01:44:19 +00:00
props: initDefaultProps(ITouchProps, {
disabled: false,
}),
2019-01-12 03:33:27 +00:00
data() {
2020-06-27 14:17:48 +00:00
this.child = null;
2018-04-04 01:44:19 +00:00
return {
active: false,
2019-01-12 03:33:27 +00:00
};
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
mounted() {
2018-04-04 01:44:19 +00:00
this.$nextTick(() => {
if (this.disabled && this.active) {
this.setState({
active: false,
2019-01-12 03:33:27 +00:00
});
2018-04-04 01:44:19 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-04-04 01:44:19 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
triggerEvent(type, isActive, ev) {
2020-06-27 14:17:48 +00:00
const eventType = `on${type}`;
const { child } = this;
if (child.props[eventType]) {
child.props[eventType](ev);
}
2018-04-04 01:44:19 +00:00
if (isActive !== this.active) {
this.setState({
active: isActive,
2019-01-12 03:33:27 +00:00
});
2018-04-04 01:44:19 +00:00
}
},
2019-01-12 03:33:27 +00:00
onTouchStart(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Touchstart', true, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchMove(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Touchmove', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchEnd(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Touchend', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchCancel(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Touchcancel', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onMouseDown(e) {
2018-04-04 01:44:19 +00:00
// pc simulate mobile
2020-06-27 14:17:48 +00:00
this.triggerEvent('Mousedown', true, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onMouseUp(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Mouseup', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onMouseLeave(e) {
2020-06-27 14:17:48 +00:00
this.triggerEvent('Mouseleave', false, e);
2018-04-04 01:44:19 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { disabled, activeClassName = '', activeStyle = {} } = this.$props;
2018-04-04 01:44:19 +00:00
2020-06-27 14:17:48 +00:00
let child = getSlot(this);
2018-04-04 01:44:19 +00:00
if (child.length !== 1) {
2019-01-12 03:33:27 +00:00
warning(false, 'm-feedback组件只能包含一个子元素');
return null;
2018-04-04 01:44:19 +00:00
}
2020-06-27 14:17:48 +00:00
const events = disabled
? undefined
: {
onTouchstart: this.onTouchStart,
onTouchmove: this.onTouchMove,
onTouchend: this.onTouchEnd,
onTouchcancel: this.onTouchCancel,
onMousedown: this.onMouseDown,
onMouseup: this.onMouseUp,
onMouseleave: this.onMouseLeave,
};
2018-04-04 01:44:19 +00:00
2020-06-27 14:17:48 +00:00
child = child[0];
this.child = child;
2018-04-04 01:44:19 +00:00
if (!disabled && this.active) {
2020-06-27 14:17:48 +00:00
let { style, class: className } = child.props;
if (activeStyle !== false) {
if (activeStyle) {
style = { ...style, ...activeStyle };
}
className = classNames(className, activeClassName);
}
return cloneElement(child, {
class: className,
style,
...events,
});
2018-04-04 01:44:19 +00:00
}
2020-06-27 14:17:48 +00:00
return cloneElement(child, events);
2018-04-04 01:44:19 +00:00
},
2020-10-12 05:27:16 +00:00
});