fix(react): useIonModal/useIonPopover dismiss accepts data and role (#25209)

Resolves #25208
This commit is contained in:
Sean Perkins
2022-04-29 11:57:41 -04:00
committed by GitHub
parent b4ba70ea14
commit 68b2f8bfe1
4 changed files with 30 additions and 8 deletions

View File

@ -40,5 +40,5 @@ export type UseIonModalResult = [
/** /**
* Dismisses the modal * Dismisses the modal
*/ */
() => void (data?: any, role?: string) => void
]; ];

View File

@ -81,8 +81,8 @@ export function useOverlay<OptionsType, OverlayType extends OverlayBase>(
} }
}, []); }, []);
const dismiss = useCallback(async () => { const dismiss = useCallback(async (data?: any, role?: string) => {
overlayRef.current && (await overlayRef.current.dismiss()); overlayRef.current && (await overlayRef.current.dismiss(data, role));
overlayRef.current = undefined; overlayRef.current = undefined;
containerElRef.current = undefined; containerElRef.current = undefined;
}, []); }, []);

View File

@ -48,6 +48,18 @@ describe('useIonModal', () => {
cy.get('ion-modal').should('not.exist'); cy.get('ion-modal').should('not.exist');
}); });
it('display modal and dismiss with data and role', () => {
//show modal
cy.get('ion-button').contains('Show Modal using component param').click();
//close modal
cy.get('ion-button').contains('Close').click();
cy.get('ion-modal').should('not.exist');
//verify role and data on main page was updated
cy.contains('Dismissed with role: close');
cy.contains('Data: {"test":true}');
});
// This test should pass in v6, skipping until merged with v6 // This test should pass in v6, skipping until merged with v6
it.skip('display modal with context', () => { it.skip('display modal with context', () => {

View File

@ -13,7 +13,7 @@ import { useContext } from 'react';
const Body: React.FC<{ const Body: React.FC<{
type: string; type: string;
count: number; count: number;
onDismiss: () => void; onDismiss: (data?: any, role?: string) => void;
onIncrement: () => void; onIncrement: () => void;
}> = ({ count, onDismiss, onIncrement, type }) => ( }> = ({ count, onDismiss, onIncrement, type }) => (
<IonPage> <IonPage>
@ -27,7 +27,7 @@ const Body: React.FC<{
<IonButton expand="block" onClick={() => onIncrement()}> <IonButton expand="block" onClick={() => onIncrement()}>
Increment Count Increment Count
</IonButton> </IonButton>
<IonButton expand="block" onClick={() => onDismiss()}> <IonButton expand="block" onClick={() => onDismiss({ test: true }, 'close')}>
Close Close
</IonButton> </IonButton>
</IonContent> </IonContent>
@ -42,12 +42,15 @@ const ModalWithContext: React.FC = () => {
const ModalHook: React.FC = () => { const ModalHook: React.FC = () => {
const [count, setCount] = useState(0); const [count, setCount] = useState(0);
const [dismissedRole, setDismissedRole] = useState<string | undefined>();
const [dismissedData, setDismissedData] = useState();
const handleIncrement = useCallback(() => { const handleIncrement = useCallback(() => {
setCount(count + 1); setCount(count + 1);
}, [count, setCount]); }, [count, setCount]);
const handleDismissWithComponent = useCallback(() => { const handleDismissWithComponent = useCallback((data, role) => {
dismissWithComponent(); dismissWithComponent(data, role);
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
@ -89,6 +92,11 @@ const ModalHook: React.FC = () => {
onClick={() => { onClick={() => {
presentWithComponent({ presentWithComponent({
cssClass: 'my-class', cssClass: 'my-class',
onDidDismiss: (ev) => {
const { data, role } = ev.detail;
setDismissedData(data);
setDismissedRole(role);
},
}); });
}} }}
> >
@ -127,6 +135,8 @@ const ModalHook: React.FC = () => {
</IonButton> </IonButton>
<div>Count: {count}</div> <div>Count: {count}</div>
<div>Dismissed with role: {dismissedRole}</div>
<div>Data: {dismissedData && JSON.stringify(dismissedData)}</div>
</IonContent> </IonContent>
</IonPage> </IonPage>
</MyContext.Provider> </MyContext.Provider>