ant-design/components/menu/OverrideContext.tsx
二货爱吃白萝卜 91878f82ad
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
refactor: update ref logic (#51952)
* refactor: update ref logic

* chore: clean up

* chore: coverage adjust

* test: coverage

* chore: back of code
2024-12-10 10:58:00 +08:00

55 lines
1.7 KiB
TypeScript

import * as React from 'react';
import { getNodeRef, supportNodeRef, useComposeRef } from 'rc-util/lib/ref';
import ContextIsolator from '../_util/ContextIsolator';
import type { MenuProps } from './menu';
// Used for Dropdown only
export interface OverrideContextProps {
prefixCls?: string;
expandIcon?: React.ReactNode;
mode?: MenuProps['mode'];
selectable?: boolean;
validator?: (menuProps: Pick<MenuProps, 'mode'>) => void;
onClick?: () => void;
rootClassName?: string;
}
const OverrideContext = React.createContext<OverrideContextProps | null>(null);
/** @internal Only used for Dropdown component. Do not use this in your production. */
export const OverrideProvider = React.forwardRef<
HTMLElement,
OverrideContextProps & { children: React.ReactNode }
>((props, ref) => {
const { children, ...restProps } = props;
const override = React.useContext(OverrideContext);
const context = React.useMemo<OverrideContextProps>(
() => ({ ...override, ...restProps }),
[
override,
restProps.prefixCls,
// restProps.expandIcon, Not mark as deps since this is a ReactNode
restProps.mode,
restProps.selectable,
restProps.rootClassName,
// restProps.validator, Not mark as deps since this is a function
],
);
const canRef = supportNodeRef(children);
const mergedRef = useComposeRef(ref, canRef ? getNodeRef(children) : null);
return (
<OverrideContext.Provider value={context}>
<ContextIsolator space>
{canRef ? React.cloneElement(children as React.ReactElement, { ref: mergedRef }) : children}
</ContextIsolator>
</OverrideContext.Provider>
);
});
/** @internal Only used for Dropdown component. Do not use this in your production. */
export default OverrideContext;