diff --git a/packages/amis/__tests__/renderers/Form/number.test.tsx b/packages/amis/__tests__/renderers/Form/number.test.tsx index ca5f7faac..7a640ded4 100644 --- a/packages/amis/__tests__/renderers/Form/number.test.tsx +++ b/packages/amis/__tests__/renderers/Form/number.test.tsx @@ -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'); +}); diff --git a/packages/amis/src/renderers/Number.tsx b/packages/amis/src/renderers/Number.tsx index 5a2e27b46..d233fa5af 100644 --- a/packages/amis/src/renderers/Number.tsx +++ b/packages/amis/src/renderers/Number.tsx @@ -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 { } } - 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 { } 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 = {value};