verce-vue-test 0.0.5 → 0.0.7
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/888/1 +121 -0
- 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/888/1
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<el-dialog
|
|
3
|
+
v-model="dialogVisible"
|
|
4
|
+
:title="isEdit ? '编辑用户' : '新增用户'"
|
|
5
|
+
width="520px"
|
|
6
|
+
destroy-on-close
|
|
7
|
+
@closed="resetForm"
|
|
8
|
+
>
|
|
9
|
+
<el-form ref="formRef" :model="form" :rules="rules" label-width="90px">
|
|
10
|
+
<el-form-item label="用户名" prop="username">
|
|
11
|
+
<el-input v-model="form.username" />
|
|
12
|
+
</el-form-item>
|
|
13
|
+
|
|
14
|
+
<el-form-item label="手机号" prop="phone">
|
|
15
|
+
<el-input v-model="form.phone" />
|
|
16
|
+
</el-form-item>
|
|
17
|
+
|
|
18
|
+
<el-form-item label="状态" prop="status">
|
|
19
|
+
<el-radio-group v-model="form.status">
|
|
20
|
+
<el-radio :value="1">启用</el-radio>
|
|
21
|
+
<el-radio :value="0">禁用</el-radio>
|
|
22
|
+
</el-radio-group>
|
|
23
|
+
</el-form-item>
|
|
24
|
+
</el-form>
|
|
25
|
+
|
|
26
|
+
<template #footer>
|
|
27
|
+
<el-button @click="dialogVisible = false">取消</el-button>
|
|
28
|
+
<el-button type="primary" :loading="loading" @click="handleSubmit">
|
|
29
|
+
保存
|
|
30
|
+
</el-button>
|
|
31
|
+
</template>
|
|
32
|
+
</el-dialog>
|
|
33
|
+
</template>
|
|
34
|
+
|
|
35
|
+
<script setup>
|
|
36
|
+
import { computed, reactive, ref, watch } from 'vue'
|
|
37
|
+
import { ElMessage } from 'element-plus'
|
|
38
|
+
|
|
39
|
+
const props = defineProps({
|
|
40
|
+
modelValue: {
|
|
41
|
+
type: Boolean,
|
|
42
|
+
default: false
|
|
43
|
+
},
|
|
44
|
+
mode: {
|
|
45
|
+
type: String,
|
|
46
|
+
default: 'create'
|
|
47
|
+
},
|
|
48
|
+
rowData: {
|
|
49
|
+
type: Object,
|
|
50
|
+
default: () => null
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const emit = defineEmits(['update:modelValue', 'success'])
|
|
55
|
+
|
|
56
|
+
const dialogVisible = computed({
|
|
57
|
+
get: () => props.modelValue,
|
|
58
|
+
set: value => emit('update:modelValue', value)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const isEdit = computed(() => props.mode === 'edit')
|
|
62
|
+
|
|
63
|
+
const formRef = ref()
|
|
64
|
+
const loading = ref(false)
|
|
65
|
+
|
|
66
|
+
const defaultForm = {
|
|
67
|
+
id: null,
|
|
68
|
+
username: '',
|
|
69
|
+
phone: '',
|
|
70
|
+
status: 1
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const form = reactive({ ...defaultForm })
|
|
74
|
+
|
|
75
|
+
const rules = {
|
|
76
|
+
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
|
|
77
|
+
phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
watch(
|
|
81
|
+
() => props.modelValue,
|
|
82
|
+
visible => {
|
|
83
|
+
if (!visible) return
|
|
84
|
+
|
|
85
|
+
Object.assign(form, defaultForm)
|
|
86
|
+
|
|
87
|
+
if (props.mode === 'edit' && props.rowData) {
|
|
88
|
+
Object.assign(form, props.rowData)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
formRef.value?.clearValidate()
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
const resetForm = () => {
|
|
96
|
+
Object.assign(form, defaultForm)
|
|
97
|
+
formRef.value?.clearValidate()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const handleSubmit = async () => {
|
|
101
|
+
await formRef.value.validate()
|
|
102
|
+
|
|
103
|
+
loading.value = true
|
|
104
|
+
try {
|
|
105
|
+
if (isEdit.value) {
|
|
106
|
+
// await updateUserApi(form.id, form)
|
|
107
|
+
console.log('编辑:', form)
|
|
108
|
+
ElMessage.success('修改成功')
|
|
109
|
+
} else {
|
|
110
|
+
// await createUserApi(form)
|
|
111
|
+
console.log('新增:', form)
|
|
112
|
+
ElMessage.success('新增成功')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
dialogVisible.value = false
|
|
116
|
+
emit('success')
|
|
117
|
+
} finally {
|
|
118
|
+
loading.value = false
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
</script>
|
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>
|