mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-23 09:52:57 +08:00
refactor(NumberKeyboard): refactor Key with composition api
This commit is contained in:
parent
ec5a759f68
commit
f735b24a4b
@ -1,13 +1,12 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { useTouch } from '../composition/use-touch';
|
||||||
import { DeleteIcon, CollapseIcon } from './Icon';
|
|
||||||
import Loading from '../loading';
|
import Loading from '../loading';
|
||||||
|
import { DeleteIcon, CollapseIcon } from './Icon';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('key');
|
const [createComponent, bem] = createNamespace('key');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [TouchMixin],
|
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
type: String,
|
type: String,
|
||||||
text: [Number, String],
|
text: [Number, String],
|
||||||
@ -19,79 +18,69 @@ export default createComponent({
|
|||||||
|
|
||||||
emits: ['press'],
|
emits: ['press'],
|
||||||
|
|
||||||
data() {
|
setup(props, { emit, slots }) {
|
||||||
return {
|
const active = ref(false);
|
||||||
active: false,
|
const touch = useTouch();
|
||||||
|
|
||||||
|
const onTouchStart = (event) => {
|
||||||
|
touch.start(event);
|
||||||
|
active.value = true;
|
||||||
};
|
};
|
||||||
},
|
|
||||||
|
|
||||||
mounted() {
|
const onTouchMove = (event) => {
|
||||||
this.bindTouchEvent(this.$el);
|
touch.move(event);
|
||||||
},
|
if (touch.direction) {
|
||||||
|
active.value = false;
|
||||||
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(event) {
|
const onTouchEnd = (event) => {
|
||||||
if (this.active) {
|
if (active.value) {
|
||||||
// eliminate tap delay on safari
|
// eliminate tap delay on safari
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this.active = false;
|
active.value = false;
|
||||||
this.$emit('press', this.text, this.type);
|
emit('press', props.text, props.type);
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
genContent() {
|
const renderContent = () => {
|
||||||
const isExtra = this.type === 'extra';
|
if (props.loading) {
|
||||||
const isDelete = this.type === 'delete';
|
|
||||||
const text = this.$slots.default ? this.$slots.default() : this.text;
|
|
||||||
|
|
||||||
if (this.loading) {
|
|
||||||
return <Loading class={bem('loading-icon')} />;
|
return <Loading class={bem('loading-icon')} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDelete) {
|
const text = slots.default ? slots.default() : props.text;
|
||||||
return text || <DeleteIcon class={bem('delete-icon')} />;
|
|
||||||
|
switch (props.type) {
|
||||||
|
case 'delete':
|
||||||
|
return text || <DeleteIcon class={bem('delete-icon')} />;
|
||||||
|
case 'extra':
|
||||||
|
return text || <CollapseIcon class={bem('collapse-icon')} />;
|
||||||
|
default:
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (isExtra) {
|
return () => (
|
||||||
return text || <CollapseIcon class={bem('collapse-icon')} />;
|
<div
|
||||||
}
|
class={bem('wrapper', { wider: props.wider })}
|
||||||
|
onTouchstart={onTouchStart}
|
||||||
return text;
|
onTouchmove={onTouchMove}
|
||||||
},
|
onTouchend={onTouchEnd}
|
||||||
},
|
onTouchcancel={onTouchEnd}
|
||||||
|
>
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<div class={bem('wrapper', { wider: this.wider })}>
|
|
||||||
<div
|
<div
|
||||||
role="button"
|
role="button"
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
class={bem([
|
class={bem([
|
||||||
this.color,
|
props.color,
|
||||||
{
|
{
|
||||||
large: this.large,
|
large: props.large,
|
||||||
active: this.active,
|
active: active.value,
|
||||||
delete: this.type === 'delete',
|
delete: props.type === 'delete',
|
||||||
},
|
},
|
||||||
])}
|
])}
|
||||||
>
|
>
|
||||||
{this.genContent()}
|
{renderContent()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user