From f735b24a4b71176ce5c214af69b7afc99deab85f Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Thu, 3 Sep 2020 20:56:56 +0800 Subject: [PATCH] refactor(NumberKeyboard): refactor Key with composition api --- src/number-keyboard/Key.js | 101 +++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/src/number-keyboard/Key.js b/src/number-keyboard/Key.js index 34c424254..9f2b7b709 100644 --- a/src/number-keyboard/Key.js +++ b/src/number-keyboard/Key.js @@ -1,13 +1,12 @@ +import { ref } from 'vue'; import { createNamespace } from '../utils'; -import { TouchMixin } from '../mixins/touch'; -import { DeleteIcon, CollapseIcon } from './Icon'; +import { useTouch } from '../composition/use-touch'; import Loading from '../loading'; +import { DeleteIcon, CollapseIcon } from './Icon'; const [createComponent, bem] = createNamespace('key'); export default createComponent({ - mixins: [TouchMixin], - props: { type: String, text: [Number, String], @@ -19,79 +18,69 @@ export default createComponent({ emits: ['press'], - data() { - return { - active: false, + setup(props, { emit, slots }) { + const active = ref(false); + const touch = useTouch(); + + const onTouchStart = (event) => { + touch.start(event); + active.value = true; }; - }, - mounted() { - this.bindTouchEvent(this.$el); - }, - - 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; + const onTouchMove = (event) => { + touch.move(event); + if (touch.direction) { + active.value = false; } - }, + }; - onTouchEnd(event) { - if (this.active) { + const onTouchEnd = (event) => { + if (active.value) { // eliminate tap delay on safari event.preventDefault(); - this.active = false; - this.$emit('press', this.text, this.type); + active.value = false; + emit('press', props.text, props.type); } - }, + }; - genContent() { - const isExtra = this.type === 'extra'; - const isDelete = this.type === 'delete'; - const text = this.$slots.default ? this.$slots.default() : this.text; - - if (this.loading) { + const renderContent = () => { + if (props.loading) { return ; } - if (isDelete) { - return text || ; + const text = slots.default ? slots.default() : props.text; + + switch (props.type) { + case 'delete': + return text || ; + case 'extra': + return text || ; + default: + return text; } + }; - if (isExtra) { - return text || ; - } - - return text; - }, - }, - - render() { - return ( -
+ return () => ( +
- {this.genContent()} + {renderContent()}
);