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

Allow model rendering to be overridden #42

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 27 additions & 10 deletions src/ModalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HideFn,
ShowFn,
UpdateFn,
RenderModalFn,
} from './types';
import {
MISSED_MODAL_ID_ERROR_MESSAGE,
Expand All @@ -16,9 +17,26 @@ import { uid } from './utils';

type Props = {
children: React.ReactNode;
renderModal?: RenderModalFn;
};

export default function ModalProvider({ children }: Props) {
const defaultRenderModal: RenderModalFn = params => {
const { Component, props, key, handleClose, handleExited } = params;

return (
<Component
{...props}
key={key}
onClose={handleClose}
{...(handleExited && { onExited: handleExited })}
/>
);
};

export default function ModalProvider({
children,
renderModal = defaultRenderModal,
}: Props) {
const [state, dispatch] = React.useReducer(reducer, initialState);

const update = React.useCallback<UpdateFn>(
Expand Down Expand Up @@ -142,15 +160,14 @@ export default function ModalProvider({ children }: Props) {
}
};

return (
<Component
{...props}
key={id}
onClose={handleClose}
{...(options &&
!options.destroyOnClose && { onExited: handleExited })}
/>
);
return renderModal({
Component,
key: id,
props,
handleClose,
handleExited:
options && !options.destroyOnClose ? handleExited : undefined,
});
});

return (
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentType } from 'react';
import { ComponentType, ReactElement } from 'react';

export type ModalComponent<P> = ComponentType<P>;

Expand Down Expand Up @@ -56,3 +56,13 @@ export type ShowFnOutput<P> = {
destroy: () => void;
update: (newProps: Partial<ModalComponentProps<P>>) => void;
};

export interface RenderModalFnParams {
Component: ComponentType<any>;
props: Props | undefined;
key: string;
handleClose: (...args: any[]) => void;
handleExited: ((...args: any[]) => void) | undefined;
}

export type RenderModalFn = (params: RenderModalFnParams) => ReactElement;