verce-vue-test 0.0.163 → 0.0.164

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "verce-vue-test",
3
- "version": "0.0.163",
3
+ "version": "0.0.164",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
package/src/App.vue CHANGED
@@ -45,6 +45,7 @@ const searchableMenus: MenuSearchItem[] = [
45
45
  { value: '循环表格', path: '/table-pro-for' },
46
46
  { value: '员工管理', path: '/employee' },
47
47
  { value: '千分位输入', path: '/thousandth' },
48
+ { value: '金额大写输入', path: '/thousandth-chinese' },
48
49
  { value: '数字区间', path: '/number-range' },
49
50
  { value: '日期区间', path: '/date-range' },
50
51
  { value: '分行看板', path: '/position-forecast' },
@@ -153,6 +154,10 @@ function toggleCollapse() {
153
154
  <el-icon><EditPen /></el-icon>
154
155
  <span>千分位输入</span>
155
156
  </el-menu-item>
157
+ <el-menu-item index="/thousandth-chinese">
158
+ <el-icon><EditPen /></el-icon>
159
+ <span>金额大写输入</span>
160
+ </el-menu-item>
156
161
  <el-menu-item index="/number-range">
157
162
  <el-icon><DataLine /></el-icon>
158
163
  <span>数字区间</span>
@@ -1,9 +1,30 @@
1
1
  <script setup lang="ts">
2
- import { computed } from 'vue'
3
-
4
- import { formatChineseCurrency } from '@/utils/chinese-currency'
5
- import ElInputThousandth from './ElInputThousandth.vue'
2
+ import { computed, ref, watch } from 'vue'
6
3
 
4
+ /**
5
+ * 自包含的金额输入框组件:
6
+ * 1. 编辑时显示不带千分位的普通数字,便于继续输入;
7
+ * 2. 失焦时按 decimalPlaces 格式化并增加千分位;
8
+ * 3. 对外只输出普通数值,同时在输入框下方实时展示人民币大写金额。
9
+ *
10
+ * 使用文档:
11
+ * ```vue
12
+ * <ElInputThousandthChinese
13
+ * v-model="form.amount"
14
+ * :decimal-places="2"
15
+ * placeholder="请输入金额"
16
+ * />
17
+ * ```
18
+ *
19
+ * 参数说明:
20
+ * - modelValue:绑定金额,输入有效内容后向外输出 number,清空时输出 undefined;
21
+ * - decimalPlaces:输入及失焦展示的小数位数,未传入时输入最多保留 2 位小数;
22
+ * - placeholder:输入框占位提示;
23
+ * - disabled:是否禁用输入框。
24
+ *
25
+ * 使用效果:以 `1234567.89` 为例,聚焦时显示 `1234567.89`,失焦后显示
26
+ * `1,234,567.89`,下方直接显示 `壹佰贰拾叁万肆仟伍佰陆拾柒元捌角玖分`。
27
+ */
7
28
  defineOptions({
8
29
  name: 'ElInputThousandthChinese',
9
30
  inheritAttrs: false,
@@ -11,9 +32,13 @@ defineOptions({
11
32
 
12
33
  const props = withDefaults(
13
34
  defineProps<{
35
+ /** v-model 绑定值,允许使用数字或带数字内容的字符串进行回填。 */
14
36
  modelValue?: number | string
37
+ /** 输入框占位提示。 */
15
38
  placeholder?: string
39
+ /** 是否禁用输入框。 */
16
40
  disabled?: boolean
41
+ /** 允许输入并在失焦时展示的小数位数,整数范围会限制在 0~20。 */
17
42
  decimalPlaces?: number
18
43
  }>(),
19
44
  {
@@ -24,39 +49,222 @@ const props = withDefaults(
24
49
  )
25
50
 
26
51
  const emit = defineEmits<{
52
+ /** 输入有效金额时输出 number,清空或输入无效时输出 undefined。 */
27
53
  'update:modelValue': [value: number | undefined]
28
54
  }>()
29
55
 
30
- /** 绑定值始终沿用 ElInputThousandth 的普通数值输出,中文大写仅作为展示文本。 */
31
- const chineseAmount = computed(() => formatChineseCurrency(props.modelValue).replace(/^人民币/, ''))
56
+ /** 当前是否处于编辑状态;该状态决定输入框使用普通数字还是千分位文本。 */
57
+ const isFocused = ref(false)
58
+ /** 输入框实际显示的文本,与对外绑定的数值分离,避免千分位参与数据保存。 */
59
+ const displayValue = ref('')
60
+
61
+ /** 解析小数位配置;无效配置时使用调用方传入的后备值。 */
62
+ function resolveDecimalPlaces(fallback?: number) {
63
+ return typeof props.decimalPlaces === 'number' && Number.isInteger(props.decimalPlaces)
64
+ ? Math.min(20, Math.max(0, props.decimalPlaces))
65
+ : fallback
66
+ }
67
+
68
+ /**
69
+ * 将数值转换为输入框的失焦展示文本:
70
+ * - 按 decimalPlaces 补齐或舍入小数位;
71
+ * - 只给整数部分增加千分位,保留小数点和小数部分。
72
+ */
73
+ function formatThousandth(value: string | number | undefined) {
74
+ if (value === undefined || value === null || value === '') return ''
75
+
76
+ const numberValue = Number(value)
77
+ if (Number.isNaN(numberValue)) return ''
78
+
79
+ const decimalPlaces = resolveDecimalPlaces()
80
+ const valueText = decimalPlaces === undefined
81
+ ? String(value)
82
+ : numberValue.toFixed(decimalPlaces)
83
+ const [integerPart = '', decimalPart] = valueText.split('.')
84
+ const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
85
+
86
+ return decimalPart === undefined
87
+ ? formattedInteger
88
+ : `${formattedInteger}.${decimalPart}`
89
+ }
90
+
91
+ /**
92
+ * 清洗用户输入,只保留数字和第一个小数点,并截断超过配置的小数位。
93
+ * 保留诸如 `.`、`12.` 这样的编辑中间态,避免输入过程中光标体验异常。
94
+ */
95
+ function stripNonNumeric(value: string) {
96
+ let result = value.replace(/[^\d.]/g, '')
97
+ const dotIndex = result.indexOf('.')
98
+
99
+ if (dotIndex !== -1) {
100
+ result = result.slice(0, dotIndex + 1) + result.slice(dotIndex + 1).replace(/\./g, '')
101
+ const [integerPart = '', decimalPart = ''] = result.split('.')
102
+ const maxDecimalPlaces = resolveDecimalPlaces(2) ?? 2
103
+ result = `${integerPart}.${decimalPart.slice(0, maxDecimalPlaces)}`
104
+ }
32
105
 
33
- function handleModelValueUpdate(value: number | string | undefined) {
34
- if (value === undefined || value === '') {
106
+ return result
107
+ }
108
+
109
+ /** 接收输入文本,更新显示值,并将可用内容转换成普通数值向父组件派发。 */
110
+ function handleInput(value: string) {
111
+ const cleanedValue = stripNonNumeric(value)
112
+ displayValue.value = cleanedValue
113
+
114
+ if (cleanedValue === '' || cleanedValue === '.') {
35
115
  emit('update:modelValue', undefined)
36
116
  return
37
117
  }
38
118
 
39
- const numericValue = typeof value === 'number' ? value : Number(value)
119
+ const numericValue = Number(cleanedValue)
40
120
  emit('update:modelValue', Number.isFinite(numericValue) ? numericValue : undefined)
41
121
  }
122
+
123
+ /** 获取焦点时移除千分位,恢复原始普通数字文本供用户编辑。 */
124
+ function handleFocus() {
125
+ isFocused.value = true
126
+ displayValue.value = props.modelValue !== undefined
127
+ && props.modelValue !== null
128
+ && props.modelValue !== ''
129
+ ? String(props.modelValue)
130
+ : ''
131
+ }
132
+
133
+ /** 失去焦点时根据当前绑定值重新格式化千分位展示文本。 */
134
+ function handleBlur() {
135
+ isFocused.value = false
136
+ displayValue.value = props.modelValue !== undefined
137
+ && props.modelValue !== null
138
+ && props.modelValue !== ''
139
+ ? formatThousandth(props.modelValue)
140
+ : ''
141
+ }
142
+
143
+ /** 中文大写数字字符表。 */
144
+ const chineseDigits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] as const
145
+ /** 整数每四位一组对应的节位单位。 */
146
+ const chineseGroupUnits = ['', '万', '亿', '兆'] as const
147
+ /** 四位分组内部从千位到个位对应的单位。 */
148
+ const chineseWithinGroupUnits = ['仟', '佰', '拾', ''] as const
149
+
150
+ /** 将 0~9999 的四位数字分组转换为中文大写,并补齐组内必要的零。 */
151
+ function formatChineseFourDigitGroup(value: number) {
152
+ const digits = String(value).padStart(4, '0')
153
+ let result = ''
154
+ let pendingZero = false
155
+
156
+ for (let index = 0; index < digits.length; index += 1) {
157
+ const digit = Number(digits[index])
158
+ if (digit === 0) {
159
+ if (result) pendingZero = true
160
+ continue
161
+ }
162
+
163
+ if (pendingZero) result += chineseDigits[0]
164
+ result += `${chineseDigits[digit] ?? ''}${chineseWithinGroupUnits[index] ?? ''}`
165
+ pendingZero = false
166
+ }
167
+
168
+ return result
169
+ }
170
+
171
+ /** 将整数从右到左按四位分组,再拼接万、亿、兆等节位单位。 */
172
+ function formatChineseInteger(value: number) {
173
+ const groups: number[] = []
174
+ let remaining = Math.trunc(value)
175
+ while (remaining > 0) {
176
+ groups.push(remaining % 10_000)
177
+ remaining = Math.floor(remaining / 10_000)
178
+ }
179
+
180
+ let result = ''
181
+ let pendingZero = false
182
+ for (let index = groups.length - 1; index >= 0; index -= 1) {
183
+ const group = groups[index] ?? 0
184
+ if (group === 0) {
185
+ if (result) pendingZero = true
186
+ continue
187
+ }
188
+
189
+ if (result && (pendingZero || group < 1_000)) result += chineseDigits[0]
190
+ result += `${formatChineseFourDigitGroup(group)}${chineseGroupUnits[index] ?? ''}`
191
+ pendingZero = false
192
+ }
193
+
194
+ return result
195
+ }
196
+
197
+ /** 将回填值转换为非负有限金额;支持数字、普通数字字符串和带逗号字符串。 */
198
+ function toAmountNumber(value: number | string | undefined) {
199
+ if (value === undefined || value === null) return null
200
+
201
+ const normalizedValue = typeof value === 'string'
202
+ ? value.replace(/,/g, '').trim()
203
+ : value
204
+
205
+ if (normalizedValue === '') return null
206
+
207
+ const amount = typeof normalizedValue === 'number'
208
+ ? normalizedValue
209
+ : Number(normalizedValue)
210
+
211
+ return Number.isFinite(amount) && amount >= 0 ? amount : null
212
+ }
213
+
214
+ /**
215
+ * 将金额转换为人民币大写文本,精确到角和分。
216
+ * 金额不足一元时保留“零元”,没有角分时使用“整”。
217
+ */
218
+ function formatChineseCurrency(value: number | string | undefined) {
219
+ const amount = toAmountNumber(value)
220
+ if (amount === null) return ''
221
+
222
+ const roundedFen = Math.round(amount * 100)
223
+ const yuan = Math.floor(roundedFen / 100)
224
+ const jiao = Math.floor((roundedFen % 100) / 10)
225
+ const fen = roundedFen % 10
226
+ const integerPart = yuan === 0 ? '零' : formatChineseInteger(yuan)
227
+ const decimalPart = jiao === 0 && fen === 0
228
+ ? '整'
229
+ : `${jiao > 0 ? `${chineseDigits[jiao]}角` : fen > 0 ? '零' : ''}${fen > 0 ? `${chineseDigits[fen]}分` : ''}`
230
+
231
+ return `人民币${integerPart}元${decimalPart}`
232
+ }
233
+
234
+ /** 金额变化时实时同步中文大写提示;展示层去掉固定的“人民币”前缀。 */
235
+ const chineseAmount = computed(() => formatChineseCurrency(props.modelValue).replace(/^人民币/, ''))
236
+
237
+ /** 外部值变化时同步输入框展示;用户聚焦编辑期间不覆盖正在输入的文本。 */
238
+ watch(
239
+ () => props.modelValue,
240
+ (value) => {
241
+ if (!isFocused.value) {
242
+ displayValue.value = formatThousandth(value)
243
+ }
244
+ },
245
+ { immediate: true },
246
+ )
42
247
  </script>
43
248
 
44
249
  <template>
45
250
  <div class="el-input-thousandth-chinese">
46
- <ElInputThousandth
251
+ <!-- 输入区域:显示文本与对外保存的数值分离。 -->
252
+ <el-input
47
253
  v-bind="$attrs"
48
- :model-value="modelValue"
254
+ :model-value="displayValue"
49
255
  :placeholder="placeholder"
50
256
  :disabled="disabled"
51
- :decimal-places="decimalPlaces"
52
- @update:model-value="handleModelValueUpdate"
257
+ @input="handleInput"
258
+ @focus="handleFocus"
259
+ @blur="handleBlur"
53
260
  />
261
+ <!-- 大写提示只在存在有效金额时显示,并通过 aria-live 通知辅助技术更新。 -->
54
262
  <span
55
263
  v-if="chineseAmount"
56
264
  class="el-input-thousandth-chinese__hint"
57
265
  aria-live="polite"
58
266
  >
59
- 大写金额:{{ chineseAmount }}
267
+ {{ chineseAmount }}
60
268
  </span>
61
269
  </div>
62
270
  </template>
@@ -61,6 +61,11 @@ const router = createRouter({
61
61
  name: 'thousandth',
62
62
  component: () => import('../views/ThousandthView.vue'),
63
63
  },
64
+ {
65
+ path: '/thousandth-chinese',
66
+ name: 'thousandthChinese',
67
+ component: () => import('../views/ThousandthChineseView.vue'),
68
+ },
64
69
  {
65
70
  path: '/number-range',
66
71
  name: 'numberRange',