Skip to content

Commit 50806a2

Browse files
authored
feat: add postTriggerProps for UniqueProvider (#578)
* feat: add postOptions * chore: rename * chore: fix ts * chore: fix ts
1 parent fffc52e commit 50806a2

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

src/UniqueProvider/index.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@ import { getAlignPopupClassName } from '../util';
1818

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

23-
const UniqueProvider = ({ children }: UniqueProviderProps) => {
25+
const UniqueProvider = ({ children, postTriggerProps }: UniqueProviderProps) => {
2426
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();
2527

28+
// ========================== Options ===========================
29+
const mergedOptions = React.useMemo(() => {
30+
if (!options || !postTriggerProps) {
31+
return options;
32+
}
33+
34+
return postTriggerProps(options);
35+
}, [options, postTriggerProps]);
36+
2637
// =========================== Popup ============================
2738
const [popupEle, setPopupEle] = React.useState<HTMLDivElement>(null);
2839
const [popupSize, setPopupSize] = React.useState<{
@@ -155,7 +166,7 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
155166
);
156167

157168
// =========================== Render ===========================
158-
const prefixCls = options?.prefixCls;
169+
const prefixCls = mergedOptions?.prefixCls;
159170

160171
return (
161172
<UniqueContext.Provider value={contextValue}>
@@ -166,14 +177,14 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
166177
ref={setPopupRef}
167178
portal={Portal}
168179
prefixCls={prefixCls}
169-
popup={options.popup}
180+
popup={mergedOptions.popup}
170181
className={classNames(
171-
options.popupClassName,
182+
mergedOptions.popupClassName,
172183
alignedClassName,
173184
`${prefixCls}-unique-controlled`,
174185
)}
175-
style={options.popupStyle}
176-
target={options.target}
186+
style={mergedOptions.popupStyle}
187+
target={mergedOptions.target}
177188
open={open}
178189
keepDom={true}
179190
fresh={true}
@@ -197,12 +208,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
197208
y: arrowY,
198209
}}
199210
align={alignInfo}
200-
zIndex={options.zIndex}
201-
mask={options.mask}
202-
arrow={options.arrow}
203-
motion={options.popupMotion}
204-
maskMotion={options.maskMotion}
205-
// getPopupContainer={options.getPopupContainer}
211+
zIndex={mergedOptions.zIndex}
212+
mask={mergedOptions.mask}
213+
arrow={mergedOptions.arrow}
214+
motion={mergedOptions.popupMotion}
215+
maskMotion={mergedOptions.maskMotion}
216+
getPopupContainer={mergedOptions.getPopupContainer}
206217
>
207218
<UniqueBody
208219
prefixCls={prefixCls}
@@ -219,12 +230,12 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
219230
y: arrowY,
220231
}}
221232
popupSize={popupSize}
222-
motion={options.popupMotion}
233+
motion={mergedOptions.popupMotion}
223234
uniqueBgClassName={classNames(
224-
options.uniqueBgClassName,
235+
mergedOptions.uniqueBgClassName,
225236
alignedClassName,
226237
)}
227-
uniqueBgStyle={options.uniqueBgStyle}
238+
uniqueBgStyle={mergedOptions.uniqueBgStyle}
228239
/>
229240
</Popup>
230241
</TriggerContext.Provider>

src/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export type {
3232
BuildInPlacements,
3333
};
3434

35-
export { default as UniqueProvider } from './UniqueProvider';
35+
import UniqueProvider, { type UniqueProviderProps } from './UniqueProvider';
36+
37+
export { UniqueProvider };
38+
export type { UniqueProviderProps };
3639

3740
export interface TriggerRef {
3841
nativeElement: HTMLElement;

tests/unique.test.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { cleanup, fireEvent, render } from '@testing-library/react';
22
import React from 'react';
3-
import Trigger, { UniqueProvider } from '../src';
3+
import Trigger, { UniqueProvider, type UniqueProviderProps } from '../src';
44
import { awaitFakeTimer } from './util';
55
import type { TriggerProps } from '../src';
6+
import classNames from 'classnames';
67

78
// Mock UniqueBody to check if open props changed
89
global.openChangeLog = [];
@@ -254,4 +255,34 @@ describe('Trigger.Unique', () => {
254255
expect(computedStyle.getPropertyValue('--arrow-x')).not.toBe('');
255256
expect(computedStyle.getPropertyValue('--arrow-y')).not.toBe('');
256257
});
258+
259+
it('should apply postTriggerProps to customize options', async () => {
260+
const postTriggerProps: UniqueProviderProps['postTriggerProps'] = (
261+
options,
262+
) => ({
263+
...options,
264+
popupClassName: classNames(
265+
options.popupClassName,
266+
'custom-post-options-class',
267+
),
268+
});
269+
270+
render(
271+
<UniqueProvider postTriggerProps={postTriggerProps}>
272+
<Trigger
273+
action={['click']}
274+
popup={<strong className="x-content">tooltip</strong>}
275+
unique
276+
popupVisible
277+
>
278+
<div className="target">click me</div>
279+
</Trigger>
280+
</UniqueProvider>,
281+
);
282+
283+
// Check that the custom class from postTriggerProps is applied
284+
expect(document.querySelector('.rc-trigger-popup')).toHaveClass(
285+
'custom-post-options-class',
286+
);
287+
});
257288
});

0 commit comments

Comments
 (0)