verce-vue-test 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/App.vue +3 -0
- package/src/components/ElInputThousandth.vue +103 -0
- package/src/router/index.ts +5 -0
- package/src/views/ThousandthView.vue +57 -0
package/package.json
CHANGED
package/src/App.vue
CHANGED
|
@@ -33,6 +33,9 @@ import { RouterView } from 'vue-router'
|
|
|
33
33
|
<el-menu-item index="/employee">
|
|
34
34
|
<span>员工管理</span>
|
|
35
35
|
</el-menu-item>
|
|
36
|
+
<el-menu-item index="/thousandth">
|
|
37
|
+
<span>千分位输入</span>
|
|
38
|
+
</el-menu-item>
|
|
36
39
|
<el-menu-item index="/about">
|
|
37
40
|
<span>关于</span>
|
|
38
41
|
</el-menu-item>
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { ref, watch, nextTick } from 'vue'
|
|
3
|
+
|
|
4
|
+
defineOptions({ name: 'ElInputThousandth' })
|
|
5
|
+
|
|
6
|
+
const props = withDefaults(
|
|
7
|
+
defineProps<{
|
|
8
|
+
modelValue?: number | string
|
|
9
|
+
placeholder?: string
|
|
10
|
+
disabled?: boolean
|
|
11
|
+
}>(),
|
|
12
|
+
{
|
|
13
|
+
modelValue: undefined,
|
|
14
|
+
placeholder: '',
|
|
15
|
+
disabled: false,
|
|
16
|
+
},
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
const emit = defineEmits<{
|
|
20
|
+
'update:modelValue': [value: number | string | undefined]
|
|
21
|
+
}>()
|
|
22
|
+
|
|
23
|
+
const isFocused = ref(false)
|
|
24
|
+
const displayValue = ref('')
|
|
25
|
+
const inputRef = ref<InstanceType<typeof import('element-plus')['ElInput']>>()
|
|
26
|
+
|
|
27
|
+
function formatThousandth(val: string | number | undefined): string {
|
|
28
|
+
if (val === undefined || val === null || val === '') return ''
|
|
29
|
+
const str = String(val)
|
|
30
|
+
const num = Number(str)
|
|
31
|
+
if (isNaN(num)) return ''
|
|
32
|
+
const parts = str.split('.')
|
|
33
|
+
const intPart = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
34
|
+
if (parts.length > 1) {
|
|
35
|
+
return intPart + '.' + parts[1]
|
|
36
|
+
}
|
|
37
|
+
return intPart
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function stripNonNumeric(val: string): string {
|
|
41
|
+
let result = val.replace(/[^\d.]/g, '')
|
|
42
|
+
const dotIndex = result.indexOf('.')
|
|
43
|
+
if (dotIndex !== -1) {
|
|
44
|
+
result = result.slice(0, dotIndex + 1) + result.slice(dotIndex + 1).replace(/\./g, '')
|
|
45
|
+
}
|
|
46
|
+
if (dotIndex !== -1) {
|
|
47
|
+
const parts = result.split('.')
|
|
48
|
+
if (parts[1].length > 2) {
|
|
49
|
+
result = parts[0] + '.' + parts[1].slice(0, 2)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return result
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function handleInput(val: string) {
|
|
56
|
+
const cleaned = stripNonNumeric(val)
|
|
57
|
+
displayValue.value = cleaned
|
|
58
|
+
if (cleaned === '' || cleaned === '.') {
|
|
59
|
+
emit('update:modelValue', undefined)
|
|
60
|
+
} else {
|
|
61
|
+
emit('update:modelValue', Number(cleaned))
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function handleFocus() {
|
|
66
|
+
isFocused.value = true
|
|
67
|
+
const raw = props.modelValue !== undefined && props.modelValue !== null && props.modelValue !== ''
|
|
68
|
+
? String(props.modelValue)
|
|
69
|
+
: ''
|
|
70
|
+
displayValue.value = raw
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function handleBlur() {
|
|
74
|
+
isFocused.value = false
|
|
75
|
+
if (props.modelValue !== undefined && props.modelValue !== null && props.modelValue !== '') {
|
|
76
|
+
displayValue.value = formatThousandth(props.modelValue)
|
|
77
|
+
} else {
|
|
78
|
+
displayValue.value = ''
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
watch(
|
|
83
|
+
() => props.modelValue,
|
|
84
|
+
(val) => {
|
|
85
|
+
if (!isFocused.value) {
|
|
86
|
+
displayValue.value = formatThousandth(val)
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
{ immediate: true },
|
|
90
|
+
)
|
|
91
|
+
</script>
|
|
92
|
+
|
|
93
|
+
<template>
|
|
94
|
+
<el-input
|
|
95
|
+
ref="inputRef"
|
|
96
|
+
:model-value="displayValue"
|
|
97
|
+
:placeholder="placeholder"
|
|
98
|
+
:disabled="disabled"
|
|
99
|
+
@input="handleInput"
|
|
100
|
+
@focus="handleFocus"
|
|
101
|
+
@blur="handleBlur"
|
|
102
|
+
/>
|
|
103
|
+
</template>
|
package/src/router/index.ts
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<el-card shadow="hover">
|
|
3
|
+
<template #header>
|
|
4
|
+
<span>千分位输入演示</span>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<el-form :model="form" label-width="120px" style="max-width: 600px">
|
|
8
|
+
<el-form-item label="金额">
|
|
9
|
+
<ElInputThousandth v-model="form.amount" placeholder="请输入金额" />
|
|
10
|
+
</el-form-item>
|
|
11
|
+
|
|
12
|
+
<el-form-item label="价格">
|
|
13
|
+
<ElInputThousandth v-model="form.price" placeholder="请输入价格" />
|
|
14
|
+
</el-form-item>
|
|
15
|
+
|
|
16
|
+
<el-form-item label="数量">
|
|
17
|
+
<el-input-number v-model="form.quantity" :min="1" />
|
|
18
|
+
</el-form-item>
|
|
19
|
+
|
|
20
|
+
<el-form-item>
|
|
21
|
+
<el-button type="primary" @click="handleSubmit">提交</el-button>
|
|
22
|
+
<el-button @click="handleReset">重置</el-button>
|
|
23
|
+
</el-form-item>
|
|
24
|
+
</el-form>
|
|
25
|
+
|
|
26
|
+
<el-divider />
|
|
27
|
+
|
|
28
|
+
<el-descriptions title="表单数据" :column="1" border>
|
|
29
|
+
<el-descriptions-item label="金额">{{ form.amount }}</el-descriptions-item>
|
|
30
|
+
<el-descriptions-item label="价格">{{ form.price }}</el-descriptions-item>
|
|
31
|
+
<el-descriptions-item label="数量">{{ form.quantity }}</el-descriptions-item>
|
|
32
|
+
</el-descriptions>
|
|
33
|
+
</el-card>
|
|
34
|
+
</template>
|
|
35
|
+
|
|
36
|
+
<script setup lang="ts">
|
|
37
|
+
import { reactive } from 'vue'
|
|
38
|
+
import { ElMessage } from 'element-plus'
|
|
39
|
+
import ElInputThousandth from '@/components/ElInputThousandth.vue'
|
|
40
|
+
|
|
41
|
+
const form = reactive({
|
|
42
|
+
amount: undefined as number | undefined,
|
|
43
|
+
price: undefined as number | undefined,
|
|
44
|
+
quantity: 1,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const handleSubmit = () => {
|
|
48
|
+
ElMessage.success('提交成功!')
|
|
49
|
+
console.log('form data:', { ...form })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const handleReset = () => {
|
|
53
|
+
form.amount = undefined
|
|
54
|
+
form.price = undefined
|
|
55
|
+
form.quantity = 1
|
|
56
|
+
}
|
|
57
|
+
</script>
|