mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-31 01:36:23 +02:00
Switch to tRPC (#173)
This commit is contained in:
parent
3d7e7e8a95
commit
2c4157ea24
245 changed files with 1585 additions and 1755 deletions
61
src/components/modal/modal-provider.tsx
Normal file
61
src/components/modal/modal-provider.tsx
Normal file
|
@ -0,0 +1,61 @@
|
|||
import * as React from "react";
|
||||
import { useList } from "react-use";
|
||||
|
||||
import { useRequiredContext } from "../use-required-context";
|
||||
import Modal, { ModalProps } from "./modal";
|
||||
|
||||
export interface ModalProviderProps {
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
const ModalContext =
|
||||
React.createContext<{
|
||||
render: (el: ModalProps) => void;
|
||||
} | null>(null);
|
||||
|
||||
ModalContext.displayName = "<ModalProvider />";
|
||||
|
||||
export const useModalContext = () => {
|
||||
return useRequiredContext(ModalContext);
|
||||
};
|
||||
|
||||
const ModalProvider: React.VoidFunctionComponent<ModalProviderProps> = ({
|
||||
children,
|
||||
}) => {
|
||||
const [modals, { push, removeAt, updateAt }] = useList<ModalProps>([]);
|
||||
|
||||
const removeModalAt = (index: number) => {
|
||||
updateAt(index, { ...modals[index], visible: false });
|
||||
setTimeout(() => {
|
||||
removeAt(index);
|
||||
}, 500);
|
||||
};
|
||||
return (
|
||||
<ModalContext.Provider
|
||||
value={{
|
||||
render: (props) => {
|
||||
push(props);
|
||||
},
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
{modals.map((props, i) => (
|
||||
<Modal
|
||||
key={i}
|
||||
visible={true}
|
||||
{...props}
|
||||
onOk={() => {
|
||||
props.onOk?.();
|
||||
removeModalAt(i);
|
||||
}}
|
||||
onCancel={() => {
|
||||
props.onCancel?.();
|
||||
removeModalAt(i);
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</ModalContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default ModalProvider;
|
Loading…
Add table
Add a link
Reference in a new issue