Skip to content

Commit 5a50916

Browse files
authored
feat: Support uniqueBgXXX for additional passing (#574)
* feat: uniqueBgClassName * feat: uniqueBgStyle * test: update * test: update
1 parent 0c60e16 commit 5a50916

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ bun.lockb
3939

4040
# dumi
4141
.dumi/tmp
42-
.dumi/tmp-production
42+
.dumi/tmp-production
43+
dist/

src/UniqueProvider/FloatBg.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface FloatBgProps {
1717
offsetY: number;
1818
popupSize?: { width: number; height: number };
1919
motion?: CSSMotionProps;
20+
uniqueBgClassName?: string;
21+
uniqueBgStyle?: React.CSSProperties;
2022
}
2123

2224
const FloatBg = (props: FloatBgProps) => {
@@ -32,6 +34,8 @@ const FloatBg = (props: FloatBgProps) => {
3234
offsetY,
3335
popupSize,
3436
motion,
37+
uniqueBgClassName,
38+
uniqueBgStyle,
3539
} = props;
3640

3741
const floatBgCls = `${prefixCls}-float-bg`;
@@ -72,7 +76,7 @@ const FloatBg = (props: FloatBgProps) => {
7276
}}
7377
>
7478
{({ className: motionClassName, style: motionStyle }) => {
75-
const cls = classNames(floatBgCls, motionClassName, {
79+
const cls = classNames(floatBgCls, motionClassName, uniqueBgClassName, {
7680
[`${floatBgCls}-visible`]: motionVisible,
7781
});
7882

@@ -83,6 +87,7 @@ const FloatBg = (props: FloatBgProps) => {
8387
...offsetStyle,
8488
...sizeStyle,
8589
...motionStyle,
90+
...uniqueBgStyle,
8691
}}
8792
/>
8893
);

src/UniqueProvider/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
216216
offsetY={offsetY}
217217
popupSize={popupSize}
218218
motion={options.popupMotion}
219+
uniqueBgClassName={options.uniqueBgClassName}
220+
uniqueBgStyle={options.uniqueBgStyle}
219221
/>
220222
</Popup>
221223
</TriggerContext.Provider>

src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface UniqueShowOptions {
2020
delay: number;
2121
prefixCls?: string;
2222
popupClassName?: string;
23+
uniqueBgClassName?: string;
24+
uniqueBgStyle?: React.CSSProperties;
2325
popupStyle?: React.CSSProperties;
2426
popupPlacement?: string;
2527
builtinPlacements?: BuildInPlacements;

src/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export interface TriggerProps {
9898
builtinPlacements?: BuildInPlacements;
9999
popupAlign?: AlignType;
100100
popupClassName?: string;
101+
/** Pass to `UniqueProvider` FloatBg */
102+
uniqueBgClassName?: string;
103+
/** Pass to `UniqueProvider` FloatBg */
104+
uniqueBgStyle?: React.CSSProperties;
101105
popupStyle?: React.CSSProperties;
102106
getPopupClassNameFromAlign?: (align: AlignType) => string;
103107
onPopupClick?: React.MouseEventHandler<HTMLDivElement>;
@@ -169,6 +173,8 @@ export function generateTrigger(
169173
// Popup
170174
popup,
171175
popupClassName,
176+
uniqueBgClassName,
177+
uniqueBgStyle,
172178
popupStyle,
173179

174180
popupPlacement,
@@ -323,6 +329,8 @@ export function generateTrigger(
323329
delay,
324330
prefixCls,
325331
popupClassName,
332+
uniqueBgClassName,
333+
uniqueBgStyle,
326334
popupStyle,
327335
popupPlacement,
328336
builtinPlacements,

tests/unique.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { cleanup, fireEvent, render } from '@testing-library/react';
22
import React from 'react';
33
import Trigger, { UniqueProvider } from '../src';
44
import { awaitFakeTimer } from './util';
5+
import type { TriggerProps } from '../src';
56

67
// Mock FloatBg to check if open props changed
78
global.openChangeLog = [];
@@ -27,6 +28,34 @@ jest.mock('../src/UniqueProvider/FloatBg', () => {
2728
};
2829
});
2930

31+
async function setupAndOpenPopup(triggerProps: Partial<TriggerProps> = {}) {
32+
const { container } = render(
33+
<UniqueProvider>
34+
<Trigger
35+
action={['click']}
36+
popup={<strong className="x-content">tooltip</strong>}
37+
unique
38+
{...triggerProps}
39+
>
40+
<div className="target">click me</div>
41+
</Trigger>
42+
</UniqueProvider>,
43+
);
44+
45+
// Initially no popup should be visible
46+
expect(document.querySelector('.rc-trigger-popup')).toBeFalsy();
47+
48+
// Click trigger to show popup
49+
fireEvent.click(container.querySelector('.target'));
50+
await awaitFakeTimer();
51+
52+
// Check that popup exists
53+
const popup = document.querySelector('.rc-trigger-popup');
54+
expect(popup).toBeTruthy();
55+
56+
return { container, popup };
57+
}
58+
3059
describe('Trigger.Unique', () => {
3160
beforeEach(() => {
3261
jest.useFakeTimers();
@@ -152,4 +181,34 @@ describe('Trigger.Unique', () => {
152181
expect(popup.className).toContain('custom-align');
153182
expect(popup.className).toContain('rc-trigger-popup-unique-controlled');
154183
});
184+
185+
it('should apply uniqueBgClassName to FloatBg component', async () => {
186+
await setupAndOpenPopup({ uniqueBgClassName: 'custom-bg-class' });
187+
188+
// Check that FloatBg has the custom background className
189+
const floatBg = document.querySelector('.rc-trigger-popup-float-bg');
190+
expect(floatBg).toBeTruthy();
191+
expect(floatBg.className).toContain('custom-bg-class');
192+
});
193+
194+
it('should apply uniqueBgStyle to FloatBg component', async () => {
195+
await setupAndOpenPopup({ uniqueBgStyle: { backgroundColor: 'red', border: '1px solid blue' } });
196+
197+
// Check that FloatBg has the custom background style
198+
const floatBg = document.querySelector('.rc-trigger-popup-float-bg');
199+
expect(floatBg).toBeTruthy();
200+
expect(floatBg).toHaveStyle({
201+
backgroundColor: 'red',
202+
border: '1px solid blue',
203+
});
204+
});
205+
206+
it('should not apply any additional className to FloatBg when uniqueBgClassName is not provided', async () => {
207+
await setupAndOpenPopup();
208+
209+
// Check that FloatBg exists but does not have custom background className
210+
const floatBg = document.querySelector('.rc-trigger-popup-float-bg');
211+
expect(floatBg).toBeTruthy();
212+
expect(floatBg.className).not.toContain('undefined');
213+
});
155214
});

0 commit comments

Comments
 (0)