Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions tests/unique.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import Trigger, { UniqueProvider } 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,27 @@ 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 = (options: any) => ({
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 options parameter is typed as any. For better type safety and maintainability, please use the UniqueShowOptions type. You will need to add an import for it at the top of the file:

import type { UniqueShowOptions } from '../src/context';
Suggested change
const postTriggerProps = (options: any) => ({
const postTriggerProps = (options: UniqueShowOptions) => ({

...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