@sheinx/hooks 3.9.11-beta.3 → 3.9.11-beta.5
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/cjs/components/use-input/use-input-number.d.ts.map +1 -1
- package/cjs/components/use-input/use-input-number.js +33 -5
- package/cjs/components/use-input/use-input-number.type.d.ts +5 -0
- package/cjs/components/use-input/use-input-number.type.d.ts.map +1 -1
- package/esm/components/use-input/use-input-number.d.ts.map +1 -1
- package/esm/components/use-input/use-input-number.js +33 -5
- package/esm/components/use-input/use-input-number.type.d.ts +5 -0
- package/esm/components/use-input/use-input-number.type.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-input-number.d.ts","sourceRoot":"","sources":["use-input-number.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAK3D,QAAA,MAAM,eAAe,UAAW,gBAAgB;;;;;;;;
|
|
1
|
+
{"version":3,"file":"use-input-number.d.ts","sourceRoot":"","sources":["use-input-number.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAK3D,QAAA,MAAM,eAAe,UAAW,gBAAgB;;;;;;;;CAuL/C,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -38,7 +38,8 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
38
38
|
step = _props$step === void 0 ? 1 : _props$step,
|
|
39
39
|
cancelBlurChange = props.cancelBlurChange,
|
|
40
40
|
disabled = props.disabled,
|
|
41
|
-
coin = props.coin
|
|
41
|
+
coin = props.coin,
|
|
42
|
+
defaultValue = props.defaultValue;
|
|
42
43
|
var getStringValue = function getStringValue(value) {
|
|
43
44
|
if (value === undefined) return value;
|
|
44
45
|
if (typeof value === 'number') {
|
|
@@ -51,8 +52,10 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
51
52
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
52
53
|
inernalInputValue = _React$useState2[0],
|
|
53
54
|
setInternalInputValue = _React$useState2[1];
|
|
55
|
+
var focusedRef = _react.default.useRef(false);
|
|
54
56
|
(0, _react.useEffect)(function () {
|
|
55
|
-
|
|
57
|
+
// 聚焦编辑期间不同步外部值,避免 form 回填 defaultValue 覆盖用户输入
|
|
58
|
+
if (props.value !== inernalInputValue && !focusedRef.current) {
|
|
56
59
|
setInternalInputValue(getStringValue(props.value));
|
|
57
60
|
}
|
|
58
61
|
}, [props.value]);
|
|
@@ -79,8 +82,10 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
79
82
|
if (val.endsWith('.') || val.includes('.') && val.endsWith('0')) return;
|
|
80
83
|
var num = parseFloat(val);
|
|
81
84
|
if (val === '') {
|
|
82
|
-
//
|
|
83
|
-
|
|
85
|
+
// 聚焦编辑期间清空发送 null 而非 undefined
|
|
86
|
+
// 因为 form 层的 defaultValue 回填仅检查 value === undefined
|
|
87
|
+
// 发送 null 可以让 form 数据跟随更新,同时避免触发回填
|
|
88
|
+
onChange === null || onChange === void 0 || onChange(focusedRef.current ? null : allowNull ? null : undefined);
|
|
84
89
|
return;
|
|
85
90
|
}
|
|
86
91
|
if (isNaN(num)) return;
|
|
@@ -91,9 +96,27 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
91
96
|
;
|
|
92
97
|
});
|
|
93
98
|
var onNumberBlur = (0, _usePersistFn.default)(function (e) {
|
|
99
|
+
focusedRef.current = false;
|
|
94
100
|
var target = e.target;
|
|
95
101
|
var newValue = target.value;
|
|
96
102
|
|
|
103
|
+
// 输入为空且有 defaultValue 时,失焦恢复为 defaultValue
|
|
104
|
+
if (newValue === '' && defaultValue != null) {
|
|
105
|
+
var _num = typeof defaultValue === 'number' ? defaultValue : parseFloat(String(defaultValue));
|
|
106
|
+
if (isNaN(_num)) _num = 0;
|
|
107
|
+
if (typeof digits === 'number') {
|
|
108
|
+
_num = parseFloat(_num.toFixed(digits));
|
|
109
|
+
}
|
|
110
|
+
_num = commonFormat(_num);
|
|
111
|
+
setInternalInputValue(getStringValue(_num));
|
|
112
|
+
if (_num !== value) {
|
|
113
|
+
target.value = String(_num);
|
|
114
|
+
if (!cancelBlurChange) onInnerChange(_num);
|
|
115
|
+
}
|
|
116
|
+
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
97
120
|
// 没有输入值
|
|
98
121
|
if (newValue === '' && value === undefined) {
|
|
99
122
|
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
@@ -124,6 +147,11 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
124
147
|
}
|
|
125
148
|
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
126
149
|
});
|
|
150
|
+
var onNumberFocus = (0, _usePersistFn.default)(function (e) {
|
|
151
|
+
var _props$onFocus;
|
|
152
|
+
focusedRef.current = true;
|
|
153
|
+
(_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props, e);
|
|
154
|
+
});
|
|
127
155
|
var onNumberChange = (0, _usePersistFn.default)(function (value) {
|
|
128
156
|
var result = value;
|
|
129
157
|
onInnerChange(result);
|
|
@@ -160,7 +188,7 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
160
188
|
coin: coin,
|
|
161
189
|
onChange: onNumberChange,
|
|
162
190
|
onBlur: onNumberBlur,
|
|
163
|
-
onFocus:
|
|
191
|
+
onFocus: onNumberFocus,
|
|
164
192
|
cancelBlurChange: true
|
|
165
193
|
})), {}, {
|
|
166
194
|
onPlus: handlePlus,
|
|
@@ -3,6 +3,11 @@ export type NumberValue = string | number | undefined | null;
|
|
|
3
3
|
export interface InputNumberProps extends Omit<InputFormatProps, 'value' | 'onChange' | 'autoFix' | 'trim' | 'type'> {
|
|
4
4
|
value: NumberValue;
|
|
5
5
|
onChange: (value: NumberValue) => void | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* @en default value, restore on blur when input is empty
|
|
8
|
+
* @cn 默认值,输入为空失焦时恢复
|
|
9
|
+
*/
|
|
10
|
+
defaultValue?: NumberValue;
|
|
6
11
|
/**
|
|
7
12
|
* @en Minimum value
|
|
8
13
|
* @cn 最小值
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-input-number.type.d.ts","sourceRoot":"","sources":["use-input-number.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;AAC7D,MAAM,WAAW,gBACf,SAAQ,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClF,KAAK,EAAE,WAAW,CAAC;IACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,GAAG,SAAS,CAAC;IACnD;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
1
|
+
{"version":3,"file":"use-input-number.type.d.ts","sourceRoot":"","sources":["use-input-number.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;AAC7D,MAAM,WAAW,gBACf,SAAQ,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClF,KAAK,EAAE,WAAW,CAAC;IACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,GAAG,SAAS,CAAC;IACnD;;;OAGG;IACH,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-input-number.d.ts","sourceRoot":"","sources":["use-input-number.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAK3D,QAAA,MAAM,eAAe,UAAW,gBAAgB;;;;;;;;
|
|
1
|
+
{"version":3,"file":"use-input-number.d.ts","sourceRoot":"","sources":["use-input-number.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAK3D,QAAA,MAAM,eAAe,UAAW,gBAAgB;;;;;;;;CAuL/C,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -29,7 +29,8 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
29
29
|
step = _props$step === void 0 ? 1 : _props$step,
|
|
30
30
|
cancelBlurChange = props.cancelBlurChange,
|
|
31
31
|
disabled = props.disabled,
|
|
32
|
-
coin = props.coin
|
|
32
|
+
coin = props.coin,
|
|
33
|
+
defaultValue = props.defaultValue;
|
|
33
34
|
var getStringValue = function getStringValue(value) {
|
|
34
35
|
if (value === undefined) return value;
|
|
35
36
|
if (typeof value === 'number') {
|
|
@@ -42,8 +43,10 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
42
43
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
43
44
|
inernalInputValue = _React$useState2[0],
|
|
44
45
|
setInternalInputValue = _React$useState2[1];
|
|
46
|
+
var focusedRef = React.useRef(false);
|
|
45
47
|
useEffect(function () {
|
|
46
|
-
|
|
48
|
+
// 聚焦编辑期间不同步外部值,避免 form 回填 defaultValue 覆盖用户输入
|
|
49
|
+
if (props.value !== inernalInputValue && !focusedRef.current) {
|
|
47
50
|
setInternalInputValue(getStringValue(props.value));
|
|
48
51
|
}
|
|
49
52
|
}, [props.value]);
|
|
@@ -70,8 +73,10 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
70
73
|
if (val.endsWith('.') || val.includes('.') && val.endsWith('0')) return;
|
|
71
74
|
var num = parseFloat(val);
|
|
72
75
|
if (val === '') {
|
|
73
|
-
//
|
|
74
|
-
|
|
76
|
+
// 聚焦编辑期间清空发送 null 而非 undefined
|
|
77
|
+
// 因为 form 层的 defaultValue 回填仅检查 value === undefined
|
|
78
|
+
// 发送 null 可以让 form 数据跟随更新,同时避免触发回填
|
|
79
|
+
onChange === null || onChange === void 0 || onChange(focusedRef.current ? null : allowNull ? null : undefined);
|
|
75
80
|
return;
|
|
76
81
|
}
|
|
77
82
|
if (isNaN(num)) return;
|
|
@@ -82,9 +87,27 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
82
87
|
;
|
|
83
88
|
});
|
|
84
89
|
var onNumberBlur = usePersistFn(function (e) {
|
|
90
|
+
focusedRef.current = false;
|
|
85
91
|
var target = e.target;
|
|
86
92
|
var newValue = target.value;
|
|
87
93
|
|
|
94
|
+
// 输入为空且有 defaultValue 时,失焦恢复为 defaultValue
|
|
95
|
+
if (newValue === '' && defaultValue != null) {
|
|
96
|
+
var _num = typeof defaultValue === 'number' ? defaultValue : parseFloat(String(defaultValue));
|
|
97
|
+
if (isNaN(_num)) _num = 0;
|
|
98
|
+
if (typeof digits === 'number') {
|
|
99
|
+
_num = parseFloat(_num.toFixed(digits));
|
|
100
|
+
}
|
|
101
|
+
_num = commonFormat(_num);
|
|
102
|
+
setInternalInputValue(getStringValue(_num));
|
|
103
|
+
if (_num !== value) {
|
|
104
|
+
target.value = String(_num);
|
|
105
|
+
if (!cancelBlurChange) onInnerChange(_num);
|
|
106
|
+
}
|
|
107
|
+
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
88
111
|
// 没有输入值
|
|
89
112
|
if (newValue === '' && value === undefined) {
|
|
90
113
|
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
@@ -115,6 +138,11 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
115
138
|
}
|
|
116
139
|
onBlur === null || onBlur === void 0 || onBlur(e);
|
|
117
140
|
});
|
|
141
|
+
var onNumberFocus = usePersistFn(function (e) {
|
|
142
|
+
var _props$onFocus;
|
|
143
|
+
focusedRef.current = true;
|
|
144
|
+
(_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props, e);
|
|
145
|
+
});
|
|
118
146
|
var onNumberChange = usePersistFn(function (value) {
|
|
119
147
|
var result = value;
|
|
120
148
|
onInnerChange(result);
|
|
@@ -151,7 +179,7 @@ var useNumberFormat = function useNumberFormat(props) {
|
|
|
151
179
|
coin: coin,
|
|
152
180
|
onChange: onNumberChange,
|
|
153
181
|
onBlur: onNumberBlur,
|
|
154
|
-
onFocus:
|
|
182
|
+
onFocus: onNumberFocus,
|
|
155
183
|
cancelBlurChange: true
|
|
156
184
|
})), {}, {
|
|
157
185
|
onPlus: handlePlus,
|
|
@@ -3,6 +3,11 @@ export type NumberValue = string | number | undefined | null;
|
|
|
3
3
|
export interface InputNumberProps extends Omit<InputFormatProps, 'value' | 'onChange' | 'autoFix' | 'trim' | 'type'> {
|
|
4
4
|
value: NumberValue;
|
|
5
5
|
onChange: (value: NumberValue) => void | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* @en default value, restore on blur when input is empty
|
|
8
|
+
* @cn 默认值,输入为空失焦时恢复
|
|
9
|
+
*/
|
|
10
|
+
defaultValue?: NumberValue;
|
|
6
11
|
/**
|
|
7
12
|
* @en Minimum value
|
|
8
13
|
* @cn 最小值
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-input-number.type.d.ts","sourceRoot":"","sources":["use-input-number.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;AAC7D,MAAM,WAAW,gBACf,SAAQ,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClF,KAAK,EAAE,WAAW,CAAC;IACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,GAAG,SAAS,CAAC;IACnD;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
1
|
+
{"version":3,"file":"use-input-number.type.d.ts","sourceRoot":"","sources":["use-input-number.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;AAC7D,MAAM,WAAW,gBACf,SAAQ,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;IAClF,KAAK,EAAE,WAAW,CAAC;IACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,GAAG,SAAS,CAAC;IACnD;;;OAGG;IACH,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|