Created
March 19, 2022 00:56
-
-
Save ChemaCLi/b75c8129416c4b48b5ed70bbc96f37d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { forwardRef, useImperativeHandle, useState } from "react" | |
| import { ModalContext } from "./context" | |
| export const ModalContextProvider = forwardRef(({ | |
| children, | |
| modalProps, | |
| modal: Modal | |
| }, ref) => { | |
| const [state, setState] = useState({ | |
| isModalOpen: false, | |
| selectedItem: null | |
| }) | |
| const openModal = (selectedItem = null) => { | |
| setState(prev => ({ | |
| ...prev, | |
| selectedItem, | |
| isModalOpen: true | |
| })) | |
| } | |
| const closeModal = () => { | |
| setState(prev => ({ | |
| ...prev, | |
| isModalOpen: false, | |
| selectedItem: null | |
| })) | |
| } | |
| useImperativeHandle( | |
| ref, | |
| () => ({ | |
| openModal, | |
| closeModal, | |
| ...state | |
| }) | |
| ) | |
| return ( | |
| <ModalContext.Provider value={{ openModal }}> | |
| <Modal | |
| onCancel={closeModal} | |
| visible={state.isModalOpen} | |
| selectedItem={state.selectedItem} | |
| {...modalProps} /> | |
| {children} | |
| </ModalContext.Provider> | |
| ) | |
| }) |
This file contains hidden or 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
| import React from "react" | |
| export const ModalContext = React.createContext({}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment