fix: input-number 值为0时静态展示问题

This commit is contained in:
allenve 2023-07-07 17:35:33 +08:00
parent db4ef97b82
commit ddde514cc3
2 changed files with 17 additions and 6 deletions

View File

@ -10,7 +10,7 @@
* 7. &
* 8.
*/
import React = require('react');
import {render, fireEvent, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
@ -273,3 +273,15 @@ test('Renderer:number with big value', async () => {
expect(container).toMatchSnapshot();
});
test('Renderer:number with static', async () => {
const {input: stringValInput} = await setup({
value: '123'
});
const {input: numberValInput} = await setup({
value: 123
});
expect(stringValInput.value).toEqual('123');
expect(numberValInput.value).toEqual('123');
});

View File

@ -1,7 +1,8 @@
import React from 'react';
import {Renderer, RendererProps, stripNumber} from 'amis-core';
import {Renderer, RendererProps, numberFormatter, stripNumber} from 'amis-core';
import moment from 'moment';
import {BaseSchema} from '../Schema';
import isNumber from 'lodash/isNumber';
import {getPropValue, Option, PlainObject, normalizeOptions} from 'amis-core';
/**
@ -89,7 +90,7 @@ export class NumberField extends React.Component<NumberProps> {
}
}
if (value) {
if (typeof value === 'number' || typeof value === 'string') {
// 设置了精度,但是原始数据是字符串,需要转成 float 之后再处理
if (typeof value === 'string' && precision) {
value = stripNumber(parseFloat(value));
@ -114,9 +115,7 @@ export class NumberField extends React.Component<NumberProps> {
}
if (kilobitSeparator) {
let parts = String(value).split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
value = parts.join('.');
value = numberFormatter(value, precision);
}
viewValue = <span>{value}</span>;