-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathFuInputRwSwitch.vue
More file actions
92 lines (78 loc) · 2.05 KB
/
FuInputRwSwitch.vue
File metadata and controls
92 lines (78 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<div class="fu-input-rw-switch">
<div
v-if="!isWrite"
class="fu-input-rw-switch__read"
@click.stop="handleReadClick"
@dblclick.stop="handleReadDblClick"
>
<span class="fu-input-rw-switch__text">{{ displayValue }}</span>
</div>
<el-input
v-else
ref="inputRef"
v-bind="$attrs"
:model-value="modelValue"
@update:model-value="handleUpdate"
@input="handleInput"
@blur="handleBlur"
@keyup.enter="handleEnter"
@click.stop
/>
</div>
</template>
<script setup lang="ts">
import { computed, nextTick, ref } from 'vue';
defineOptions({ name: 'FuInputRwSwitch', inheritAttrs: false });
const props = defineProps({
modelValue: {
type: [String, Number],
default: '',
},
writeTrigger: {
type: String,
default: 'onClick',
},
});
const emit = defineEmits(['update:modelValue', 'input', 'blur', 'enter']);
const inputRef = ref();
const isWrite = ref(false);
const displayValue = computed(() => {
return props.modelValue === '' || props.modelValue === undefined || props.modelValue === null
? '-'
: String(props.modelValue);
});
const openWrite = async () => {
isWrite.value = true;
await nextTick();
inputRef.value?.focus?.();
inputRef.value?.select?.();
};
const closeWrite = () => {
isWrite.value = false;
};
const handleReadClick = () => {
if (props.writeTrigger === 'onClick') {
openWrite();
}
};
const handleReadDblClick = () => {
if (props.writeTrigger === 'onDblclick') {
openWrite();
}
};
const handleUpdate = (value: string | number) => {
emit('update:modelValue', value);
};
const handleInput = (value: string) => {
emit('input', value);
};
const handleBlur = (event: FocusEvent) => {
emit('blur', event);
closeWrite();
};
const handleEnter = (event: KeyboardEvent) => {
emit('enter', event);
closeWrite();
};
</script>