2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2024-12-10 10:58:00 +08:00
|
|
|
import { getNodeRef, supportNodeRef, useComposeRef } from 'rc-util/lib/ref';
|
2023-09-20 16:30:13 +08:00
|
|
|
|
2024-06-21 02:10:21 +08:00
|
|
|
import ContextIsolator from '../_util/ContextIsolator';
|
2022-07-28 20:33:10 +08:00
|
|
|
import type { MenuProps } from './menu';
|
2022-06-17 14:37:18 +08:00
|
|
|
|
|
|
|
// Used for Dropdown only
|
|
|
|
export interface OverrideContextProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
expandIcon?: React.ReactNode;
|
|
|
|
mode?: MenuProps['mode'];
|
|
|
|
selectable?: boolean;
|
|
|
|
validator?: (menuProps: Pick<MenuProps, 'mode'>) => void;
|
2022-06-21 15:48:29 +08:00
|
|
|
onClick?: () => void;
|
2023-11-08 14:56:15 +08:00
|
|
|
rootClassName?: string;
|
2022-06-17 14:37:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OverrideContext = React.createContext<OverrideContextProps | null>(null);
|
|
|
|
|
2022-09-30 14:26:41 +08:00
|
|
|
/** @internal Only used for Dropdown component. Do not use this in your production. */
|
2023-06-18 13:21:39 +08:00
|
|
|
export const OverrideProvider = React.forwardRef<
|
|
|
|
HTMLElement,
|
|
|
|
OverrideContextProps & { children: React.ReactNode }
|
|
|
|
>((props, ref) => {
|
2022-11-21 09:52:33 +08:00
|
|
|
const { children, ...restProps } = props;
|
2022-06-17 14:37:18 +08:00
|
|
|
const override = React.useContext(OverrideContext);
|
|
|
|
|
2022-11-21 09:52:33 +08:00
|
|
|
const context = React.useMemo<OverrideContextProps>(
|
|
|
|
() => ({ ...override, ...restProps }),
|
2022-06-17 14:37:18 +08:00
|
|
|
[
|
|
|
|
override,
|
|
|
|
restProps.prefixCls,
|
|
|
|
// restProps.expandIcon, Not mark as deps since this is a ReactNode
|
|
|
|
restProps.mode,
|
|
|
|
restProps.selectable,
|
2023-11-08 14:56:15 +08:00
|
|
|
restProps.rootClassName,
|
2022-06-17 14:37:18 +08:00
|
|
|
// restProps.validator, Not mark as deps since this is a function
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-09-20 16:30:13 +08:00
|
|
|
const canRef = supportNodeRef(children);
|
2024-12-10 10:58:00 +08:00
|
|
|
const mergedRef = useComposeRef(ref, canRef ? getNodeRef(children) : null);
|
2023-09-20 16:30:13 +08:00
|
|
|
|
2023-06-18 13:21:39 +08:00
|
|
|
return (
|
|
|
|
<OverrideContext.Provider value={context}>
|
2024-06-22 21:59:12 +08:00
|
|
|
<ContextIsolator space>
|
2023-09-20 16:30:13 +08:00
|
|
|
{canRef ? React.cloneElement(children as React.ReactElement, { ref: mergedRef }) : children}
|
2024-06-21 02:10:21 +08:00
|
|
|
</ContextIsolator>
|
2023-06-18 13:21:39 +08:00
|
|
|
</OverrideContext.Provider>
|
|
|
|
);
|
|
|
|
});
|
2022-06-17 14:37:18 +08:00
|
|
|
|
2023-04-26 14:53:23 +08:00
|
|
|
/** @internal Only used for Dropdown component. Do not use this in your production. */
|
2022-06-17 14:37:18 +08:00
|
|
|
export default OverrideContext;
|