mirror of
https://github.com/ant-design/ant-design.git
synced 2026-02-09 02:49:18 +08:00
* feat(ConfigProvider,Form): support tooltipIcon and tooltipProps * feat(ConfigProvider,Form): support tooltipIcon and tooltipProps * feat(ConfigProvider,Form): support tooltipIcon and tooltipProps * feat(Form): support tooltip config * feat(Form): support tooltip config * fix(Form): remove unnecessary type assertion in tooltip prop * refactor(Form): enhance tooltip prop handling by merging context with tooltip properties --------- Co-authored-by: thinkasany <480968828@qq.com>
114 lines
3.4 KiB
TypeScript
114 lines
3.4 KiB
TypeScript
import type { PropsWithChildren, ReactNode } from 'react';
|
|
import * as React from 'react';
|
|
import { FormProvider as RcFormProvider } from '@rc-component/form';
|
|
import type { FormProviderProps as RcFormProviderProps } from '@rc-component/form/lib/FormContext';
|
|
import type { Meta } from '@rc-component/form/lib/interface';
|
|
import { omit } from '@rc-component/util';
|
|
|
|
import type { Variant } from '../config-provider';
|
|
import type { ColProps } from '../grid/col';
|
|
import type {
|
|
FormInstance,
|
|
FormLayout,
|
|
FormSemanticClassNames,
|
|
FormSemanticStyles,
|
|
RequiredMark,
|
|
} from './Form';
|
|
import type { FeedbackIcons, ValidateStatus } from './FormItem';
|
|
import type { FormTooltipProps } from './FormItemLabel';
|
|
import type { FormLabelAlign, NamePath } from './interface';
|
|
|
|
/** Form Context. Set top form style and pass to Form Item usage. */
|
|
export interface FormContextProps {
|
|
classNames?: FormSemanticClassNames;
|
|
styles?: FormSemanticStyles;
|
|
layout: FormLayout;
|
|
name?: string;
|
|
colon?: boolean;
|
|
labelAlign?: FormLabelAlign;
|
|
labelWrap?: boolean;
|
|
labelCol?: ColProps;
|
|
wrapperCol?: ColProps;
|
|
requiredMark?: RequiredMark;
|
|
itemRef: (name: (string | number)[]) => (node: React.ReactElement) => void;
|
|
form?: FormInstance;
|
|
feedbackIcons?: FeedbackIcons;
|
|
tooltip?: FormTooltipProps;
|
|
}
|
|
|
|
export const FormContext = React.createContext<FormContextProps>({
|
|
labelAlign: 'right',
|
|
layout: 'horizontal',
|
|
itemRef: (() => {}) as any,
|
|
});
|
|
|
|
/** `noStyle` Form Item Context. Used for error collection */
|
|
export type ReportMetaChange = (meta: Meta, uniqueKeys: React.Key[]) => void;
|
|
export const NoStyleItemContext = React.createContext<ReportMetaChange | null>(null);
|
|
|
|
/** Form Provider */
|
|
export interface FormProviderProps extends Omit<RcFormProviderProps, 'validateMessages'> {
|
|
prefixCls?: string;
|
|
}
|
|
|
|
export const FormProvider: React.FC<FormProviderProps> = (props) => {
|
|
const providerProps = omit(props, ['prefixCls']);
|
|
return <RcFormProvider {...providerProps} />;
|
|
};
|
|
|
|
/** Used for ErrorList only */
|
|
export interface FormItemPrefixContextProps {
|
|
prefixCls: string;
|
|
status?: ValidateStatus;
|
|
}
|
|
|
|
export const FormItemPrefixContext = React.createContext<FormItemPrefixContextProps>({
|
|
prefixCls: '',
|
|
});
|
|
|
|
export interface FormItemStatusContextProps {
|
|
isFormItemInput?: boolean;
|
|
status?: ValidateStatus;
|
|
errors?: React.ReactNode[];
|
|
warnings?: React.ReactNode[];
|
|
hasFeedback?: boolean;
|
|
feedbackIcon?: ReactNode;
|
|
name?: NamePath;
|
|
}
|
|
|
|
export const FormItemInputContext = React.createContext<FormItemStatusContextProps>({});
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
FormItemInputContext.displayName = 'FormItemInputContext';
|
|
}
|
|
|
|
export type NoFormStyleProps = PropsWithChildren<{
|
|
status?: boolean;
|
|
override?: boolean;
|
|
}>;
|
|
|
|
export const NoFormStyle: React.FC<NoFormStyleProps> = ({ children, status, override }) => {
|
|
const formItemInputContext = React.useContext(FormItemInputContext);
|
|
|
|
const newFormItemInputContext = React.useMemo(() => {
|
|
const newContext = { ...formItemInputContext };
|
|
if (override) {
|
|
delete newContext.isFormItemInput;
|
|
}
|
|
if (status) {
|
|
delete newContext.status;
|
|
delete newContext.hasFeedback;
|
|
delete newContext.feedbackIcon;
|
|
}
|
|
return newContext;
|
|
}, [status, override, formItemInputContext]);
|
|
|
|
return (
|
|
<FormItemInputContext.Provider value={newFormItemInputContext}>
|
|
{children}
|
|
</FormItemInputContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const VariantContext = React.createContext<Variant | undefined>(undefined);
|