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 <chenlinxh@gmail.com>
This commit is contained in:
SemmyWong 2022-07-28 09:26:50 +08:00 committed by GitHub
parent 5e1b1170a8
commit 92cd76ce48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 20 additions and 15 deletions

View File

@ -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) => {

View File

@ -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<InputProps & InputNumberProps> = (props: any) => {
const { step, value, addonBefore, addonAfter } = props;
if (!isValid(props.value)) {

View File

@ -1,4 +1,4 @@
import { toFixedByStep } from '../ReadPretty';
import { toFixedByStep } from '@nocobase/utils/client';
describe('toFixedByStep', () => {
it('less than precision', () => {

View File

@ -1,4 +1,5 @@
export * from './merge';
export * from './number';
export * from './registry';
export * from './uid';

View File

@ -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';

View File

@ -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);
}

View File

@ -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';

View File

@ -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) {