Skip to content

Commit fab2cac

Browse files
authored
fix(numeric): add parse-input prop (#4205)
* fix(numeric): add parse-input prop * fix: 调整一下逻辑 * fix: 删除多余文件
1 parent fff845b commit fab2cac

8 files changed

Lines changed: 84 additions & 6 deletions

File tree

examples/sites/demos/apis/numeric.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,22 @@ export default {
409409
mode: ['pc', 'mobile-first'],
410410
pcDemo: 'string-mode',
411411
mfDemo: ''
412+
},
413+
{
414+
name: 'parse-input',
415+
type: '(value:string)=>string',
416+
defaultValue: '',
417+
desc: {
418+
'zh-CN': '自定义输入值解析函数,优先级较高。当输入字符以及粘贴时,会调用该函数进行解析。',
419+
'en-US':
420+
'custom input value parsing function with high priority.This function is called for parsing when input characters are entered or pasted'
421+
},
422+
mode: ['pc', 'mobile-first'],
423+
pcDemo: 'parse-input',
424+
mfDemo: '',
425+
meta: {
426+
stable: '3.31.0'
427+
}
412428
}
413429
],
414430
events: [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<tiny-numeric v-model="value" placeholder="请输入非空数值" :parse-input="parseInput"></tiny-numeric>
3+
</template>
4+
5+
<script setup lang="ts">
6+
import { ref } from 'vue'
7+
import { TinyNumeric } from '@opentiny/vue'
8+
9+
const value = ref(1)
10+
const parseInput = (value: string) => {
11+
return value.replaceAll(',', '').replaceAll('_', '')
12+
}
13+
</script>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<tiny-numeric v-model="value" placeholder="请输入非空数值" :parse-input="parseInput"></tiny-numeric>
3+
</template>
4+
5+
<script lang="ts">
6+
import { TinyNumeric } from '@opentiny/vue'
7+
8+
export default {
9+
components: {
10+
TinyNumeric
11+
},
12+
data() {
13+
return {
14+
value: 1
15+
}
16+
},
17+
methods: {
18+
parseInput(value: string) {
19+
// 允许输入千分符, _ 分隔符等,比如 1,000.00 或者 1_000.00
20+
return value.replaceAll(',', '').replaceAll('_', '')
21+
}
22+
}
23+
}
24+
</script>

examples/sites/demos/pc/app/numeric/webdoc/numeric.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ export default {
6969
},
7070
codeFiles: ['dynamic-disabled.vue']
7171
},
72+
{
73+
demoId: 'parse-input',
74+
name: {
75+
'zh-CN': '自定义输入值解析',
76+
'en-US': 'Custom Input Value Parsing'
77+
},
78+
desc: {
79+
'zh-CN': '自定义输入值解析函数,优先级较高。当输入字符以及粘贴时,会调用该函数进行解析。',
80+
'en-US':
81+
'Custom input value parsing function with high priority. This function is called for parsing when input characters are entered or pasted'
82+
},
83+
codeFiles: ['parse-input.vue'],
84+
meta: {
85+
stable: '3.31.0'
86+
}
87+
},
88+
7289
{
7390
demoId: 'allow-empty',
7491
name: {

packages/renderless/src/numeric/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export const setCurrentValue =
313313
}
314314
}
315315

316-
/** 处理输入字符: input / compositionend 事件均进入该函数 */
316+
/** 处理输入字符: input / compositionend , paste 事件均进入该函数 */
317317
export const handleInput =
318318
({ state, api, emit, props }: Pick<INumericRenderlessParams, 'state' | 'api' | 'emit' | 'props'>) =>
319319
(event: InputEvent): void => {
@@ -322,15 +322,18 @@ export const handleInput =
322322
return
323323
}
324324

325-
// 此时为输入了1个有效的: 数字\英文\中文
325+
// 此时为输入了1个有效的: 数字\英文\中文, 或者paste进来
326+
let value = event.target.value.replace(/^-+/, '-')
327+
328+
if (props.parseInput) {
329+
value = props.parseInput(value)
330+
}
326331
const { fraction } = state.format
327332
const emitError = () => {
328333
if (state.pasting) {
329334
emit('paste-error', event.target.value)
330335
}
331336
}
332-
let value = event.target.value.replace(/^-+/, '-')
333-
334337
if (value !== '-' && api.getDecimal(value).isNaN()) {
335338
emitError()
336339

packages/vue/src/numeric/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ export const numericProps = {
157157
changeCompat: {
158158
type: Boolean,
159159
default: false
160+
},
161+
parseInput: {
162+
type: Function
160163
}
161164
}
162165

packages/vue/src/numeric/src/mobile-first.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ export default defineComponent({
177177
'hideUnit',
178178
'unitCenter',
179179
'displayOnly',
180-
'changeCompat'
180+
'changeCompat',
181+
'parseInput'
181182
],
182183
emits: ['update:modelValue', 'change', 'blur', 'focus'],
183184
setup(props, context): any {

packages/vue/src/numeric/src/pc.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ export default defineComponent({
276276
'clearable',
277277
'filter',
278278
'blank',
279-
'changeCompat'
279+
'changeCompat',
280+
'parseInput'
280281
],
281282
emits: ['update:modelValue', 'change', 'blur', 'focus', 'paste-error', 'filter-change', 'clear'],
282283
setup(props, context) {

0 commit comments

Comments
 (0)