Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/UniqueProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
import FloatBg from './FloatBg';
import classNames from 'classnames';
import MotionContent from './MotionContent';
import { getAlignPopupClassName } from '../util';

export interface UniqueProviderProps {
children: React.ReactNode;
Expand Down Expand Up @@ -93,6 +94,26 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
false, // isMobile
);

const alignedClassName = React.useMemo(() => {
if (!options) {
return '';
}

const baseClassName = getAlignPopupClassName(
options.builtinPlacements || {},
options.prefixCls || '',
alignInfo,
false, // alignPoint is false for UniqueProvider
);

return classNames(baseClassName, options.getPopupClassNameFromAlign?.(alignInfo));
}, [
alignInfo,
options?.getPopupClassNameFromAlign,
options?.builtinPlacements,
options?.prefixCls,
]);
Comment on lines +97 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The dependency array for React.useMemo uses optional chaining (options?.prop), which is an anti-pattern and can lead to incorrect memoization behavior. The exhaustive-deps lint rule would likely flag this. It's better to depend on the options object directly and destructure the needed properties inside the memoized function. This makes the dependencies clearer and ensures the memoization works as expected when the options object itself changes.

  const alignedClassName = React.useMemo(() => {
    if (!options) {
      return '';
    }

    const { builtinPlacements, prefixCls, getPopupClassNameFromAlign } = options;

    const baseClassName = getAlignPopupClassName(
      builtinPlacements || {},
      prefixCls || '',
      alignInfo,
      false, // alignPoint is false for UniqueProvider
    );

    return classNames(baseClassName, getPopupClassNameFromAlign?.(alignInfo));
  }, [alignInfo, options]);


const contextValue = React.useMemo<UniqueContextProps>(
() => ({
show,
Expand Down Expand Up @@ -141,6 +162,7 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
}
className={classNames(
options.popupClassName,
alignedClassName,
`${prefixCls}-unique-controlled`,
)}
style={options.popupStyle}
Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface UniqueShowOptions {
maskMotion?: CSSMotionProps;
arrow?: ArrowTypeOuter;
getPopupContainer?: TriggerProps['getPopupContainer'];
getPopupClassNameFromAlign?: (align: AlignType) => string;
}

export interface UniqueContextProps {
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export function generateTrigger(
maskMotion,
arrow: innerArrow,
getPopupContainer,
getPopupClassNameFromAlign,
id,
}));

Expand Down
52 changes: 52 additions & 0 deletions tests/unique.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,56 @@ describe('Trigger.Unique', () => {
// FloatBg open prop should not have changed during transition (no close animation)
expect(global.openChangeLog).toHaveLength(0);
});

it('should add aligned className to UniqueProvider popup', async () => {
const getPopupClassNameFromAlign = (align: any) => {
return `custom-align-${align.points?.[0] || 'default'}`;
};

const { container } = render(
<UniqueProvider>
<Trigger
action={['click']}
popup={<strong className="x-content">tooltip</strong>}
unique
popupPlacement="bottomLeft"
builtinPlacements={{
bottomLeft: {
points: ['tl', 'bl'],
offset: [0, 4],
overflow: {
adjustX: 0,
adjustY: 1,
},
},
}}
getPopupClassNameFromAlign={getPopupClassNameFromAlign}
>
<div className="target">click me</div>
</Trigger>
</UniqueProvider>,
);

// Initially no popup should be visible
expect(document.querySelector('.rc-trigger-popup')).toBeFalsy();

// Click trigger to show popup
fireEvent.click(container.querySelector('.target'));
await awaitFakeTimer();

// Wait a bit more for alignment to complete
await awaitFakeTimer();

// Check that popup exists
const popup = document.querySelector('.rc-trigger-popup');
expect(popup).toBeTruthy();
expect(popup.querySelector('.x-content').textContent).toBe('tooltip');

// Check that custom className from getPopupClassNameFromAlign is applied
expect(popup.className).toContain('custom-align');
expect(popup.className).toContain('rc-trigger-popup-unique-controlled');

// The base placement className might not be available immediately due to async alignment
// but the custom className should always be applied
});
});
Loading