mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
82 lines
1.5 KiB
JavaScript
82 lines
1.5 KiB
JavaScript
import { createNamespace } from '../utils';
|
|
import { TouchMixin } from '../mixins/touch';
|
|
import { BORDER } from '../utils/constant';
|
|
|
|
const [createComponent, bem] = createNamespace('key');
|
|
|
|
export default createComponent({
|
|
mixins: [TouchMixin],
|
|
|
|
props: {
|
|
type: String,
|
|
text: [Number, String],
|
|
theme: {
|
|
type: Array,
|
|
default: () => []
|
|
}
|
|
},
|
|
|
|
data() {
|
|
return {
|
|
active: false
|
|
};
|
|
},
|
|
|
|
computed: {
|
|
className() {
|
|
const classNames = this.theme.slice(0);
|
|
|
|
if (this.active) {
|
|
classNames.push('active');
|
|
}
|
|
|
|
if (this.type) {
|
|
classNames.push(this.type);
|
|
}
|
|
|
|
return bem(classNames);
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
onTouchStart(event) {
|
|
// compatible with Vue 2.6 event bubble bug
|
|
event.stopPropagation();
|
|
|
|
this.touchStart(event);
|
|
this.active = true;
|
|
},
|
|
|
|
onTouchMove(event) {
|
|
this.touchMove(event);
|
|
|
|
if (this.direction) {
|
|
this.active = false;
|
|
}
|
|
},
|
|
|
|
onTouchEnd() {
|
|
if (this.active) {
|
|
this.active = false;
|
|
this.$emit('press', this.text, this.type);
|
|
}
|
|
}
|
|
},
|
|
|
|
render() {
|
|
return (
|
|
<i
|
|
role="button"
|
|
tabindex="0"
|
|
class={[BORDER, this.className]}
|
|
onTouchstart={this.onTouchStart}
|
|
onTouchmove={this.onTouchMove}
|
|
onTouchend={this.onTouchEnd}
|
|
onTouchcancel={this.onTouchEnd}
|
|
>
|
|
{this.slots('default') || this.text}
|
|
</i>
|
|
);
|
|
}
|
|
});
|