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

Add ability to resolve/reject and hide #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 41 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useRef } from 'react';
import { render, screen, fireEvent, waitForElementToBeRemoved, act } from '@testing-library/react';
import { render, screen, fireEvent, waitForElementToBeRemoved, act, waitFor } from '@testing-library/react';
import NiceModal, {
useModal,
Provider,
Expand Down Expand Up @@ -146,6 +146,46 @@ const testUseModal = async (modal, props = {}) => {

await waitForElementToBeRemoved(screen.queryByText('HocTestModal'));
expect(rejected && rejected.message).toBe('sample error');

/**
* Resolve and hide
*/
let unresolvedValue;

act(() => {
unresolvedValue = modal.show();
});

modalTextElement = screen.queryByText("HocTestModal");
expect(modalTextElement).toBeInTheDocument();

act(() => {
modal.resolveAndHide("example-value");
});

await waitForElementToBeRemoved(screen.queryByText("HocTestModal"));
await waitFor(() => expect(unresolvedValue).resolves.toEqual('example-value'))

/**
* Reject and hide
*/
let caughtValue;

act(() => {
unresolvedValue = modal.show().catch((value) => {
caughtValue = value
});
});

modalTextElement = screen.queryByText("HocTestModal");
expect(modalTextElement).toBeInTheDocument();

act(() => {
modal.rejectAndHide("example-reject");
});

await waitForElementToBeRemoved(screen.queryByText("HocTestModal"));
await waitFor(() => expect(caughtValue).toEqual('example-reject'))
};

test('useModal by id of registered modal', async () => {
Expand Down
26 changes: 26 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ export interface NiceModalHandler<Props = Record<string, unknown>> extends NiceM
* Resolve the promise returned by {@link NiceModalHandler.hide | hide} method.
*/
resolveHide: (args?: unknown) => void;

/**
* Runs {@link NiceModalHandler.resolve} followed by {@link NiceModalHandler.hide}
*/
resolveAndHide: (args?: unknown) => void;

/**
* Runs {@link NiceModalHandler.reject} followed by {@link NiceModalHandler.hide}
*/
rejectAndHide: (args?: unknown) => void;
}

// Omit will not work if extends Record<string, unknown>, which is not needed here
Expand Down Expand Up @@ -339,6 +349,20 @@ export function useModal(modal?: any, args?: any): any {
},
[mid],
);
const resolveAndHide = useCallback(
(args?: unknown) => {
resolveCallback(args);
hideCallback();
},
[resolveCallback, hideCallback],
);
const rejectAndHide = useCallback(
(args?: unknown) => {
rejectCallback(args);
hideCallback();
},
[rejectCallback, hideCallback],
);

return {
id: mid,
Expand All @@ -351,6 +375,8 @@ export function useModal(modal?: any, args?: any): any {
resolve: resolveCallback,
reject: rejectCallback,
resolveHide,
resolveAndHide,
rejectAndHide,
};
}
export const create = <P extends {}>(Comp: React.ComponentType<P>): React.FC<P & NiceModalHocProps> => {
Expand Down