Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions demos/form/layout-change.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ModalForm,
ProForm,
ProFormDateRangePicker,
ProFormDigit,
ProFormRadio,
ProFormSelect,
ProFormText,
Expand Down Expand Up @@ -263,8 +264,7 @@ const Demo = () => {
</Button>
}
onFinish={async (values: any) => {
await waitTime(2000);

await waitTime(500);
message.success('Submission successful');
}}
initialValues={{
Expand Down Expand Up @@ -340,6 +340,17 @@ const Demo = () => {
label="Business Manager"
initialValue="书琰"
/>
<ProFormDigit
name="percent"
label="percent"
width="xs"
min={1}
max={10}
fieldProps={{
precision: 2,
addonAfter: '%',
}}
/>
</FormComponents>
</div>
</>
Expand Down
26 changes: 4 additions & 22 deletions src/field/components/Digit/FieldDigitEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { omit } from '@rc-component/util';
import { InputNumber } from 'antd';
import { InputNumber } from 'antd';
import React from 'react';
import type { ProFieldFC } from '../../types';
import { isEmptyOrWhitespace } from './digitUtils';
import type { FieldDigitProps } from './types';

type Props = Parameters<ProFieldFC<FieldDigitProps>>[0] & {
placeholderValue: string;
proxyChange: (value: number | string | null) => number | string | undefined;
};

export function FieldDigitEdit(props: Props, ref: React.Ref<unknown>) {
Expand All @@ -17,28 +14,13 @@ export function FieldDigitEdit(props: Props, ref: React.Ref<unknown>) {
formItemRender,
fieldProps,
placeholderValue,
proxyChange,
} = props;
const dom = (
<InputNumber<number | string>
ref={ref as React.Ref<any>}
<InputNumber
ref={ref}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ref parameter is typed as React.Ref<unknown>, which is generally not assignable to the more specific ref types expected by InputNumber (typically React.Ref<HTMLInputElement>). Removing the as React.Ref<any> cast may lead to TypeScript compilation errors in environments with strict type checking. It is recommended to restore the cast to ensure compatibility.

Suggested change
ref={ref}
ref={ref as React.Ref<any>}

min={0}
placeholder={placeholderValue}
{...omit(fieldProps, ['onChange', 'onBlur'])}
onChange={(e) => fieldProps?.onChange?.(proxyChange(e))}
onBlur={(e) => {
const value = e.target.value;
if (isEmptyOrWhitespace(value)) {
fieldProps?.onBlur?.(e);
return;
}
const processedValue = proxyChange(value);
if (e.target && typeof processedValue === 'number') {
e.target.value = processedValue.toString();
fieldProps?.onChange?.(processedValue);
}
fieldProps?.onBlur?.(e);
}}
{...fieldProps}
/>
);
if (formItemRender) {
Expand Down
5 changes: 0 additions & 5 deletions src/field/components/Digit/digitUtils.ts

This file was deleted.

31 changes: 1 addition & 30 deletions src/field/components/Digit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useCallback } from 'react';
import React from 'react';
import { useIntl } from '../../../provider';
import { isNil } from '../../../utils';
import {
isProFieldEditOrUpdateMode,
isProFieldReadMode,
Expand All @@ -22,34 +21,7 @@ const FieldDigit: ProFieldFC<FieldDigitProps> = (
const intl = useIntl();
const placeholderValue =
placeholder || intl.getMessage('tableForm.inputPlaceholder', '请输入');
const proxyChange = useCallback(
(value: number | string | null) => {
let val = value ?? undefined;

if (!fieldProps.stringMode && typeof val === 'string') {
const numVal = Number(val);
if (isNaN(numVal)) {
const match = val.match(/^(\d+(?:\.\d+)?)/);
if (match) {
val = Number(match[1]);
} else {
val = undefined;
}
} else {
val = numVal;
}
}
if (
typeof val === 'number' &&
!isNil(val) &&
!isNil(fieldProps.precision)
) {
val = Number(val.toFixed(fieldProps.precision));
}
return val;
},
[fieldProps],
);
if (isProFieldReadMode(type)) {
return FieldDigitRead(
{ text, mode: type, render, placeholder, formItemRender, fieldProps },
Expand All @@ -66,7 +38,6 @@ const FieldDigit: ProFieldFC<FieldDigitProps> = (
formItemRender,
fieldProps,
placeholderValue,
proxyChange,
},
ref,
);
Expand Down
12 changes: 10 additions & 2 deletions tests/form/base.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3575,10 +3575,18 @@ describe('ProForm', () => {
await act(async () => {
fireEvent.change(dom, {
target: {
value: '22.22.22',
value: '22.22', // 先建立合法的 decimalValue = 22.22
},
});
fireEvent.blur(dom);
});

await act(async () => {
fireEvent.change(dom, {
target: {
value: '22.22.22', // 再设置非法字符串
},
});
fireEvent.blur(dom); // blur 回退到 decimalValue → precision=0 → 22
});

await act(async () => {
Expand Down
Loading