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

95 lines
2.3 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { initDefaultProps } from '../../_util/props-util';
import { cloneElement } from '../../_util/vnode';
import warning from '../../_util/warning';
import BaseMixin from '../../_util/BaseMixin';
import { ITouchProps } from './PropTypes';
2018-04-04 01:44:19 +00:00
export default {
name: 'TouchFeedback',
mixins: [BaseMixin],
props: initDefaultProps(ITouchProps, {
disabled: false,
}),
2019-01-12 03:33:27 +00:00
data() {
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) {
2018-11-02 13:45:11 +00:00
// 暂时仅有input-number用到事件直接到挂载到Touchable上不需要像antd那样从子组件触发
2019-01-12 03:33:27 +00:00
this.$emit(type, 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) {
this.triggerEvent('touchstart', true, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchMove(e) {
this.triggerEvent('touchmove', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchEnd(e) {
this.triggerEvent('touchend', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onTouchCancel(e) {
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
2019-01-12 03:33:27 +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) {
this.triggerEvent('mouseup', false, e);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
onMouseLeave(e) {
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
2019-01-12 03:33:27 +00:00
const child = this.$slots.default;
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
}
let childProps = {
2019-01-12 03:33:27 +00:00
on: disabled
? {}
: {
touchstart: this.onTouchStart,
touchmove: this.onTouchMove,
touchend: this.onTouchEnd,
touchcancel: this.onTouchCancel,
mousedown: this.onMouseDown,
mouseup: this.onMouseUp,
mouseleave: this.onMouseLeave,
},
};
2018-04-04 01:44:19 +00:00
if (!disabled && this.active) {
2019-01-12 03:33:27 +00:00
childProps = {
...childProps,
...{
style: activeStyle,
class: activeClassName,
},
};
2018-04-04 01:44:19 +00:00
}
2019-01-12 03:33:27 +00:00
return cloneElement(child, childProps);
2018-04-04 01:44:19 +00:00
},
2019-01-12 03:33:27 +00:00
};