PeriodInput.vue
11.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<template>
<view>
<!-- 标签 -->
<view v-if="label" class="text-sm text-gray-600 mb-2 flex items-center">
<text v-if="required" class="text-red-500 mr-1">*</text>
<text>{{ label }}</text>
</view>
<!-- 触发区域 -->
<view
class="flex justify-between items-center border border-gray-200 rounded-lg p-3 bg-gray-50"
@tap="openInput"
>
<text :class="displayValue ? 'text-gray-900' : 'text-gray-400'" class="text-sm">
{{ displayValue || placeholder }}
</text>
<IconFont name="right" size="14" color="#9CA3AF" />
</view>
<!-- 自定义输入弹窗 -->
<nut-popup
v-model:visible="showInputModal"
position="bottom"
:overlay="true"
:close-on-click-overlay="false"
:style="{ padding: '0', borderRadius: '24rpx 24rpx 0 0', overflow: 'hidden', height: '40%' }"
:overlay-style="{ backgroundColor: 'rgba(0, 0, 0, 0.5)' }"
>
<view class="flex flex-col bg-white">
<!-- 标题栏 -->
<view class="flex justify-between items-center p-4 border-b border-gray-100">
<text class="text-base font-semibold text-gray-900">{{ inputLabel || '请输入提取期' }}</text>
<view class="w-8 h-8 flex items-center justify-center rounded-full bg-gray-100" @tap="onCancel">
<text class="text-xl text-gray-500 leading-none">×</text>
</view>
</view>
<!-- 输入区域 -->
<view class="p-4">
<!-- 数字输入 + 单位 -->
<view class="flex items-center border border-gray-300 rounded-lg px-4 py-3 mb-3 bg-gray-50">
<input
v-model="inputValue"
class="flex-1 text-base text-gray-900 bg-transparent border-none outline-none"
type="digit"
:placeholder="inputPlaceholder"
@input="onInputChange"
/>
<text class="text-base text-gray-500 ml-2">年</text>
</view>
<!-- 验证提示 -->
<view class="mb-4 min-h-[20rpx]" :class="isValid && inputValue ? 'text-green-500' : 'text-gray-500'">
<text v-if="!inputValue" class="text-xs">{{ validationHint }}</text>
<text v-else-if="isValid" class="text-xs">✓ 格式正确</text>
<text v-else class="text-xs text-red-500">✗ {{ validationError }}</text>
</view>
<!-- 快捷选项 -->
<view class="flex flex-wrap items-center gap-2">
<text class="text-sm text-gray-500">快捷选项:</text>
<view
v-for="option in quickOptions"
:key="option"
class="px-4 py-2 rounded-lg border text-sm transition-colors"
:class="inputValue === option ? 'bg-blue-50 border-blue-500 text-blue-600' : 'bg-white border-gray-300 text-gray-700'"
@tap="selectQuickOption(option)"
>
<text>{{ option }}</text>
</view>
</view>
</view>
<!-- 操作按钮 -->
<view class="flex gap-3 p-4 border-t border-gray-100">
<view
class="flex-1 h-11 flex items-center justify-center rounded-lg text-base bg-gray-100 text-gray-700"
@tap="onCancel"
>
<text>取消</text>
</view>
<view
class="flex-1 h-11 flex items-center justify-center rounded-lg text-base transition-colors"
:class="canConfirm ? 'bg-blue-500 text-white' : 'bg-gray-200 text-gray-400'"
@tap="onConfirm"
>
<text>确定</text>
</view>
</view>
</view>
</nut-popup>
</view>
</template>
<script setup>
/**
* 提取期自定义输入组件
*
* @description 用于输入自定义的提取期,支持整数年期(1-100年)和快捷选项
* - 整数约束:只接受整数年期(1-100)
* - 快捷选项:终身、一笔过
* - 实时验证:输入时即时反馈格式是否正确
* - 可扩展:预留 customValidators 接口支持未来格式扩展
*
* @module components/plan/PlanFields/PeriodInput
* @author Claude Code
* @version 1.0.0
*
* @example
* <PeriodInput
* v-model:visible="showPeriodInput"
* v-model="periodValue"
* label="提取期"
* inputLabel="请输入提取期"
* :validation-rules="{ min: 1, max: 100 }"
* @confirm="handlePeriodConfirm"
* />
*/
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import IconFont from '@/components/icons/IconFont.vue'
import { useGlobalPopup } from './GlobalPopupManager.js'
/**
* 组件属性
*/
const props = defineProps({
/**
* 弹窗显示状态(v-model:visible)
* @type {boolean}
*/
visible: {
type: Boolean,
default: false
},
/**
* 绑定的值(v-model)
* @type {string}
*/
modelValue: {
type: String,
default: ''
},
/**
* 标签文本
* @type {string}
*/
label: {
type: String,
default: ''
},
/**
* 是否必填
* @type {boolean}
*/
required: {
type: Boolean,
default: false
},
/**
* 占位符文本
* @type {string}
*/
placeholder: {
type: String,
default: '请选择或输入提取期'
},
/**
* 弹窗内输入提示文本
* @type {string}
*/
inputLabel: {
type: String,
default: '请输入提取期'
},
/**
* 输入框占位符
* @type {string}
*/
inputPlaceholder: {
type: String,
default: '请输入年数'
},
/**
* 验证规则
* @type {Object}
* @property {number} min - 最小年期(默认1)
* @property {number} max - 最大年期(默认100)
* @property {string[]} allowed_formats - 允许的非年期格式(如['终身', '一笔过'])
* @property {Function[]} custom_validators - 自定义验证函数数组(预留扩展)
*/
validationRules: {
type: Object,
default: () => ({
min: 1,
max: 100,
allowed_formats: ['终身', '一笔过'],
custom_validators: []
})
}
})
/**
* 组件事件
*/
const emit = defineEmits([
/**
* 更新弹窗显示状态(v-model:visible)
* @event update:visible
* @param {boolean} value - 弹窗显示状态
*/
'update:visible',
/**
* 更新绑定值(v-model)
* @event update:modelValue
* @param {string} value - 选中的提取期值
*/
'update:modelValue',
/**
* 确认输入事件
* @event confirm
* @param {string} value - 确认的提取期值
*/
'confirm',
/**
* 取消输入事件
* @event cancel
*/
'cancel'
])
/**
* 使用全局弹窗管理器
* @description 当弹窗打开时,通知父弹窗隐藏底部按钮
*/
const { registerPopup, activatePopup, deactivatePopup } = useGlobalPopup()
/**
* 输入弹窗 ID
* @type {Ref<string|null>}
*/
const inputPopupId = ref(null)
/**
* 组件挂载时注册弹窗
*/
onMounted(() => {
inputPopupId.value = registerPopup()
})
/**
* 组件卸载时取消注册
*/
onUnmounted(() => {
if (inputPopupId.value) {
deactivatePopup(inputPopupId.value)
}
})
/**
* 弹窗显示状态
*/
const showInputModal = ref(false)
/**
* 输入框的值(原始输入)
*/
const inputValue = ref('')
/**
* 当前验证状态
*/
const isValid = ref(false)
/**
* 验证错误信息
*/
const validationError = ref('')
/**
* 验证提示信息(无输入时显示)
*/
const validationHint = ref('')
/**
* 快捷选项列表
*/
const quickOptions = computed(() => {
return props.validationRules?.allowed_formats || ['终身', '一笔过']
})
/**
* 显示的值
*/
const displayValue = computed(() => {
return props.modelValue || ''
})
/**
* 是否可以确认
*/
const canConfirm = computed(() => {
return isValid.value && inputValue.value
})
/**
* 监听 visible prop 变化
*/
watch(() => props.visible, (newVal) => {
showInputModal.value = newVal
if (newVal) {
// 打开弹窗时,初始化输入值
initInputValue()
}
})
/**
* 监听弹窗状态变化,同步到父组件
*/
watch(showInputModal, (newVal) => {
if (newVal) {
// 弹窗打开:激活全局弹窗,通知父弹窗隐藏 footer
if (inputPopupId.value) {
activatePopup(inputPopupId.value)
}
} else {
// 弹窗关闭:停用全局弹窗,通知父弹窗恢复 footer
if (inputPopupId.value) {
deactivatePopup(inputPopupId.value)
}
emit('update:visible', false)
}
})
/**
* 监听输入值变化,触发验证
*/
watch(inputValue, () => {
// 每次输入值变化都触发验证(防止 @input 事件丢失)
validateInput()
})
/**
* 初始化输入值
*/
const initInputValue = () => {
if (props.modelValue) {
// 如果是快捷选项,直接使用
if (quickOptions.value.includes(props.modelValue)) {
inputValue.value = props.modelValue
} else {
// 如果是年期格式,提取数字
const match = props.modelValue.match(/^(\d+)年$/)
if (match) {
inputValue.value = match[1] // 只存储数字
} else {
inputValue.value = props.modelValue
}
}
} else {
inputValue.value = ''
}
validateInput()
}
/**
* 打开输入弹窗
*/
const openInput = () => {
emit('update:visible', true)
}
/**
* 输入变化处理
*/
const onInputChange = () => {
validateInput()
}
/**
* 选择快捷选项
*/
const selectQuickOption = (option) => {
inputValue.value = option
validateInput()
}
/**
* 验证输入
* @returns {boolean} 是否有效
*/
const validateInput = () => {
const rules = props.validationRules || {}
const value = inputValue.value.trim()
// 空值
if (!value) {
isValid.value = false
validationError.value = ''
validationHint.value = `请输入${rules.min || 1}-${rules.max || 100}之间的整数,或选择快捷选项`
return false
}
// 快捷选项(终身、一笔过等)
if (quickOptions.value.includes(value)) {
isValid.value = true
validationError.value = ''
validationHint.value = ''
return true
}
// 数字年期验证
const num = Number(value)
// 检查是否为有效数字
if (Number.isNaN(num)) {
isValid.value = false
validationError.value = '请输入数字'
validationHint.value = ''
return false
}
// 检查是否为整数(核心要求)
if (!Number.isInteger(num)) {
isValid.value = false
validationError.value = '年期必须是整数'
validationHint.value = ''
return false
}
// 检查范围
const min = rules.min ?? 1
const max = rules.max ?? 100
if (num < min) {
isValid.value = false
validationError.value = `年期不能小于${min}年`
validationHint.value = ''
return false
}
if (num > max) {
isValid.value = false
validationError.value = `年期不能大于${max}年`
validationHint.value = ''
return false
}
// 自定义验证器(预留扩展)
if (rules.custom_validators && rules.custom_validators.length > 0) {
for (const validator of rules.custom_validators) {
const result = validator(value)
if (result.valid === false) {
isValid.value = false
validationError.value = result.error || '格式不正确'
validationHint.value = ''
return false
}
}
}
// 验证通过
isValid.value = true
validationError.value = ''
validationHint.value = ''
return true
}
/**
* 确认输入
*/
const onConfirm = () => {
if (!canConfirm.value) return
let finalValue = inputValue.value
// 如果是纯数字,添加"年"单位
const num = Number(inputValue.value)
if (!Number.isNaN(num) && Number.isInteger(num)) {
finalValue = `${num}年`
}
emit('update:modelValue', finalValue)
emit('confirm', finalValue)
showInputModal.value = false
}
/**
* 取消输入
*/
const onCancel = () => {
inputValue.value = ''
isValid.value = false
showInputModal.value = false
emit('cancel')
}
</script>
<style lang="less" scoped>
/* 组件样式(如有需要可在此补充 TailwindCSS 无法覆盖的样式) */
</style>