-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Story Title Feature: Modal Component ## Changes made - Create Modal Component, on Atom/Modal - Create Jotai Reducer to handle Modal Component - Add Modal Component to jotai-sample page ## How does the solution address the problem This PR will create a reusable modal component that can be accessed anywhere in the app, and can display any content that is passed to it.
- Loading branch information
1 parent
7d0db68
commit 4bc21a9
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use client"; | ||
import { Modal as AntModal, ModalProps } from "antd"; | ||
import { useAtom } from "jotai"; | ||
import { modalReducerAtom } from "@/app/_data/modalState"; | ||
|
||
interface IModalProps extends ModalProps {} | ||
|
||
const Modal: React.FunctionComponent<IModalProps> = ({}) => { | ||
// reducer sample for jotai uses dispatch instead of setModal | ||
const [modal, dispatch] = useAtom(modalReducerAtom); | ||
|
||
const handleCancel = () => { | ||
dispatch({ type: "close" }); | ||
}; | ||
|
||
return ( | ||
<AntModal | ||
open={modal?.open} | ||
onCancel={handleCancel} | ||
// hide the default ok & cancel buttons | ||
footer={null} | ||
> | ||
{modal?.content} | ||
</AntModal> | ||
); | ||
}; | ||
|
||
export default Modal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use client"; | ||
import { ReactNode } from "react"; | ||
|
||
import { atomWithReducer } from "jotai/utils"; | ||
|
||
interface IModalState { | ||
open: boolean; | ||
content: ReactNode | null; | ||
} | ||
|
||
const modalState: IModalState = { | ||
open: false, | ||
content: null, | ||
}; | ||
|
||
const modalReducer = (prev: any, action: any) => { | ||
// content is the content of the modal, for ex, a login form | ||
// this will be passed on reducer action.payload | ||
if (action.type == "open") | ||
return { | ||
open: true, | ||
content: action.payload, | ||
}; | ||
if (action.type == "close") | ||
return { | ||
open: false, | ||
content: null, | ||
}; | ||
}; | ||
|
||
export const modalReducerAtom = atomWithReducer(modalState, modalReducer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters