Skip to content

Commit 8fd6ce7

Browse files
authoredApr 7, 2022
feat(Input): 增加数字输入框增量设置和超出警告功能 (#748)
1 parent b8cf088 commit 8fd6ce7

File tree

3 files changed

+106
-23
lines changed

3 files changed

+106
-23
lines changed
 

‎packages/react-input/README.md

+52-14
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,48 @@ ReactDOM.render(<Demo />, _mount_);
3535
import ReactDOM from 'react-dom';
3636
import { InputNumber } from 'uiw';
3737

38-
const Demo = () => (
39-
<div>
40-
<InputNumber
41-
placeholder="请输入内容"
42-
style={{ maxWidth: 200 }}
43-
min={1}
44-
max={10}
45-
/>
46-
</div>
47-
);
38+
const Demo = () => {
39+
40+
return(
41+
<div>
42+
<Row gutter={10}>
43+
<Col fixed>
44+
<InputNumber
45+
placeholder="请输入内容"
46+
style={{ width: 200 }}
47+
/>
48+
</Col>
49+
<Col fixed>限制大小</Col>
50+
<Col fixed>
51+
<InputNumber
52+
placeholder="请输入内容"
53+
style={{ width: 200 }}
54+
min={1}
55+
max={10}
56+
/>
57+
</Col>
58+
<Col fixed>超出限界警告</Col>
59+
<Col fixed>
60+
<InputNumber
61+
placeholder="请输入内容"
62+
style={{ width: 200 }}
63+
min={1}
64+
max={10}
65+
overLimitColor={'red'}
66+
/>
67+
</Col>
68+
<Col fixed>设置步涨值</Col>
69+
<Col fixed>
70+
<InputNumber
71+
placeholder="请输入内容"
72+
style={{ width: 200 }}
73+
step={5}
74+
/>
75+
</Col>
76+
</Row>
77+
</div>
78+
)
79+
};
4880
ReactDOM.render(<Demo />, _mount_);
4981
```
5082

@@ -410,17 +442,23 @@ ReactDOM.render(<Demo />, _mount_);
410442

411443
## Input
412444

413-
| 参数 | 说明 | 类型 | 默认值 |
414-
|--------- |-------- |--------- |-------- |
445+
| 参数 | 说明 | 类型 | 默认值 | 版本 |
446+
|--------- |-------- |--------- |-------- |-------- |
415447
| value | 绑定值 | String | - |
416448
| disabled | 禁用输入框 | Boolean | `false` |
417449
| preIcon | 输入框``面放置图标 | String/ReactNode | - |
418450
| addonAfter | 带标签的 input,设置后置标签 | String/ReactNode | - |
419451
| size | 指定输入框的尺寸,除了默认的大小外,还提供了 `large``small``default` 三种尺寸。 | String | - |
452+
| inputStyle | 传递给input的样式,在需要动态设置样式场景下使用 | Object | - | v4.18.2 |
420453

421454
## InputNumber
455+
在v4.18.2中增加
422456

423457
| 参数 | 说明 | 类型 | 默认值 | 版本 |
424458
|--------- |-------- |--------- |-------- |-------- |
425-
| min | 最小值 | Number | - | v4.18.2 |
426-
| max | 最大值 | Number | - | v4.18.2 |
459+
| min | 最小值 | Number | - |
460+
| max | 最大值 | Number | - |
461+
| step | 设置步长值 | Number | - |
462+
| overLimitColor | 允许超出`min`,`max`限制, 使用该警告色显示 | String | - |
463+
464+
更多属性文档请参考 Input。
+44-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useMemo } from 'react';
22
import Input, { InputProps } from './';
33

44
interface InputNumberProps extends InputProps {
55
min?: number;
66
max?: number;
77
step?: number;
8+
overLimitColor?: string;
9+
formatter?: (value: number) => string;
10+
parser?: (value: number) => number;
11+
keyboard?: boolean;
812
}
913

1014
export default React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {
11-
const { min, max, ...inputProps } = props;
15+
const {
16+
className,
17+
min,
18+
max,
19+
step,
20+
overLimitColor,
21+
keyboard = false,
22+
formatter,
23+
prefixCls = 'w-input-number',
24+
...otherProps
25+
} = props;
1226

13-
const [value, valueSet] = useState(props.value || 0);
27+
const value = useMemo(() => Number.parseFloat((props.value || 0)?.toString()), [props.value]);
28+
const [isOverLimit, isOverLimitSet] = useState(overLimitComp(value));
29+
30+
function overLimitComp(value: number) {
31+
if (typeof min === 'number' && value < min) return true;
32+
if (typeof max === 'number' && value > max) return true;
33+
return false;
34+
}
1435

1536
const onChange = (v: React.ChangeEvent<HTMLInputElement>) => {
16-
const parseValue = Number.parseInt(v.target.value);
17-
if (typeof min === 'number' && parseValue < min) return;
18-
if (typeof max === 'number' && parseValue > max) return;
37+
const isOverLimit = overLimitComp(Number.parseFloat(v.target.value));
38+
isOverLimitSet(isOverLimit);
1939

20-
valueSet(v.target.value);
2140
props.onChange?.(v);
2241
};
2342

24-
return <Input {...inputProps} value={value} onChange={onChange} type="number" />;
43+
const overLimitProps = useMemo(() => {
44+
if (!overLimitColor) return { min, max };
45+
}, []);
46+
47+
const cls = [prefixCls, className].filter(Boolean).join(' ').trim();
48+
const inputStyle = useMemo(() => (isOverLimit ? { color: overLimitColor?.toString() } : undefined), [isOverLimit]);
49+
50+
return (
51+
<Input
52+
{...otherProps}
53+
className={cls}
54+
type="number"
55+
inputStyle={inputStyle}
56+
onChange={onChange}
57+
step={step}
58+
{...overLimitProps}
59+
/>
60+
);
2561
});

‎packages/react-input/src/index.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface InputProps extends IProps, Omit<HTMLInputProps, 'size'> {
99
preIcon?: IconProps['type'];
1010
addonAfter?: React.ReactNode;
1111
size?: 'large' | 'default' | 'small';
12+
inputStyle?: React.CSSProperties;
1213
}
1314

1415
export default React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
@@ -20,6 +21,7 @@ export default React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
2021
type = 'text',
2122
preIcon = null,
2223
addonAfter,
24+
inputStyle,
2325
...otherProps
2426
} = props;
2527
const inputRef = React.useRef<HTMLInputElement>(null);
@@ -51,7 +53,14 @@ export default React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
5153
return (
5254
<div className={cls} style={style}>
5355
<Icon type={preIcon} />
54-
<input ref={inputRef} type={type} autoComplete="off" {...otherProps} className={`${prefixCls}-inner`} />
56+
<input
57+
ref={inputRef}
58+
type={type}
59+
autoComplete="off"
60+
{...otherProps}
61+
style={inputStyle}
62+
className={`${prefixCls}-inner`}
63+
/>
5564
{addonAfter && (
5665
<span className={`${prefixCls}-addon-after`} ref={addonRef}>
5766
{addonAfter}

0 commit comments

Comments
 (0)
Please sign in to comment.