mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 08:59:15 +08:00
95 lines
1.7 KiB
Markdown
95 lines
1.7 KiB
Markdown
---
|
|
order: 1
|
|
title:
|
|
zh-CN: 带输入框的滑块
|
|
en-US: Slider with InputNumber
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
和 [数字输入框](/components/input-number/) 组件保持同步。
|
|
|
|
## en-US
|
|
|
|
Synchronize with [InputNumber](/components/input-number/) component.
|
|
|
|
```tsx
|
|
import { Col, InputNumber, Row, Slider } from 'antd';
|
|
import React, { useState } from 'react';
|
|
|
|
const IntegerStep = () => {
|
|
const [inputValue, setInputValue] = useState(1);
|
|
|
|
const onChange = (newValue: number) => {
|
|
setInputValue(newValue);
|
|
};
|
|
|
|
return (
|
|
<Row>
|
|
<Col span={12}>
|
|
<Slider
|
|
min={1}
|
|
max={20}
|
|
onChange={onChange}
|
|
value={typeof inputValue === 'number' ? inputValue : 0}
|
|
/>
|
|
</Col>
|
|
<Col span={4}>
|
|
<InputNumber
|
|
min={1}
|
|
max={20}
|
|
style={{ margin: '0 16px' }}
|
|
value={inputValue}
|
|
onChange={onChange}
|
|
/>
|
|
</Col>
|
|
</Row>
|
|
);
|
|
};
|
|
|
|
const DecimalStep = () => {
|
|
const [inputValue, setInputValue] = useState(0);
|
|
|
|
const onChange = (value: number) => {
|
|
if (isNaN(value)) {
|
|
return;
|
|
}
|
|
|
|
setInputValue(value);
|
|
};
|
|
|
|
return (
|
|
<Row>
|
|
<Col span={12}>
|
|
<Slider
|
|
min={0}
|
|
max={1}
|
|
onChange={onChange}
|
|
value={typeof inputValue === 'number' ? inputValue : 0}
|
|
step={0.01}
|
|
/>
|
|
</Col>
|
|
<Col span={4}>
|
|
<InputNumber
|
|
min={0}
|
|
max={1}
|
|
style={{ margin: '0 16px' }}
|
|
step={0.01}
|
|
value={inputValue}
|
|
onChange={onChange}
|
|
/>
|
|
</Col>
|
|
</Row>
|
|
);
|
|
};
|
|
|
|
const App: React.FC = () => (
|
|
<div>
|
|
<IntegerStep />
|
|
<DecimalStep />
|
|
</div>
|
|
);
|
|
|
|
export default App;
|
|
```
|