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
8 changes: 8 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface TriggerProps {
popupVisible?: boolean;
defaultPopupVisible?: boolean;
onOpenChange?: (visible: boolean) => void;
afterOpenChange?: (visible: boolean) => void;
/** @deprecated Use `onOpenChange` instead */
onPopupVisibleChange?: (visible: boolean) => void;
/** @deprecated Use `afterOpenChange` instead */
afterPopupVisibleChange?: (visible: boolean) => void;

// =================== Portal ====================
Expand Down Expand Up @@ -139,6 +143,8 @@ export function generateTrigger(
popupVisible,
defaultPopupVisible,
onOpenChange,
afterOpenChange,
onPopupVisibleChange,
afterPopupVisibleChange,

// Delay
Expand Down Expand Up @@ -300,6 +306,7 @@ export function generateTrigger(
) {
lastTriggerRef.current.push(nextOpen);
onOpenChange?.(nextOpen);
onPopupVisibleChange?.(nextOpen);
}
});

Expand Down Expand Up @@ -453,6 +460,7 @@ export function generateTrigger(
const onVisibleChanged = (visible: boolean) => {
setInMotion(false);
onAlign();
afterOpenChange?.(visible);
afterPopupVisibleChange?.(visible);
};

Expand Down
11 changes: 9 additions & 2 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,18 @@ describe('Trigger.Basic', () => {
fireEvent.mouseDown(document.body);
expect(isPopupAllHidden()).toBeTruthy();
});
describe('afterPopupVisibleChange can be triggered', () => {
describe('afterOpenChange can be triggered', () => {
it('uncontrolled', async () => {
let triggered = 0;
const afterPopupVisibleChange = jest.fn();
const { container } = render(
<Trigger
action={['click']}
popupAlign={placementAlignMap.left}
afterPopupVisibleChange={() => {
afterOpenChange={() => {
triggered = 1;
}}
afterPopupVisibleChange={afterPopupVisibleChange}
popup={<strong>trigger</strong>}
>
<div className="target">click</div>
Expand All @@ -256,6 +258,7 @@ describe('Trigger.Basic', () => {
await awaitFakeTimer();

expect(triggered).toBe(1);
expect(afterPopupVisibleChange).toHaveBeenCalledWith(true);
});

it('controlled', async () => {
Expand Down Expand Up @@ -979,10 +982,12 @@ describe('Trigger.Basic', () => {
describe('click window to hide', () => {
it('should hide', async () => {
const onOpenChange = jest.fn();
const onPopupVisibleChange = jest.fn();

const { container } = render(
<Trigger
onOpenChange={onOpenChange}
onPopupVisibleChange={onPopupVisibleChange}
action="click"
popup={<strong>trigger</strong>}
>
Expand All @@ -993,13 +998,15 @@ describe('Trigger.Basic', () => {
fireEvent.click(container.querySelector('.target'));
await awaitFakeTimer();
expect(onOpenChange).toHaveBeenCalledWith(true);
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
onOpenChange.mockReset();

// Click outside to close
fireEvent.mouseDown(document.body);
fireEvent.click(document.body);
await awaitFakeTimer();
expect(onOpenChange).toHaveBeenCalledWith(false);
expect(onPopupVisibleChange).toHaveBeenCalledWith(false);
});

it('should not hide when mouseDown inside but mouseUp outside', async () => {
Expand Down
Loading