fix: InputNumber输入值带单位时渲染错误问题 (#5268)

This commit is contained in:
RUNZE LU 2022-08-30 20:06:46 +08:00
parent 8bd11effcd
commit cdb8211f90
2 changed files with 23 additions and 9 deletions

View File

@ -26,11 +26,12 @@ order: 32
## 设置精度
`precision` 设置数字的显示精度,一般需要配合`step`属性使用,以实现细粒度调整。
`precision` 设置数字的显示精度,一般需要配合`step`属性使用,以实现细粒度调整。注意带有单位的输入不支持配置精度属性。
```schema: scope="body"
{
"type": "form",
"debug": true,
"api": "/api/mock2/form/saveForm",
"data": {
"number2": 3.1234

View File

@ -146,15 +146,23 @@ export default class NumberControl extends React.Component<
const {formItem, setPrinstineValue, precision, value} = props;
const normalizedPrecision = this.filterNum(precision);
/** 如果设置了precision需要处理入参value的精度 */
if (formItem && value != null && normalizedPrecision != null) {
const normalizedValue = toFixed(
value.toString(),
'.',
normalizedPrecision
/**
* precision需要处理入参value的精度
*
*/
if (
formItem &&
value != null &&
normalizedPrecision != null &&
(!unit || unitOptions.length === 0)
) {
const normalizedValue = parseFloat(
toFixed(value.toString(), '.', normalizedPrecision)
);
setPrinstineValue(parseFloat(normalizedValue));
if (!isNaN(normalizedValue)) {
setPrinstineValue(normalizedValue);
}
}
this.state = {unit, unitOptions};
@ -265,10 +273,15 @@ export default class NumberControl extends React.Component<
}
componentDidUpdate(prevProps: NumberProps) {
if (this.props.value !== prevProps.value) {
if (
!isNaN(this.props.value) &&
!isNaN(prevProps.value) &&
this.props.value !== prevProps.value
) {
const unit = this.getUnit();
this.setState({unit: unit});
}
if (this.props.unitOptions !== prevProps.unitOptions) {
this.setState({unitOptions: normalizeOptions(this.props.unitOptions)});
}