Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use PrefixContext to set prefixName instead of using FieldContext #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getNamePath,
getValue,
} from './utils/valueUtil';
import PrefixContext from './PrefixContext';

const EMPTY_ERRORS: any[] = [];

Expand Down Expand Up @@ -88,6 +89,8 @@ export interface InternalFieldProps<Values = any> {
/** @private Pass context as prop instead of context api
* since class component can not get context in constructor */
fieldContext?: InternalFormInstance;

prefixName?: InternalNamePath;
}

export interface FieldProps<Values = any>
Expand Down Expand Up @@ -151,6 +154,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
initEntityValue(this);
}
}
props: { name?: NamePath; rules?: Rule[]; dependencies?: NamePath[]; initialValue?: any; };

public componentDidMount() {
const { shouldUpdate, fieldContext } = this.props;
Expand Down Expand Up @@ -187,8 +191,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F

// ================================== Utils ==================================
public getNamePath = (): InternalNamePath => {
const { name, fieldContext } = this.props;
const { prefixName = [] }: InternalFormInstance = fieldContext;
const { name, prefixName = [] } = this.props;

return name !== undefined ? [...prefixName, ...name] : [];
};
Expand Down Expand Up @@ -624,6 +627,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F

function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>) {
const fieldContext = React.useContext(FieldContext);
const prefixName = React.useContext(PrefixContext);

const namePath = name !== undefined ? getNamePath(name) : undefined;

Expand All @@ -643,7 +647,7 @@ function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>)
warning(false, '`preserve` should not apply on Form.List fields.');
}

return <Field key={key} name={namePath} {...restProps} fieldContext={fieldContext} />;
return <Field key={key} name={namePath} {...restProps} fieldContext={fieldContext} prefixName={prefixName} />;
}

export default WrapperField;
12 changes: 6 additions & 6 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import warning from 'rc-util/lib/warning';
import type { InternalNamePath, NamePath, StoreValue, ValidatorRule, Meta } from './interface';
import FieldContext from './FieldContext';
import PrefixContext from './PrefixContext';
import Field from './Field';
import { move, getNamePath } from './utils/valueUtil';
import type { ListContextProps } from './ListContext';
Expand Down Expand Up @@ -45,12 +46,11 @@ const List: React.FunctionComponent<ListProps> = ({
});
const keyManager = keyRef.current;

const prefixNameContext = React.useContext(PrefixContext);
const prefixName: InternalNamePath = React.useMemo(() => {
const parentPrefixName = getNamePath(context.prefixName) || [];
const parentPrefixName = getNamePath(prefixNameContext) || [];
return [...parentPrefixName, ...getNamePath(name)];
}, [context.prefixName, name]);

const fieldContext = React.useMemo(() => ({ ...context, prefixName }), [context, prefixName]);
}, [prefixNameContext, name]);

// List context
const listContext = React.useMemo<ListContextProps>(
Expand Down Expand Up @@ -79,7 +79,7 @@ const List: React.FunctionComponent<ListProps> = ({

return (
<ListContext.Provider value={listContext}>
<FieldContext.Provider value={fieldContext}>
<PrefixContext.Provider value={prefixName}>
<Field
name={[]}
shouldUpdate={shouldUpdate}
Expand Down Expand Up @@ -188,7 +188,7 @@ const List: React.FunctionComponent<ListProps> = ({
);
}}
</Field>
</FieldContext.Provider>
</PrefixContext.Provider>
</ListContext.Provider>
);
};
Expand Down
7 changes: 7 additions & 0 deletions src/PrefixContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import * as React from 'react';
import { InternalNamePath } from './interface';

const Context = React.createContext<InternalNamePath>([]);

export default Context;
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { FormProps } from './Form';
import FieldForm from './Form';
import { FormProvider } from './FormContext';
import FieldContext from './FieldContext';
import PrefixContext from './PrefixContext';
import ListContext from './ListContext';
import useWatch from './useWatch';

Expand All @@ -31,7 +32,7 @@ RefForm.List = List;
RefForm.useForm = useForm;
RefForm.useWatch = useWatch;

export { FormInstance, Field, List, useForm, FormProvider, FieldContext, ListContext, useWatch };
export { FormInstance, Field, List, useForm, FormProvider, FieldContext, ListContext, PrefixContext, useWatch };

export type { FormProps };

Expand Down
5 changes: 0 additions & 5 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ export interface FormInstance<Values = any> {
export type InternalFormInstance = Omit<FormInstance, 'validateFields'> & {
validateFields: InternalValidateFields;

/**
* Passed by field context props
*/
prefixName?: InternalNamePath;

validateTrigger?: string | string[] | false;

/**
Expand Down