Skip to content
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

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

Merged
merged 5 commits into from
Apr 29, 2022
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
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useIonModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export type UseIonModalResult = [
/**
* Dismisses the modal
*/
() => void
(data?: any, role?: string) => void
];
4 changes: 2 additions & 2 deletions packages/react/src/hooks/useOverlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export function useOverlay<OptionsType, OverlayType extends OverlayBase>(
}
}, []);

const dismiss = useCallback(async () => {
overlayRef.current && (await overlayRef.current.dismiss());
const dismiss = useCallback(async (data?: any, role?: string) => {
overlayRef.current && (await overlayRef.current.dismiss(data, role));
overlayRef.current = undefined;
containerElRef.current = undefined;
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ describe('useIonModal', () => {
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
it.skip('display modal with context', () => {
Expand Down
20 changes: 15 additions & 5 deletions packages/react/test-app/src/pages/overlay-hooks/ModalHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useContext } from 'react';
const Body: React.FC<{
type: string;
count: number;
onDismiss: () => void;
onDismiss: (data?: any, role?: string) => void;
onIncrement: () => void;
}> = ({ count, onDismiss, onIncrement, type }) => (
<IonPage>
Expand All @@ -27,7 +27,7 @@ const Body: React.FC<{
<IonButton expand="block" onClick={() => onIncrement()}>
Increment Count
</IonButton>
<IonButton expand="block" onClick={() => onDismiss()}>
<IonButton expand="block" onClick={() => onDismiss({ test: true }, 'close')}>
Close
</IonButton>
</IonContent>
Expand All @@ -42,13 +42,16 @@ const ModalWithContext: React.FC = () => {
const ModalHook: React.FC = () => {
const [count, setCount] = useState(0);

const [dismissedRole, setDismissedRole] = useState<string | undefined>();
const [dismissedData, setDismissedData] = useState();

const handleIncrement = useCallback(() => {
setCount(count + 1);
}, [count, setCount]);

const handleDismissWithComponent = useCallback(() => {
dismissWithComponent();
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleDismissWithComponent = useCallback((data, role) => {
dismissWithComponent(data, role);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handleDismissWithElement = useCallback(() => {
Expand Down Expand Up @@ -89,6 +92,11 @@ const ModalHook: React.FC = () => {
onClick={() => {
presentWithComponent({
cssClass: 'my-class',
onDidDismiss: (ev) => {
const { data, role } = ev.detail;
setDismissedData(data);
setDismissedRole(role);
},
});
}}
>
Expand Down Expand Up @@ -127,6 +135,8 @@ const ModalHook: React.FC = () => {
</IonButton>

<div>Count: {count}</div>
<div>Dismissed with role: {dismissedRole}</div>
<div>Data: {dismissedData && JSON.stringify(dismissedData)}</div>
</IonContent>
</IonPage>
</MyContext.Provider>
Expand Down