bazarr/frontend/src/modules/modals/WithModal.tsx

37 lines
1002 B
TypeScript
Raw Normal View History

2022-05-31 15:49:04 +00:00
/* eslint-disable @typescript-eslint/ban-types */
2022-03-27 06:42:28 +00:00
2022-05-31 15:49:04 +00:00
import { ContextModalProps } from "@mantine/modals";
import { ModalSettings } from "@mantine/modals/lib/context";
import { createContext, FunctionComponent } from "react";
2022-03-27 06:42:28 +00:00
2022-05-31 15:49:04 +00:00
export type ModalComponent<P extends Record<string, unknown> = {}> =
FunctionComponent<ContextModalProps<P>> & {
modalKey: string;
settings?: ModalSettings;
};
export const StaticModals: ModalComponent[] = [];
2022-03-27 06:42:28 +00:00
2022-05-31 15:49:04 +00:00
export const ModalIdContext = createContext<string | null>(null);
export default function withModal<T extends {}>(
2022-03-27 06:42:28 +00:00
Content: FunctionComponent<T>,
2022-05-31 15:49:04 +00:00
key: string,
defaultSettings?: ModalSettings
2022-03-27 06:42:28 +00:00
) {
2022-05-31 15:49:04 +00:00
const Comp: ModalComponent<T> = (props) => {
const { id, innerProps } = props;
2022-03-27 06:42:28 +00:00
return (
2022-05-31 15:49:04 +00:00
<ModalIdContext.Provider value={id}>
<Content {...innerProps}></Content>
</ModalIdContext.Provider>
2022-03-27 06:42:28 +00:00
);
};
Comp.modalKey = key;
2022-05-31 15:49:04 +00:00
Comp.settings = defaultSettings;
StaticModals.push(Comp as ModalComponent);
2022-03-27 06:42:28 +00:00
return Comp;
}