2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-20 16:30:13 +08:00
|
|
|
import { supportNodeRef, useComposeRef } from 'rc-util';
|
|
|
|
|
2023-06-18 13:21:39 +08:00
|
|
|
import { NoCompactStyle } from '../space/Compact';
|
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);
|
|
|
|
const mergedRef = useComposeRef(ref, canRef ? (children as any).ref : null);
|
|
|
|
|
2023-06-18 13:21:39 +08:00
|
|
|
return (
|
|
|
|
<OverrideContext.Provider value={context}>
|
2023-08-23 19:23:25 +08:00
|
|
|
<NoCompactStyle>
|
2023-09-20 16:30:13 +08:00
|
|
|
{canRef ? React.cloneElement(children as React.ReactElement, { ref: mergedRef }) : children}
|
2023-08-23 19:23:25 +08:00
|
|
|
</NoCompactStyle>
|
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;
|