mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
fix(react): prevent errors when dismissing inline popover after containing element is removed (#24569)
This commit is contained in:
@ -78,10 +78,19 @@ export const createInlineOverlayComponent = <PropType, ElementType>(
|
|||||||
* cleanup properly.
|
* cleanup properly.
|
||||||
*/
|
*/
|
||||||
this.ref.current?.addEventListener('didDismiss', (evt: any) => {
|
this.ref.current?.addEventListener('didDismiss', (evt: any) => {
|
||||||
const wrapper = this.wrapperRef.current!;
|
const wrapper = this.wrapperRef.current;
|
||||||
this.ref.current!.append(wrapper);
|
const el = this.ref.current;
|
||||||
|
|
||||||
this.setState({ isOpen: false });
|
/**
|
||||||
|
* This component might be unmounted already, if the containing
|
||||||
|
* element was removed while the popover was still open. (For
|
||||||
|
* example, if an item contains an inline popover with a button
|
||||||
|
* that removes the item.)
|
||||||
|
*/
|
||||||
|
if (wrapper && el) {
|
||||||
|
el.append(wrapper);
|
||||||
|
this.setState({ isOpen: false });
|
||||||
|
}
|
||||||
|
|
||||||
this.props.onDidDismiss && this.props.onDidDismiss(evt);
|
this.props.onDidDismiss && this.props.onDidDismiss(evt);
|
||||||
});
|
});
|
||||||
|
@ -18,4 +18,12 @@ describe('IonPopover', () => {
|
|||||||
cy.get('ion-popover ion-list-header').contains('Ionic');
|
cy.get('ion-popover ion-list-header').contains('Ionic');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('display popover and remove containing element', () => {
|
||||||
|
//show popover, remove containing item
|
||||||
|
cy.get('#openPopover').click();
|
||||||
|
cy.get('#removeItem').click();
|
||||||
|
|
||||||
|
//verify popover is gone
|
||||||
|
cy.get('#popoverInItem').should('not.exist');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -30,6 +30,8 @@ const PopoverComponent: React.FC = () => {
|
|||||||
event: undefined,
|
event: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [renderItem, setRenderItem] = useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IonPage>
|
<IonPage>
|
||||||
<IonContent>
|
<IonContent>
|
||||||
@ -70,6 +72,12 @@ const PopoverComponent: React.FC = () => {
|
|||||||
>
|
>
|
||||||
Show Popover, hide after 250 ms
|
Show Popover, hide after 250 ms
|
||||||
</IonButton>
|
</IonButton>
|
||||||
|
{renderItem && <IonItem>
|
||||||
|
<IonButton id="openPopover">Open</IonButton>
|
||||||
|
<IonPopover id="popoverInItem" trigger="openPopover" dismissOnSelect={true}>
|
||||||
|
<IonButton id="removeItem" onClick={() => setRenderItem(false)}>Remove Item</IonButton>
|
||||||
|
</IonPopover>
|
||||||
|
</IonItem>}
|
||||||
</IonContent>
|
</IonContent>
|
||||||
</IonPage>
|
</IonPage>
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user