1
- import { computed , reactive , toRefs , watch , ref , inject } from 'vue' ;
1
+ import { computed , reactive , toRefs , watch , ref , inject , InjectionKey } from 'vue' ;
2
2
import type { SetupContext , Ref , CSSProperties } from 'vue' ;
3
3
import { InputNumberProps , UseEvent , UseRender , IState , UseExpose } from './input-number-types' ;
4
4
import { useNamespace } from '../../shared/hooks/use-namespace' ;
5
5
import { isNumber , isUndefined } from '../../shared/utils' ;
6
- import { FORM_TOKEN } from '../../form' ;
6
+ import { FORM_TOKEN , type FormProps } from '../../form' ;
7
7
8
8
const ns = useNamespace ( 'input-number' ) ;
9
9
10
10
export function useRender ( props : InputNumberProps , ctx : SetupContext ) : UseRender {
11
- const formContext = inject ( FORM_TOKEN , undefined ) ;
11
+ const formContext : FormProps | undefined | any = inject ( FORM_TOKEN , undefined ) ; // 修复ts语法错误组件不被d-from组件引用时,formContext未被定义
12
12
const { style, class : customClass , ...otherAttrs } = ctx . attrs ;
13
13
const customStyle = { style : style as CSSProperties } ;
14
-
15
14
const inputNumberSize = computed ( ( ) => props . size || formContext ?. size || 'md' ) ;
16
15
17
16
const wrapClass = computed ( ( ) => [
@@ -56,12 +55,12 @@ export function useExpose(ctx: SetupContext): UseExpose {
56
55
return { inputRef } ;
57
56
}
58
57
59
- function getPrecision ( pre : number | undefined ) : number {
58
+ function getPrecision ( pre : string | number | undefined | null ) : number {
60
59
let precision = 0 ;
61
60
if ( isUndefined ( pre ) ) {
62
61
return precision ;
63
62
}
64
- const preString = pre . toString ( ) ;
63
+ const preString = ( pre as string ) . toString ( ) ;
65
64
const dotIndex = preString . indexOf ( '.' ) ;
66
65
if ( dotIndex !== - 1 ) {
67
66
precision = preString . length - dotIndex - 1 ;
@@ -89,8 +88,8 @@ export function useEvent(props: InputNumberProps, ctx: SetupContext, inputRef: R
89
88
return state . userInputValue ;
90
89
}
91
90
let currentValue = state . currentValue ;
92
- if ( currentValue === '' || isUndefined ( currentValue ) || Number . isNaN ( currentValue ) ) {
93
- return '' ;
91
+ if ( ! currentValue && currentValue !== 0 ) {
92
+ return null ;
94
93
}
95
94
if ( isNumber ( currentValue ) ) {
96
95
// todo 小数精度 确认是否应该以正则处理
@@ -111,17 +110,16 @@ export function useEvent(props: InputNumberProps, ctx: SetupContext, inputRef: R
111
110
} ;
112
111
113
112
const correctValue = ( value : number | string | undefined | null ) => {
113
+ if ( ( ! value && value !== 0 ) && props . allowEmpty ) { // 当用户开始允许空值时 value不为0的false全返回null(即'',null,undefined,NaN都会反回null设计与dev_ui_ag版本一致)
114
+ return null ;
115
+ }
114
116
// 校验正则
115
117
const valueStr = value + '' ;
116
118
if ( props . reg && ! valueStr . match ( new RegExp ( props . reg ) ) ) {
117
119
return undefined ;
118
120
}
119
121
120
122
let newVal = Number ( value ) ;
121
- // 不是0 是假值或者是NaN返回undefined
122
- if ( newVal !== 0 && ( ! Number ( value ) || Number . isNaN ( newVal ) ) ) {
123
- return undefined ;
124
- }
125
123
126
124
// 精度限制存在才做转换
127
125
if ( ! isUndefined ( props . precision ) ) {
@@ -135,14 +133,14 @@ export function useEvent(props: InputNumberProps, ctx: SetupContext, inputRef: R
135
133
return newVal ;
136
134
} ;
137
135
138
- const setCurrentValue = ( value : number | string | undefined ) => {
136
+ const setCurrentValue = ( value : number | string | undefined | null ) => {
139
137
const oldVal = state . currentValue ;
140
138
const newVal = correctValue ( value ) ;
141
139
142
140
state . userInputValue = undefined ;
143
141
144
- // 0 可以被更新
145
- if ( newVal !== 0 && ! newVal ) {
142
+ // 0 和 '' 可以被更新
143
+ if ( newVal !== 0 && newVal !== null && ! newVal ) {
146
144
ctx . emit ( 'update:modelValue' , oldVal ) ;
147
145
return ;
148
146
}
0 commit comments