Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Jan 13, 2025
1 parent 9e5ed94 commit e4e7326
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"@types/jest": "^29.2.5",
"@types/lodash": "^4.14.135",
"@types/node": "^22.0.2",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react": "^19.0.6",
"@types/react-dom": "^19.0.1",
"@umijs/fabric": "^4.0.1",
"dumi": "^2.0.0",
"eslint": "^8.54.0",
Expand Down
5 changes: 3 additions & 2 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
}

// Filed element only
const childList = toChildrenArray(children);
const childList = toChildrenArray(children as any);

if (childList.length !== 1 || !React.isValidElement(childList[0])) {
return { child: childList, isFunction: false };
return { child: childList as React.ReactNode, isFunction: false };
}

return { child: childList[0], isFunction: false };
Expand Down
2 changes: 1 addition & 1 deletion src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const Form: React.ForwardRefRenderFunction<FormRef, FormProps> = (
useSubscribe(!childrenRenderProps);

// Listen if fields provided. We use ref to save prev data here to avoid additional render
const prevFieldsRef = React.useRef<FieldData[] | undefined>();
const prevFieldsRef = React.useRef<FieldData[] | undefined>(null);
React.useEffect(() => {
if (!isSimilar(prevFieldsRef.current || [], fields || [])) {
formInstance.setFields(fields || []);
Expand Down
2 changes: 1 addition & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ export class FormStore {
}

function useForm<Values = any>(form?: FormInstance<Values>): [FormInstance<Values>] {
const formRef = React.useRef<FormInstance>();
const formRef = React.useRef<FormInstance>(null);
const [, forceUpdate] = React.useState({});

if (!formRef.current) {
Expand Down
12 changes: 8 additions & 4 deletions tests/useWatch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,13 @@ describe('useWatch', () => {
const more = Form.useWatch(['age', 'name', 'gender'], form);
const demo = Form.useWatch<string>(['demo']);

const values2 = Form.useWatch(values => ({ newName: values.name, newAge: values.age }), form);
const values3 = Form.useWatch<FieldType, { newName?: string }>(values => ({
newName: values.name,
const values2 = Form.useWatch(
_values => ({ newName: _values.name, newAge: _values.age }),
form,
);

const values3 = Form.useWatch<FieldType, { newName?: string }>(_values => ({
newName: _values.name,
}));

return (
Expand Down Expand Up @@ -393,7 +397,7 @@ describe('useWatch', () => {
it('first undefined', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const Demo: React.FC = () => {
const formRef = useRef<FormInstance>();
const formRef = useRef<FormInstance>(null);
const name = Form.useWatch('name', formRef.current);
const [, setUpdate] = useState({});
return (
Expand Down

0 comments on commit e4e7326

Please sign in to comment.