Skip to content

feat: add fresh #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2023
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
90 changes: 52 additions & 38 deletions docs/examples/click-nested.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,58 @@ const popupBorderStyle = {
background: 'rgba(255, 0, 0, 0.1)',
};

class Test extends React.Component {
render() {
return (
<div style={{ margin: 200 }}>
<div>
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popup={
// Level 2
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popup={<div style={popupBorderStyle}>i am a click popup</div>}
>
<div style={popupBorderStyle}>
i am a click popup{' '}
<button
type="button"
onMouseDown={(e) => {
e.stopPropagation();
e.preventDefault();
}}
>
I am preventPop
</button>
</div>
</Trigger>
}
>
<span>Click Me</span>
</Trigger>
</div>
const NestPopup = ({ open, setOpen }) => {
return (
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popup={<div style={popupBorderStyle}>i am a click popup</div>}
popupVisible={open}
onPopupVisibleChange={setOpen}
>
<div style={popupBorderStyle}>
i am a click popup{' '}
<button
type="button"
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
}}
>
I am preventPop
</button>
</div>
);
}
}
</Trigger>
);
};

NestPopup.displayName = '🐞 NestPopup';

const Test = () => {
const [open1, setOpen1] = React.useState(false);
const [open2, setOpen2] = React.useState(false);

return (
<div style={{ margin: 200 }}>
<div>
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popupVisible={open1}
onPopupVisibleChange={setOpen1}
popup={
// Level 2
<NestPopup open={open2} setOpen={setOpen2} />
}
fresh
>
<span>Click Me</span>
</Trigger>
</div>
</div>
);
};

export default Test;
6 changes: 5 additions & 1 deletion src/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface PopupProps {
open: boolean;
/** Tell Portal that should keep in screen. e.g. should wait all motion end */
keepDom: boolean;
fresh?: boolean;

// Click
onClick?: React.MouseEventHandler<HTMLDivElement>;
Expand Down Expand Up @@ -76,6 +77,7 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
// Open
open,
keepDom,
fresh,

// Click
onClick,
Expand Down Expand Up @@ -262,7 +264,9 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
align={align}
/>
)}
<PopupContent cache={!open}>{childNode}</PopupContent>
<PopupContent cache={!open && !fresh}>
{childNode}
</PopupContent>
</div>
);
}}
Expand Down
9 changes: 9 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export interface TriggerProps {

alignPoint?: boolean; // Maybe we can support user pass position in the future

/**
* Trigger will memo content when close.
* This may affect the case if want to keep content update.
* Set `fresh` to `false` will always keep update.
*/
fresh?: boolean;

// ==================== Arrow ====================
arrow?: boolean | ArrowTypeOuter;

Expand Down Expand Up @@ -179,6 +186,7 @@ export function generateTrigger(
zIndex,
stretch,
getPopupClassNameFromAlign,
fresh,

alignPoint,

Expand Down Expand Up @@ -687,6 +695,7 @@ export function generateTrigger(
// Open
open={mergedOpen}
keepDom={inMotion}
fresh={fresh}
// Click
onClick={onPopupClick}
// Mask
Expand Down
26 changes: 26 additions & 0 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1165,4 +1165,30 @@ describe('Trigger.Basic', () => {
trigger(container, '.popup', 'mouseEnter');
expect(onPopupVisibleChange).not.toHaveBeenCalled();
});

// https://gith(ub.com/ant-design/ant-design/issues/44830
it('fresh should work', () => {
const Demo = () => {
const [open, setOpen] = React.useState(true);

return (
<Trigger
popupVisible={open}
onPopupVisibleChange={setOpen}
popup={<strong className="x-content">{String(open)}</strong>}
action={['click']}
popupAlign={placementAlignMap.left}
fresh

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldUpdate ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldUpdate 没有判断条件,这里只能是个 boolean

>
<div className="target">click</div>
</Trigger>
);
};

const { container } = render(<Demo />);
expect(document.querySelector('.x-content').textContent).toBe('true');

trigger(container, '.target');
expect(document.querySelector('.x-content').textContent).toBe('false');
});
});