2018-03-20 11:06:04 +00:00
< cn >
#### 输入时格式化展示
2019-02-16 08:51:21 +00:00
结合 [Tooltip ](/components/tooltip-cn/ ) 组件,实现一个数值输入框,方便内容超长时的全量展现。
2018-03-20 11:06:04 +00:00
< / cn >
< us >
#### Format Tooltip Input
2019-02-16 08:51:21 +00:00
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.
2018-03-20 11:06:04 +00:00
< / us >
2019-10-09 10:32:23 +00:00
```tpl
2018-02-27 06:08:15 +00:00
< template >
2019-09-28 12:45:07 +00:00
< a-tooltip :trigger = "['focus']" placement = "topLeft" overlayClassName = "numeric-input" >
2018-02-27 06:08:15 +00:00
< span slot = "title" v-if = "value" class = "numeric-input-title" >
{{value !== '-' ? formatNumber(value) : '-'}}
< / span >
< template slot = "title" v-else >
Input a number
< / template >
< a-input
:value="value"
@change ="onChange"
@blur ="onBlur"
placeholder="Input a number"
2019-09-11 10:11:48 +00:00
:maxLength="25"
2018-02-27 06:08:15 +00:00
style="width: 120px"
/>
< / a-tooltip >
< / template >
< script >
2019-09-28 12:45:07 +00:00
function formatNumber(value) {
value += '';
const list = value.split('.');
const prefix = list[0].charAt(0) === '-' ? '-' : '';
let num = prefix ? list[0].slice(1) : list[0];
let result = '';
while (num.length > 3) {
result = `,${num.slice(-3)}${result}` ;
num = num.slice(0, num.length - 3);
}
if (num) {
result = num + result;
}
return `${prefix}${result}${list[1] ? ` .${list[1]}` : ''}`;
2018-02-27 06:08:15 +00:00
}
2019-09-28 12:45:07 +00:00
export default {
data() {
return {
value: '',
};
2018-02-27 06:08:15 +00:00
},
2019-09-28 12:45:07 +00:00
methods: {
formatNumber,
onChange(e) {
const { value } = e.target;
const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
if ((!isNaN(value) & & reg.test(value)) || value === '' || value === '-') {
this.value = value;
}
},
// '.' at the end or only '-' in the input box.
onBlur() {
const { value, onChange } = this;
if (value.charAt(value.length - 1) === '.' || value === '-') {
onChange({ value: value.slice(0, -1) });
}
},
2018-02-27 06:08:15 +00:00
},
2019-09-28 12:45:07 +00:00
};
2018-02-27 06:08:15 +00:00
< / script >
< style >
2019-09-28 12:45:07 +00:00
/* to prevent the arrow overflow the popup container,
2018-02-27 06:08:15 +00:00
or the height is not enough when content is empty */
2019-09-28 12:45:07 +00:00
.numeric-input .ant-tooltip-inner {
min-width: 32px;
min-height: 37px;
}
2018-02-27 06:08:15 +00:00
2019-09-28 12:45:07 +00:00
.numeric-input .numeric-input-title {
font-size: 14px;
}
2018-02-27 06:08:15 +00:00
< / style >
2018-03-20 11:06:04 +00:00
```