2016-11-24 14:03:57 +08:00
---
order: 7
title:
2019-05-07 14:57:32 +08:00
zh-CN: 输入时格式化展示
en-US: Format Tooltip Input
2016-11-24 14:03:57 +08:00
---
## zh-CN
结合 [Tooltip ](/components/tooltip ) 组件,实现一个数值输入框,方便内容超长时的全量展现。
## en-US
You can use the Input in conjunction with [Tooltip ](/components/tooltip ) component to create a Numeric Input, which can provide a good experience for extra-long content display.
2022-05-19 09:46:26 +08:00
```tsx
2016-11-24 14:03:57 +08:00
import { Input, Tooltip } from 'antd';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2016-11-24 14:03:57 +08:00
2022-05-19 09:46:26 +08:00
interface NumericInputProps {
style: React.CSSProperties;
value: string;
onChange: (value: string) => void;
2016-11-24 14:03:57 +08:00
}
2022-05-19 09:46:26 +08:00
const formatNumber = (value: number) => new Intl.NumberFormat().format(value);
2022-05-14 16:40:42 +08:00
2022-05-19 09:46:26 +08:00
const NumericInput = (props: NumericInputProps) => {
const { value, onChange } = props;
const handleChange = (e: React.ChangeEvent< HTMLInputElement > ) => {
const { value: inputValue } = e.target;
2020-04-03 15:02:11 +08:00
const reg = /^-?\d*(\.\d*)?$/;
2022-05-19 09:46:26 +08:00
if (reg.test(inputValue) || inputValue === '' || inputValue === '-') {
2022-05-14 16:40:42 +08:00
onChange(inputValue);
2016-11-24 14:03:57 +08:00
}
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-11-24 14:03:57 +08:00
// '.' at the end or only '-' in the input box.
2022-05-14 16:40:42 +08:00
const handleBlur = () => {
2020-01-07 11:42:31 +08:00
let valueTemp = value;
2016-11-24 14:03:57 +08:00
if (value.charAt(value.length - 1) === '.' || value === '-') {
2020-01-07 11:42:31 +08:00
valueTemp = value.slice(0, -1);
2016-11-24 14:03:57 +08:00
}
2020-01-07 11:42:31 +08:00
onChange(valueTemp.replace(/0*(\d+)/, '$1'));
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2022-05-14 16:40:42 +08:00
const title = value ? (
2022-05-19 09:46:26 +08:00
< span className = "numeric-input-title" > {value !== '-' ? formatNumber(Number(value)) : '-'}< / span >
2022-05-14 16:40:42 +08:00
) : (
'Input a number'
);
2022-05-19 09:46:26 +08:00
2022-05-14 16:40:42 +08:00
return (
< Tooltip trigger = {['focus']} title = {title} placement = "topLeft" overlayClassName = "numeric-input" >
< Input
{...props}
onChange={handleChange}
onBlur={handleBlur}
placeholder="Input a number"
2022-09-24 23:13:24 +08:00
maxLength={16}
2022-05-14 16:40:42 +08:00
/>
< / Tooltip >
);
};
2016-11-24 14:03:57 +08:00
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
const [value, setValue] = useState('');
2018-06-27 15:55:04 +08:00
2022-05-19 09:46:26 +08:00
return < NumericInput style = {{ width: 120 } } value = {value} onChange = {setValue} / > ;
2022-05-14 16:40:42 +08:00
};
2022-05-19 09:46:26 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```
2016-11-24 14:03:57 +08:00
2019-05-07 14:57:32 +08:00
```css
2017-01-05 15:35:43 +08:00
/* to prevent the arrow overflow the popup container,
2016-11-24 14:03:57 +08:00
or the height is not enough when content is empty */
.numeric-input .ant-tooltip-inner {
min-width: 32px;
min-height: 37px;
}
.numeric-input .numeric-input-title {
font-size: 14px;
}
2019-05-07 14:57:32 +08:00
```