Skip to content

Commit 0fc90dd

Browse files
feat: ✨ 修复 InputNumber 在设置为 allow-null 时被赋值为空时未触发更新的问题并支持异步更新 (#812)
1 parent 583acc2 commit 0fc90dd

5 files changed

Lines changed: 173 additions & 93 deletions

File tree

docs/component/input-number.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ function handleChange1({ value }) {
9696
}
9797
```
9898

99+
## 异步变更
100+
通过 `before-change` 可以在输入值变化前进行校验和拦截。
101+
102+
```html
103+
<wd-input-number v-model="value" :before-change="beforeChange" />
104+
```
105+
106+
```typescript
107+
import { ref } from 'vue'
108+
import { useToast } from '@/uni_modules/wot-design-uni'
109+
import type { InputNumberBeforeChange } from '@/uni_modules/wot-design-uni/components/wd-input-number/types'
110+
const { loading, close } = useToast()
111+
112+
const value = ref<number>(1)
113+
114+
const beforeChange: InputNumberBeforeChange = (value) => {
115+
loading({ msg: `正在更新到${value}...` })
116+
return new Promise((resolve) => {
117+
setTimeout(() => {
118+
close()
119+
resolve(true)
120+
}, 500)
121+
})
122+
}
123+
```
124+
125+
126+
99127
## Attributes
100128

101129
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
@@ -109,12 +137,13 @@ function handleChange1({ value }) {
109137
| disabled | 禁用 | boolean | - | false | - |
110138
| without-input | 不显示输入框 | boolean | - | false | - |
111139
| input-width | 输入框宽度 | string | - | 36px | - |
112-
| allow-null | 允许空值 | boolean | - | false | - |
140+
| allow-null | 是否允许输入的值为空,设置为 `true` 后允许传入空字符串 | boolean | - | false | - |
113141
| placeholder | 占位文本 | string | - | - | - |
114142
| disable-input | 禁用输入框 | boolean | - | false | 0.2.14 |
115143
| disable-plus | 禁用增加按钮 | boolean | - | false | 0.2.14 |
116144
| disable-minus | 禁用减少按钮 | boolean | - | false | 0.2.14 |
117145
| adjustPosition | 原生属性,键盘弹起时,是否自动上推页面 | boolean | - | true | 1.3.11 |
146+
| before-change | 输入框值改变前触发,返回 false 会阻止输入框值改变,支持返回 `Promise` | `(value: number \| string) => boolean \| Promise<boolean>` | - | - | $LOWEST_VERSION$ |
118147

119148

120149
## Events

src/pages/inputNumber/Index.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@
3333
<demo-block title="允许空值,并设置 placeholder">
3434
<wd-input-number v-model="value9" allow-null placeholder="不限" input-width="70px" @change="handleChange9" />
3535
</demo-block>
36+
<demo-block title="异步变更">
37+
<wd-input-number v-model="value11" :before-change="beforeChange" />
38+
</demo-block>
3639
</page-wraper>
3740
</template>
3841
<script lang="ts" setup>
42+
import { useToast } from '@/uni_modules/wot-design-uni'
43+
import type { InputNumberBeforeChange } from '@/uni_modules/wot-design-uni/components/wd-input-number/types'
3944
import { ref } from 'vue'
45+
const { loading, close } = useToast()
4046
4147
const value1 = ref<number>(1)
4248
const value2 = ref<number>(1)
@@ -48,6 +54,7 @@ const value7 = ref<number>(1)
4854
const value8 = ref<number>(2)
4955
const value9 = ref<string>('')
5056
const value10 = ref<number>(1)
57+
const value11 = ref<number>(1)
5158
5259
function handleChange1({ value }: any) {
5360
console.log(value)
@@ -76,6 +83,16 @@ function handleChange8({ value }: any) {
7683
function handleChange9({ value }: any) {
7784
console.log(value)
7885
}
86+
87+
const beforeChange: InputNumberBeforeChange = (value) => {
88+
loading({ msg: `正在更新到${value}...` })
89+
return new Promise((resolve) => {
90+
setTimeout(() => {
91+
close()
92+
resolve(true)
93+
}, 500)
94+
})
95+
}
7996
</script>
8097
<style lang="scss" scoped>
8198
.flex {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { isPromise } from './util'
2+
3+
function noop() {}
4+
5+
export type Interceptor = (...args: any[]) => Promise<boolean> | boolean | undefined | void
6+
7+
export function callInterceptor(
8+
interceptor: Interceptor | undefined,
9+
{
10+
args = [],
11+
done,
12+
canceled,
13+
error
14+
}: {
15+
args?: unknown[]
16+
done: () => void
17+
canceled?: () => void
18+
error?: () => void
19+
}
20+
) {
21+
if (interceptor) {
22+
// eslint-disable-next-line prefer-spread
23+
const returnVal = interceptor.apply(null, args)
24+
25+
if (isPromise(returnVal)) {
26+
returnVal
27+
.then((value) => {
28+
if (value) {
29+
done()
30+
} else if (canceled) {
31+
canceled()
32+
}
33+
})
34+
.catch(error || noop)
35+
} else if (returnVal) {
36+
done()
37+
} else if (canceled) {
38+
canceled()
39+
}
40+
} else {
41+
done()
42+
}
43+
}

src/uni_modules/wot-design-uni/components/wd-input-number/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/*
22
* @Author: weisheng
33
* @Date: 2024-03-15 20:40:34
4-
* @LastEditTime: 2024-09-18 09:49:12
4+
* @LastEditTime: 2024-12-31 00:33:21
55
* @LastEditors: weisheng
66
* @Description:
7-
* @FilePath: \wot-design-uni\src\uni_modules\wot-design-uni\components\wd-input-number\types.ts
7+
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-input-number/types.ts
88
* 记得注释
99
*/
10+
import type { PropType } from 'vue'
1011
import { baseProps, makeBooleanProp, makeNumberProp, makeNumericProp, makeRequiredProp, makeStringProp, numericProp } from '../common/props'
1112

13+
export type InputNumberBeforeChange = (value: number | string) => boolean | Promise<boolean>
14+
1215
export const inputNumberProps = {
1316
...baseProps,
1417
/**
@@ -70,5 +73,9 @@ export const inputNumberProps = {
7073
/**
7174
* 原生属性,键盘弹起时,是否自动上推页面
7275
*/
73-
adjustPosition: makeBooleanProp(true)
76+
adjustPosition: makeBooleanProp(true),
77+
/**
78+
* 输入值变化前的回调函数,返回 `false` 可阻止输入,支持返回 `Promise`
79+
*/
80+
beforeChange: Function as PropType<InputNumberBeforeChange>
7481
}

0 commit comments

Comments
 (0)