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
41 changes: 26 additions & 15 deletions src/UniqueProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ import { getAlignPopupClassName } from '../util';

export interface UniqueProviderProps {
children: React.ReactNode;
/** Additional handle options data to do the customize info */
postTriggerProps?: (options: UniqueShowOptions) => UniqueShowOptions;
}

const UniqueProvider = ({ children }: UniqueProviderProps) => {
const UniqueProvider = ({ children, postTriggerProps }: UniqueProviderProps) => {
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();

// ========================== Options ===========================
const mergedOptions = React.useMemo(() => {
if (!options || !postTriggerProps) {
return options;
}

return postTriggerProps(options);
}, [options, postTriggerProps]);

// =========================== Popup ============================
const [popupEle, setPopupEle] = React.useState<HTMLDivElement>(null);
const [popupSize, setPopupSize] = React.useState<{
Expand Down Expand Up @@ -155,7 +166,7 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
);

// =========================== Render ===========================
const prefixCls = options?.prefixCls;
const prefixCls = mergedOptions?.prefixCls;

return (
<UniqueContext.Provider value={contextValue}>
Expand All @@ -166,14 +177,14 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
ref={setPopupRef}
portal={Portal}
prefixCls={prefixCls}
popup={options.popup}
popup={mergedOptions.popup}
className={classNames(
options.popupClassName,
mergedOptions.popupClassName,
alignedClassName,
`${prefixCls}-unique-controlled`,
)}
style={options.popupStyle}
target={options.target}
style={mergedOptions.popupStyle}
target={mergedOptions.target}
open={open}
keepDom={true}
fresh={true}
Expand All @@ -197,12 +208,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
y: arrowY,
}}
align={alignInfo}
zIndex={options.zIndex}
mask={options.mask}
arrow={options.arrow}
motion={options.popupMotion}
maskMotion={options.maskMotion}
// getPopupContainer={options.getPopupContainer}
zIndex={mergedOptions.zIndex}
mask={mergedOptions.mask}
arrow={mergedOptions.arrow}
motion={mergedOptions.popupMotion}
maskMotion={mergedOptions.maskMotion}
getPopupContainer={mergedOptions.getPopupContainer}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This change enables the getPopupContainer prop, which was previously commented out. This is a useful enhancement, but it should be covered by a test case to ensure it works correctly with UniqueProvider and doesn't cause any regressions. Please consider adding a test where getPopupContainer is used within a UniqueProvider.

>
<UniqueBody
prefixCls={prefixCls}
Expand All @@ -219,12 +230,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
y: arrowY,
}}
popupSize={popupSize}
motion={options.popupMotion}
motion={mergedOptions.popupMotion}
uniqueBgClassName={classNames(
options.uniqueBgClassName,
mergedOptions.uniqueBgClassName,
alignedClassName,
)}
uniqueBgStyle={options.uniqueBgStyle}
uniqueBgStyle={mergedOptions.uniqueBgStyle}
/>
</Popup>
</TriggerContext.Provider>
Expand Down
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export type {
BuildInPlacements,
};

export { default as UniqueProvider } from './UniqueProvider';
import UniqueProvider, { type UniqueProviderProps } from './UniqueProvider';

export { UniqueProvider };
export type { UniqueProviderProps };

export interface TriggerRef {
nativeElement: HTMLElement;
Expand Down
33 changes: 32 additions & 1 deletion tests/unique.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cleanup, fireEvent, render } from '@testing-library/react';
import React from 'react';
import Trigger, { UniqueProvider } from '../src';
import Trigger, { UniqueProvider, type UniqueProviderProps } from '../src';
import { awaitFakeTimer } from './util';
import type { TriggerProps } from '../src';
import classNames from 'classnames';

// Mock UniqueBody to check if open props changed
global.openChangeLog = [];
Expand Down Expand Up @@ -254,4 +255,34 @@ describe('Trigger.Unique', () => {
expect(computedStyle.getPropertyValue('--arrow-x')).not.toBe('');
expect(computedStyle.getPropertyValue('--arrow-y')).not.toBe('');
});

it('should apply postTriggerProps to customize options', async () => {
const postTriggerProps: UniqueProviderProps['postTriggerProps'] = (
options,
) => ({
...options,
popupClassName: classNames(
options.popupClassName,
'custom-post-options-class',
),
});

render(
<UniqueProvider postTriggerProps={postTriggerProps}>
<Trigger
action={['click']}
popup={<strong className="x-content">tooltip</strong>}
unique
popupVisible
>
<div className="target">click me</div>
</Trigger>
</UniqueProvider>,
);

// Check that the custom class from postTriggerProps is applied
expect(document.querySelector('.rc-trigger-popup')).toHaveClass(
'custom-post-options-class',
);
});
});
Loading