@hi-ui/input 4.0.9 → 4.0.11

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.
@@ -14,50 +14,46 @@ import { setAttrStatus } from '@hi-ui/dom-utils';
14
14
  import { callAllFuncs } from '@hi-ui/func-utils';
15
15
  import { format, pure, formatAmount } from './utils/index.js';
16
16
  import { useInputCursor } from './use-input-cursor.js';
17
- var EXTRA_TYPE = ['text', 'id', 'tel', 'card', 'amount', 'email']; // 需要格式化后校对光标的类型
18
-
17
+ var EXTRA_TYPE = ['text', 'id', 'tel', 'card', 'amount', 'email'];
18
+ // 需要格式化后校对光标的类型
19
19
  var RESET_CURSOR_TYPE = ['id', 'tel', 'card'];
20
-
21
20
  var useInput = function useInput(_ref) {
22
21
  var name = _ref.name,
23
- _ref$autoFocus = _ref.autoFocus,
24
- autoFocus = _ref$autoFocus === void 0 ? false : _ref$autoFocus,
25
- _ref$disabled = _ref.disabled,
26
- disabled = _ref$disabled === void 0 ? false : _ref$disabled,
27
- _ref$readOnly = _ref.readOnly,
28
- readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
29
- maxLength = _ref.maxLength,
30
- placeholder = _ref.placeholder,
31
- _ref$defaultValue = _ref.defaultValue,
32
- defaultValue = _ref$defaultValue === void 0 ? '' : _ref$defaultValue,
33
- valueProp = _ref.value,
34
- onChange = _ref.onChange,
35
- onFocus = _ref.onFocus,
36
- onBlur = _ref.onBlur,
37
- onKeyDown = _ref.onKeyDown,
38
- _ref$trimValueOnBlur = _ref.trimValueOnBlur,
39
- trimValueOnBlur = _ref$trimValueOnBlur === void 0 ? false : _ref$trimValueOnBlur,
40
- _ref$type = _ref.type,
41
- type = _ref$type === void 0 ? 'text' : _ref$type,
42
- clearElementRef = _ref.clearElementRef,
43
- inputElementRef = _ref.inputElementRef; // Object.is 避免 trimValueOnBlur 和 点击 clearIcon 触发 2 次相同的 onCHange
44
-
22
+ _ref$autoFocus = _ref.autoFocus,
23
+ autoFocus = _ref$autoFocus === void 0 ? false : _ref$autoFocus,
24
+ _ref$disabled = _ref.disabled,
25
+ disabled = _ref$disabled === void 0 ? false : _ref$disabled,
26
+ _ref$readOnly = _ref.readOnly,
27
+ readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
28
+ maxLength = _ref.maxLength,
29
+ placeholder = _ref.placeholder,
30
+ _ref$defaultValue = _ref.defaultValue,
31
+ defaultValue = _ref$defaultValue === void 0 ? '' : _ref$defaultValue,
32
+ valueProp = _ref.value,
33
+ onChange = _ref.onChange,
34
+ onFocus = _ref.onFocus,
35
+ onBlur = _ref.onBlur,
36
+ onKeyDown = _ref.onKeyDown,
37
+ _ref$trimValueOnBlur = _ref.trimValueOnBlur,
38
+ trimValueOnBlur = _ref$trimValueOnBlur === void 0 ? false : _ref$trimValueOnBlur,
39
+ _ref$type = _ref.type,
40
+ type = _ref$type === void 0 ? 'text' : _ref$type,
41
+ clearElementRef = _ref.clearElementRef,
42
+ inputElementRef = _ref.inputElementRef;
43
+ // Object.is 避免 trimValueOnBlur 和 点击 clearIcon 触发 2 次相同的 onCHange
45
44
  var _useUncontrolledState = useUncontrolledState(defaultValue, valueProp, onChange, Object.is),
46
- value = _useUncontrolledState[0],
47
- tryChangeValue = _useUncontrolledState[1];
48
-
45
+ value = _useUncontrolledState[0],
46
+ tryChangeValue = _useUncontrolledState[1];
49
47
  var _useInputCursor = useInputCursor({
50
- inputElementRef: inputElementRef,
51
- value: format(value, type)
52
- }),
53
- formatHandleChange = _useInputCursor.handleChange,
54
- handleOnKeyDown = _useInputCursor.handleOnKeyDown,
55
- position = _useInputCursor.position;
48
+ inputElementRef: inputElementRef,
49
+ value: format(value, type)
50
+ }),
51
+ formatHandleChange = _useInputCursor.handleChange,
52
+ handleOnKeyDown = _useInputCursor.handleOnKeyDown,
53
+ position = _useInputCursor.position;
56
54
  /**
57
55
  * 修复值格式化时光标位置问题:https://github.com/XiaoMi/hiui/issues/2438
58
56
  */
