Skip to content

Commit b8cf088

Browse files
authoredApr 7, 2022
feat(Input):增强数字Input输入框功能 (#747)
1 parent c533549 commit b8cf088

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
 

‎packages/react-input/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ const Demo = () => (
2828
ReactDOM.render(<Demo />, _mount_);
2929
```
3030

31+
### 数字输入框
32+
33+
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
34+
```jsx
35+
import ReactDOM from 'react-dom';
36+
import { InputNumber } from 'uiw';
37+
38+
const Demo = () => (
39+
<div>
40+
<InputNumber
41+
placeholder="请输入内容"
42+
style={{ maxWidth: 200 }}
43+
min={1}
44+
max={10}
45+
/>
46+
</div>
47+
);
48+
ReactDOM.render(<Demo />, _mount_);
49+
```
50+
3151
### Form 中使用 Input
3252

3353
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
@@ -69,6 +89,13 @@ const Demo = () => (
6989
validator: (value = '') => value.length < 4 ? '必填选项!' : null,
7090
children: <Input placeholder="请输入内容" style={{ maxWidth: 200 }} />,
7191
},
92+
inputNumber: {
93+
value: 'www',
94+
label: '请输入内容',
95+
help: '必须长度超过 3 个字符!',
96+
validator: (value = '') => value.length < 4 ? '必填选项!' : null,
97+
children: <InputNumber placeholder="请输入内容" style={{ maxWidth: 200 }} />,
98+
},
7299
}}
73100
>
74101
{({ fields, state, canSubmit }) => {
@@ -77,6 +104,9 @@ const Demo = () => (
77104
<Row>
78105
<Col>{fields.input}</Col>
79106
</Row>
107+
<Row>
108+
<Col>{fields.inputNumber}</Col>
109+
</Row>
80110
<Row>
81111
<Col>
82112
<Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
@@ -386,4 +416,11 @@ ReactDOM.render(<Demo />, _mount_);
386416
| disabled | 禁用输入框 | Boolean | `false` |
387417
| preIcon | 输入框``面放置图标 | String/ReactNode | - |
388418
| addonAfter | 带标签的 input,设置后置标签 | String/ReactNode | - |
389-
| size | 指定输入框的尺寸,除了默认的大小外,还提供了 `large``small``default` 三种尺寸。 | String | - |
419+
| size | 指定输入框的尺寸,除了默认的大小外,还提供了 `large``small``default` 三种尺寸。 | String | - |
420+
421+
## InputNumber
422+
423+
| 参数 | 说明 | 类型 | 默认值 | 版本 |
424+
|--------- |-------- |--------- |-------- |-------- |
425+
| min | 最小值 | Number | - | v4.18.2 |
426+
| max | 最大值 | Number | - | v4.18.2 |
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { useState, useEffect } from 'react';
2+
import Input, { InputProps } from './';
3+
4+
interface InputNumberProps extends InputProps {
5+
min?: number;
6+
max?: number;
7+
step?: number;
8+
}
9+
10+
export default React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {
11+
const { min, max, ...inputProps } = props;
12+
13+
const [value, valueSet] = useState(props.value || 0);
14+
15+
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;
19+
20+
valueSet(v.target.value);
21+
props.onChange?.(v);
22+
};
23+
24+
return <Input {...inputProps} value={value} onChange={onChange} type="number" />;
25+
});

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

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { useEffect, useImperativeHandle } from 'react';
22
import Icon, { IconProps } from '@uiw/react-icon';
33
import { IProps, HTMLInputProps } from '@uiw/utils';
44
import './style/input.less';
5+
export * from './InputNumber';
6+
export { default as InputNumber } from './InputNumber';
57

68
export interface InputProps extends IProps, Omit<HTMLInputProps, 'size'> {
79
preIcon?: IconProps['type'];

‎website/src/routes/components/input/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
import { Divider, Input, Form, Notify, Button, Tag, Row, Col } from 'uiw';
2+
import { Divider, Input, InputNumber, Form, Notify, Button, Tag, Row, Col } from 'uiw';
33
import Markdown from '../../../components/Markdown';
44

55
export default function Page() {
66
return (
77
<Markdown
88
path="https://github.com/uiwjs/uiw/tree/master/packages/react-input/README.md"
9-
dependencies={{ Divider, Input, Form, Notify, Button, Tag, Row, Col }}
9+
dependencies={{ Divider, Input, InputNumber, Form, Notify, Button, Tag, Row, Col }}
1010
renderPage={async () => {
1111
const md = await import('uiw/node_modules/@uiw/react-input/README.md');
1212
return md.default || md;

0 commit comments

Comments
 (0)
Please sign in to comment.