Skip to content

Commit 720f5e6

Browse files
committed
feat: add postOptions
1 parent fffc52e commit 720f5e6

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
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+
postOptions?: (options: UniqueShowOptions) => UniqueShowOptions;
2123
}
2224

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

28+
// ========================== Options ===========================
29+
const mergedOptions = React.useMemo(() => {
30+
if (!options || !postOptions) {
31+
return options;
32+
}
33+
34+
return postOptions(options);
35+
}, [options, postOptions]);
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>

tests/unique.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import Trigger, { UniqueProvider } 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,27 @@ 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 postOptions to customize options', async () => {
260+
const postOptions = (options: any) => ({
261+
...options,
262+
popupClassName: classNames(options.popupClassName, 'custom-post-options-class'),
263+
});
264+
265+
render(
266+
<UniqueProvider postOptions={postOptions}>
267+
<Trigger
268+
action={['click']}
269+
popup={<strong className="x-content">tooltip</strong>}
270+
unique
271+
popupVisible
272+
>
273+
<div className="target">click me</div>
274+
</Trigger>
275+
</UniqueProvider>,
276+
);
277+
278+
// Check that the custom class from postOptions is applied
279+
expect(document.querySelector('.rc-trigger-popup')).toHaveClass('custom-post-options-class');
280+
});
257281
});

0 commit comments

Comments
 (0)