59
-
60
-
61
57
  var needResetCursorPosition = useMemo(function () {
62
58
  return RESET_CURSOR_TYPE.includes(type);
63
59
  }, [type]);
@@ -65,18 +61,15 @@ var useInput = function useInput(_ref) {
65
61
  if (trim === void 0) {
66
62
  trim = false;
67
63
  }
68
-
69
64
  evt.persist();
70
65
  var nextValue = evt.target.value;
71
- var valueTrue = pure(nextValue, type); // 防溢出,保证 onChange 拿到的是值是最新的 formatted value
72
-
66
+ var valueTrue = pure(nextValue, type);
67
+ // 防溢出,保证 onChange 拿到的是值是最新的 formatted value
73
68
  var value = format(nextValue, type);
74
-
75
69
  if (trim) {
76
70
  valueTrue = valueTrue.trim();
77
71
  value = value.trim();
78
72
  }
79
-
80
73
  var event = Object.create(evt);
81
74
  event.target = Object.assign(Object.assign({}, evt.target), {
82
75
  value: value
@@ -84,31 +77,27 @@ var useInput = function useInput(_ref) {
84
77
  tryChangeValue(valueTrue, event);
85
78
  needResetCursorPosition && formatHandleChange(event);
86
79
  }, [formatHandleChange, needResetCursorPosition, tryChangeValue, type]);
87
-
88
80
  var _useState = useState(autoFocus),
89
- focused = _useState[0],
90
- setFocused = _useState[1];
91
-
81
+ focused = _useState[0],
82
+ setFocused = _useState[1];
92
83
  var handleFocus = useLatestCallback(function (evt) {
93
84
  setFocused(true);
94
85
  onFocus === null || onFocus === void 0 ? void 0 : onFocus(evt);
95
86
  });
96
87
  var handleBlur = useLatestCallback(function (event) {
97
- var relatedTarget = event.relatedTarget; // 拦截 clearIcon 点击清空,阻止其触发 input 失焦
98
-
88
+ var relatedTarget = event.relatedTarget;
89
+ // 拦截 clearIcon 点击清空,阻止其触发 input 失焦
99
90
  if (clearElementRef && clearElementRef.current && clearElementRef.current === relatedTarget) {
100
91
  return;
101
92
  }
102
-
103
- setFocused(false); // amount 自动添加小数
104
-
93
+ setFocused(false);
94
+ // amount 自动添加小数
105
95
  if (type === 'amount') {
106
96
  event.target.value = formatAmount(value, true);
107
97
  handleChange(event, trimValueOnBlur);
108
98
  } else if (trimValueOnBlur) {
109
99
  handleChange(event, true);
110
100
  }
111
-
112
101
  onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
113
102
  });
114
103
  var nativeInputProps = useMemo(function () {
@@ -147,5 +136,4 @@ var useInput = function useInput(_ref) {
147
136
  getInputProps: getInputProps
148
137
  };
149
138
  };
150
-
151
139
  export { useInput };
@@ -7,18 +7,15 @@
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
-
11
10
  /**
12
11
  * 格式化身份证
13
12
  */
14
13
  var formatId = function formatId(val) {
15
14
  val = val.replace(/[^a-zA-Z0-9]/g, '');
16
15
  var len = val.length;
17
-
18
16
  if (val === '' || len < 7) {
19
17
  return val;
20
18
  }
21
-
22
19
  if (len < 11) {
23
20
  return val.slice(0, 6) + ' ' + val.slice(6, 10);
24
21
  } else if (len < 15) {
@@ -27,7 +24,6 @@ var formatId = function formatId(val) {
27
24
  return val.slice(0, 6) + ' ' + val.slice(6, 10) + ' ' + val.slice(10, 14) + ' ' + val.slice(14, 18);
28
25
  }
29
26
  };
30
-
31
27
  var pureId = function pureId(val) {
32
28
  var tmp = val.replace(/[^\d|.]/g, '');
33
29
  return tmp.slice(0, 18);
@@ -35,23 +31,18 @@ var pureId = function pureId(val) {
35
31
  /**
36
32
  * 格式化手机号
37
33
  */
38
-
39
-
40
34
  var formatTel = function formatTel(val) {
41
35
  val = val.replace(/\D/g, '');
42
36
  var len = val.length;
43
-
44
37
  if (val === '' || len < 4) {
45
38
  return val;
46
39
  }
47
-
48
40
  if (len < 8) {
49
41
  return val.slice(0, 3) + ' ' + val.slice(3, 7);
50
42
  } else {
51
43
  return val.slice(0, 3) + ' ' + val.slice(3, 7) + ' ' + val.slice(7, 11);
52
44
  }
53
45
  };
54
-
55
46
  var pureTel = function pureTel(val) {
56
47
  var tmp = val.replace(/[^\d|.]/g, '');
57
48
  return tmp.slice(0, 11);
@@ -59,16 +50,12 @@ var pureTel = function pureTel(val) {
59
50
  /**
60
51
  * 格式化银行卡号
61
52
  */
62
-
63
-
64
53
  var formatCard = function formatCard(val) {
65
54
  val = val.replace(/\D/g, '');
66
55
  var len = val.length;
67
-
68
56
  if (val === '' || val.length < 5) {
69
57
  return val;
70
58
  }
71
-
72
59
  if (len < 9) {
73
60
  return val.slice(0, 4) + ' ' + val.slice(4, 8);
74
61
  } else if (len < 13) {
@@ -79,7 +66,6 @@ var formatCard = function formatCard(val) {
79
66
  return val.slice(0, 4) + ' ' + val.slice(4, 8) + ' ' + val.slice(8, 12) + ' ' + val.slice(12, 16) + ' ' + val.slice(16, 19);
80
67
  }
81
68
  };
82
-
83
69
  var pureCard = function pureCard(val) {
84
70
  var tmp = val.replace(/[^\d|.]/g, '');
85
71
  return tmp.slice(0, 19);
@@ -88,59 +74,44 @@ var pureCard = function pureCard(val) {
88
74
  * 金额自动生成小数
89
75
  * @param {string} val 需要处理的值
90
76
  */
91
-
92
-
93
77
  var formatAmount = function formatAmount(val, fill) {
94
78
  if (fill === void 0) {
95
79
  fill = false;
96
80
  }
97
-
98
81
  val = val.replace(/[^\d|.|,]/g, '').replace(/(\.\d*?)(\.|,).*/, function (_, $1) {
99
82
  return $1;
100
83
  });
101
-
102
84
  if (fill) {
103
85
  if (!val) return val;
104
86
  return val.indexOf('.') > -1 ? val : val + '.00';
105
87
  }
106
-
107
88
  return val;
108
89
  };
109
-
110
90
  var pureAmount = function pureAmount(val) {
111
91
  var tmp = val.replace(/[^\d|.]/g, '');
112
92
  return tmp;
113
93
  };
114
-
115
94
  var formatEmail = function formatEmail(val) {
116
95
  return val.replace(/\W/g, '');
117
96
  };
118
-
119
97
  var pureEmail = function pureEmail(val) {
120
98
  return val;
121
99
  };
122
100
  /**
123
101
  * 输入规则
124
102
  */
125
-
126
-
127
103
  var format = function format(val, type) {
128
104
  switch (type) {
129
105
  case 'id':
130
106
  return formatId(val);
131
-
132
107
  case 'tel':
133
108
  return formatTel(val);
134
-
135
109
  case 'card':
136
110
  return formatCard(val);
137
-
138
111
  case 'email':
139
112
  return formatEmail(val);
140
-
141
113
  case 'amount':
142
114
  return formatAmount(val);
143
-
144
115
  default:
145
116
  return val;
146
117
  }
@@ -148,28 +119,20 @@ var format = function format(val, type) {
148
119
  /**
149
120
  * 净化规则
150
121
  */
151
-
152
-
153
122
  var pure = function pure(val, type) {
154
123
  switch (type) {
155
124
  case 'id':
156
125
  return pureId(val);
157
-
158
126
  case 'tel':
159
127
  return pureTel(val);
160
-
161
128
  case 'card':
162
129
  return pureCard(val);
163
-
164
130
  case 'email':
165
131
  return pureEmail(val);
166
-
167
132
  case 'amount':
168
133
  return pureAmount(val);
169
-
170
134
  default:
171
135
  return val;
172
136
  }
173
137
  };
174
-
175
138
  export { format, formatAmount, formatCard, formatEmail, formatId, formatTel, pure, pureAmount, pureCard, pureEmail, pureId, pureTel };
@@ -3,13 +3,6 @@ import { HiBaseHTMLFieldProps } from '@hi-ui/core';
3
3
  import { InputAppearanceEnum, InputTypeEnum } from './types';
4
4
  /**
5
5
  * 输入框
6
- *
7
- * @TODO:
8
- * 1. size api 确认
9
- * 2. 修改类名结构
10
- * 3. 支持带数字展示
11
- * 4. InputGroup 模式支持
12
- * 5. 手动聚焦支持额外配置
13
6
  */
14
7
  export declare const Input: React.ForwardRefExoticComponent<InputProps & React.RefAttributes<HTMLInputElement | null>>;
15
8
  export interface InputProps extends HiBaseHTMLFieldProps<'input'> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/input",
3
- "version": "4.0.9",
3
+ "version": "4.0.11",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",
@@ -43,25 +43,24 @@
43
43
  "url": "https://github.com/XiaoMi/hiui/issues"
44
44
  },
45
45
  "dependencies": {
46
- "@hi-ui/classname": "^4.0.1",
47
- "@hi-ui/dom-utils": "^4.0.4",
48
- "@hi-ui/env": "^4.0.1",
49
- "@hi-ui/func-utils": "^4.0.1",
50
- "@hi-ui/icons": "^4.0.4",
51
- "@hi-ui/type-assertion": "^4.0.1",
52
- "@hi-ui/use-latest": "^4.0.1",
53
- "@hi-ui/use-merge-refs": "^4.0.1",
54
- "@hi-ui/use-uncontrolled-state": "^4.0.1"
46
+ "@hi-ui/classname": "^4.0.2",
47
+ "@hi-ui/dom-utils": "^4.0.5",
48
+ "@hi-ui/env": "^4.0.2",
49
+ "@hi-ui/func-utils": "^4.0.2",
50
+ "@hi-ui/icons": "^4.0.16",
51
+ "@hi-ui/type-assertion": "^4.0.2",
52
+ "@hi-ui/use-latest": "^4.0.2",
53
+ "@hi-ui/use-merge-refs": "^4.0.2",
54
+ "@hi-ui/use-uncontrolled-state": "^4.0.2"
55
55
  },
56
56
  "peerDependencies": {
57
- "@hi-ui/core": ">=4.0.0",
57
+ "@hi-ui/core": ">=4.0.6",
58
58
  "react": ">=16.8.6",
59
59
  "react-dom": ">=16.8.6"
60
60
  },
61
61
  "devDependencies": {
62
- "@hi-ui/core": "^4.0.4",
63
- "@hi-ui/core-css": "^4.1.0",
64
- "@hi-ui/hi-build": "^4.0.1",
62
+ "@hi-ui/core": "^4.0.6",
63
+ "@hi-ui/core-css": "^4.1.3",
65
64
  "react": "^17.0.1",
66
65
  "react-dom": "^17.0.1"
67
66
  }