= (props: any) => {
- const { step, value, addonBefore, addonAfter } = props;
+ const { step, formatStyle, value, addonBefore, addonAfter, unitConversion, unitConversionType, separator } = props;
if (!isValid(props.value)) {
return null;
}
- const result = toFixedByStep(value, step);
- if (isNaN(result)) {
+ //单位换算
+ const unitData = formatUnitConversion(value, unitConversionType, unitConversion);
+ //精度换算
+ const preciationData = toFixedByStep(unitData, step);
+ let result;
+ //分隔符换算
+ result = formatNumberWithSeparator(Number(preciationData), separator, countDecimalPlaces(step));
+ if (formatStyle === 'scientifix') {
+ //科学计数显示
+ result = scientificNotation(Number(unitData), countDecimalPlaces(step), separators?.[separator]?.['decimal']);
+ }
+
+ if (!result) {
return null;
}
return (
{addonBefore}
- {result}
+
{addonAfter}
);
diff --git a/packages/core/client/src/schema-component/antd/input-number/__tests__/input-number.test.tsx b/packages/core/client/src/schema-component/antd/input-number/__tests__/input-number.test.tsx
index 678e4707e..01adf6902 100644
--- a/packages/core/client/src/schema-component/antd/input-number/__tests__/input-number.test.tsx
+++ b/packages/core/client/src/schema-component/antd/input-number/__tests__/input-number.test.tsx
@@ -3,6 +3,7 @@ import React from 'react';
import App2 from '../demos/addonBefore&addonAfter';
import App3 from '../demos/highPrecisionDecimals';
import App1 from '../demos/inputNumber';
+import { formatNumberWithSeparator, formatUnitConversion, scientificNotation } from '../ReadPretty';
describe('InputNumber', () => {
it('should display the title', () => {
@@ -43,7 +44,7 @@ describe('InputNumber: addonBefore/addonAfter', () => {
fireEvent.change(input, { target: { value: 1 } });
expect(input.value).toBe('1');
// @ts-ignore
- expect(screen.getByText('¥1万元')).toBeInTheDocument();
+ expect(screen.getByText('¥')).toBeInTheDocument();
// empty value
fireEvent.change(input, { target: { value: '' } });
@@ -67,7 +68,7 @@ describe('InputNumber: High precision decimals', () => {
fireEvent.change(input, { target: { value: 1 } });
expect(input.value).toBe('1.00');
// @ts-ignore
- expect(screen.getByText('1.00%')).toBeInTheDocument();
+ expect(screen.getByText('1.00')).toBeInTheDocument();
// empty value
fireEvent.change(input, { target: { value: '' } });
@@ -75,3 +76,43 @@ describe('InputNumber: High precision decimals', () => {
expect(screen.queryByText('NaN')).toBeNull();
});
});
+
+describe('ReadPretty:formatNumberWithSeparator', () => {
+ // Test case 1: Format a number with default format '0,0.00'
+ test('Format number with default separator', () => {
+ const formatted = formatNumberWithSeparator(1234567.89);
+ expect(formatted).toBe('1,234,567.9');
+ });
+
+ // Test case 2: Format a number with custom format '0.00'
+ test('Format number with custom separator', () => {
+ const formatted = formatNumberWithSeparator(1234567.89, '0.00', 1);
+ expect(formatted).toBe('1234567.9');
+ });
+});
+describe('ReadPretty:formatUnitConversion', () => {
+ // Test case 1: Multiply a value by 2
+ test('Multiply value by 2', () => {
+ const result = formatUnitConversion(10, '*', 2);
+ expect(result).toBe(20);
+ });
+ // Test case 2: Divide a value by 0 (error case)
+ test('Divide value by zero', () => {
+ const result = formatUnitConversion(10, '/', 0);
+ expect(result).toBe(10);
+ });
+});
+
+describe('ReadPretty:scientificNotation', () => {
+ // Test case 1: Format a number into scientific notation with 2 decimal places
+ test('Format number into scientific notation', () => {
+ const formatted = scientificNotation(1234567.89, 2);
+ expect(formatted).toBe('1.23 × 106');
+ });
+
+ // Test case 2: Format a number into scientific notation with custom separator '.'
+ test('Format number into scientific notation with custom separator', () => {
+ const formatted = scientificNotation(1234567.89, 2, '.');
+ expect(formatted).toBe('1.23 × 106');
+ });
+});
diff --git a/packages/core/client/src/schema-settings/SchemaSettingsNumberFormat.tsx b/packages/core/client/src/schema-settings/SchemaSettingsNumberFormat.tsx
new file mode 100644
index 000000000..2f578b0d3
--- /dev/null
+++ b/packages/core/client/src/schema-settings/SchemaSettingsNumberFormat.tsx
@@ -0,0 +1,156 @@
+import { css } from '@emotion/css';
+import { ISchema, Schema, useField, useForm } from '@formily/react';
+import React from 'react';
+import { useTranslation } from 'react-i18next';
+import { Select } from 'antd';
+import { useCollectionManager_deprecated, useDesignable } from '..';
+import { SchemaSettingsModalItem } from './SchemaSettings';
+
+const UnitConversion = ({ unitConversionType }) => {
+ const form = useForm();
+ const { t } = useTranslation();
+ return (
+
+ );
+};
+
+export const SchemaSettingsNumberFormat = function NumberFormatConfig(props: { fieldSchema: Schema }) {
+ const { fieldSchema } = props;
+ const field = useField();
+ const { dn } = useDesignable();
+ const { t } = useTranslation();
+ const { getCollectionJoinField } = useCollectionManager_deprecated();
+ const collectionField = getCollectionJoinField(fieldSchema?.['x-collection-field']) || {};
+ const { formatStyle, unitConversion, unitConversionType, separator, step, addonBefore, addonAfter } =
+ fieldSchema['x-component-props'] || {};
+ const { step: prescition } = collectionField?.uiSchema['x-component-props'] || {};
+
+ return (
+ ,
+ },
+ },
+ separator: {
+ type: 'string',
+ default: separator || '0,0.00',
+ enum: [
+ {
+ value: '0,0.00',
+ label: t('100,000.00'),
+ },
+ {
+ value: '0.0,00',
+ label: t('100.000,00'),
+ },
+ {
+ value: '0 0,00',
+ label: t('100 000.00'),
+ },
+ {
+ value: '0.00',
+ label: t('100000.00'),
+ },
+ ],
+ 'x-decorator': 'FormItem',
+ 'x-component': 'Select',
+ title: "{{t('Separator')}}",
+ },
+ step: {
+ type: 'string',
+ title: '{{t("Precision")}}',
+ 'x-component': 'Select',
+ 'x-decorator': 'FormItem',
+ default: step || prescition || '1',
+ enum: [
+ { value: '1', label: '1' },
+ { value: '0.1', label: '1.0' },
+ { value: '0.01', label: '1.00' },
+ { value: '0.001', label: '1.000' },
+ { value: '0.0001', label: '1.0000' },
+ { value: '0.00001', label: '1.00000' },
+ ],
+ },
+ addonBefore: {
+ type: 'string',
+ title: '{{t("Prefix")}}',
+ 'x-component': 'Input',
+ 'x-decorator': 'FormItem',
+ default: addonBefore,
+ },
+ addonAfter: {
+ type: 'string',
+ title: '{{t("Suffix")}}',
+ 'x-component': 'Input',
+ 'x-decorator': 'FormItem',
+ default: addonAfter,
+ },
+ },
+ } as ISchema
+ }
+ onSubmit={(data) => {
+ const schema = {
+ ['x-uid']: fieldSchema['x-uid'],
+ };
+ schema['x-component-props'] = fieldSchema['x-component-props'] || {};
+ fieldSchema['x-component-props'] = {
+ ...(fieldSchema['x-component-props'] || {}),
+ ...data,
+ };
+ schema['x-component-props'] = fieldSchema['x-component-props'];
+ field.componentProps = fieldSchema['x-component-props'];
+ //子表格/表格区块
+ const parts = (field.path.entire as string).split('.');
+ parts.pop();
+ const modifiedString = parts.join('.');
+ field.query(`${modifiedString}.*[0:].${fieldSchema.name}`).forEach((f) => {
+ if (f.props.name === fieldSchema.name) {
+ f.setComponentProps({ ...data });
+ }
+ });
+ dn.emit('patch', {
+ schema,
+ });
+ dn.refresh();
+ }}
+ />
+ );
+};
diff --git a/packages/core/client/src/schema-settings/SchemaSettingsPlugin.ts b/packages/core/client/src/schema-settings/SchemaSettingsPlugin.ts
index 7272480d3..e9c608b05 100644
--- a/packages/core/client/src/schema-settings/SchemaSettingsPlugin.ts
+++ b/packages/core/client/src/schema-settings/SchemaSettingsPlugin.ts
@@ -44,6 +44,7 @@ import { selectComponentFieldSettings } from '../modules/fields/component/Select
import { subTablePopoverComponentFieldSettings } from '../modules/fields/component/SubTable/subTablePopoverComponentFieldSettings';
import { tagComponentFieldSettings } from '../modules/fields/component/Tag/tagComponentFieldSettings';
import { unixTimestampComponentFieldSettings } from '../modules/fields/component/UnixTimestamp/unixTimestampComponentFieldSettings';
+import { inputNumberComponentFieldSettings } from '../modules/fields/component/InputNumber/inputNumberComponentFieldSettings';
export class SchemaSettingsPlugin extends Plugin {
async load() {
@@ -94,6 +95,7 @@ export class SchemaSettingsPlugin extends Plugin {
this.schemaSettingsManager.add(subTablePopoverComponentFieldSettings);
this.schemaSettingsManager.add(datePickerComponentFieldSettings);
this.schemaSettingsManager.add(unixTimestampComponentFieldSettings);
+ this.schemaSettingsManager.add(inputNumberComponentFieldSettings);
this.schemaSettingsManager.add(fileManagerComponentFieldSettings);
this.schemaSettingsManager.add(tagComponentFieldSettings);