From 92cd76ce4813d73da6ee2f4e89a20dc9d356b83c Mon Sep 17 00:00:00 2001 From: SemmyWong <67748948+semmywong@users.noreply.github.com> Date: Thu, 28 Jul 2022 09:26:50 +0800 Subject: [PATCH] fix: fix accuracy of percent (#685) * fix: fix accuracy of percent * fix: fix accuracy of percent * fix: test unit modify * fix: export number Co-authored-by: chenos --- .../antd/input-number/InputNumber.tsx | 2 +- .../antd/input-number/ReadPretty.tsx | 12 +----------- .../input-number/__tests__/toFixedByStep.test.ts | 2 +- packages/core/utils/src/client.ts | 1 + packages/core/utils/src/index.ts | 2 +- packages/core/utils/src/number.ts | 11 +++++++++++ packages/core/utils/src/server.ts | 1 + .../plugins/export/src/server/renders/renders.ts | 4 +++- 8 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 packages/core/utils/src/number.ts diff --git a/packages/core/client/src/schema-component/antd/input-number/InputNumber.tsx b/packages/core/client/src/schema-component/antd/input-number/InputNumber.tsx index bd3f5142b..6ee347adf 100644 --- a/packages/core/client/src/schema-component/antd/input-number/InputNumber.tsx +++ b/packages/core/client/src/schema-component/antd/input-number/InputNumber.tsx @@ -1,6 +1,6 @@ -import React from 'react'; import { connect, mapReadPretty } from '@formily/react'; import { InputNumber as AntdNumber } from 'antd'; +import React from 'react'; import { ReadPretty } from './ReadPretty'; export const InputNumber = connect((props) => { diff --git a/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx b/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx index 3c6db75e2..4a1538cda 100644 --- a/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx +++ b/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx @@ -1,19 +1,9 @@ import { isValid } from '@formily/shared'; +import { toFixedByStep } from '@nocobase/utils/client'; import type { InputProps } from 'antd/lib/input'; import type { InputNumberProps } from 'antd/lib/input-number'; -import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal'; -import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil'; import React from 'react'; -export function toFixedByStep(value: any, step: string | number) { - if (typeof value === 'undefined' || value === null || value === '') { - return ''; - } - const precision = getNumberPrecision(step); - // return parseFloat(String(value)).toFixed(precision); - return toFixed(String(value), '.', precision); -} - export const ReadPretty: React.FC = (props: any) => { const { step, value, addonBefore, addonAfter } = props; if (!isValid(props.value)) { diff --git a/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts b/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts index c2fcc010a..b2525758e 100644 --- a/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts +++ b/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts @@ -1,4 +1,4 @@ -import { toFixedByStep } from '../ReadPretty'; +import { toFixedByStep } from '@nocobase/utils/client'; describe('toFixedByStep', () => { it('less than precision', () => { diff --git a/packages/core/utils/src/client.ts b/packages/core/utils/src/client.ts index 66beec492..e6eb7f346 100644 --- a/packages/core/utils/src/client.ts +++ b/packages/core/utils/src/client.ts @@ -1,4 +1,5 @@ export * from './merge'; +export * from './number'; export * from './registry'; export * from './uid'; diff --git a/packages/core/utils/src/index.ts b/packages/core/utils/src/index.ts index 7990ed798..096103c48 100644 --- a/packages/core/utils/src/index.ts +++ b/packages/core/utils/src/index.ts @@ -1,7 +1,7 @@ export * from './merge'; export * from './mixin'; export * from './mixin/AsyncEmitter'; +export * from './number'; export * from './registry'; export * from './requireModule'; export * from './uid'; - diff --git a/packages/core/utils/src/number.ts b/packages/core/utils/src/number.ts new file mode 100644 index 000000000..89eb07921 --- /dev/null +++ b/packages/core/utils/src/number.ts @@ -0,0 +1,11 @@ +import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal'; +import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil'; + +export function toFixedByStep(value: any, step: string | number) { + if (typeof value === 'undefined' || value === null || value === '') { + return ''; + } + const precision = getNumberPrecision(step); + // return parseFloat(String(value)).toFixed(precision); + return toFixed(String(value), '.', precision); +} diff --git a/packages/core/utils/src/server.ts b/packages/core/utils/src/server.ts index 7990ed798..f50ad7d92 100644 --- a/packages/core/utils/src/server.ts +++ b/packages/core/utils/src/server.ts @@ -1,6 +1,7 @@ export * from './merge'; export * from './mixin'; export * from './mixin/AsyncEmitter'; +export * from './number'; export * from './registry'; export * from './requireModule'; export * from './uid'; diff --git a/packages/plugins/export/src/server/renders/renders.ts b/packages/plugins/export/src/server/renders/renders.ts index 061c3f679..f590fc82e 100644 --- a/packages/plugins/export/src/server/renders/renders.ts +++ b/packages/plugins/export/src/server/renders/renders.ts @@ -1,3 +1,4 @@ +import { toFixedByStep } from '@nocobase/utils'; import moment from 'moment'; export async function _(field, row, ctx, column?: any) { @@ -38,7 +39,8 @@ export async function datetime(field, row, ctx) { export async function percent(field, row, ctx) { const value = row.get(field.name); - return value && `${value}%`; + const step = field.options?.uiSchema?.['x-component-props']?.['step'] ?? 0; + return value && `${toFixedByStep(value * 100, step)}%`; } export async function boolean(field, row, ctx, column?: any) {