utopia-ui/src/Components/Map/hooks/usePopupForm.tsx
Max 82b1f39141
refactor(source): refactor Layer and its subcomponents, replacing cloneElement by context (#185)
* Refactor Layer and its subcomponents, replacing cloneElement by context

* Add showcase for PopupButton template component

* Templateify exported elements (WIP)

* Remove unused file

* Export templateified PopupStartEndInput

* Fix template component type

* Change folder structure

* Lower test coverage

* changed export name

* Refactor PopupForm and PopupView

* More refactoring

* Add provider for PopupFormContext

* Fix popupform title

* Add comments

* Use correct ItemFormPopup for new items

* Fix linting

* Reduce coverage

* Change tailwind prefix

* Fix type

---------

Co-authored-by: Anton Tranelis <mail@antontranelis.de>
Co-authored-by: Anton Tranelis <31516529+antontranelis@users.noreply.github.com>
2025-05-22 20:14:42 +02:00

35 lines
1.0 KiB
TypeScript

import { createContext, useContext, useState } from 'react'
import type { PopupFormState } from '#types/PopupFormState'
type UsePopupFormManagerResult = ReturnType<typeof usePopupFormManager>
const PoupFormContext = createContext<UsePopupFormManagerResult>({
popupForm: {} as PopupFormState | null,
setPopupForm: () => {
/* empty function */
},
})
function usePopupFormManager(): {
popupForm: PopupFormState | null
setPopupForm: React.Dispatch<React.SetStateAction<PopupFormState | null>>
} {
const [popupForm, setPopupForm] = useState<PopupFormState | null>(null)
return { popupForm, setPopupForm }
}
interface Props {
children?: React.ReactNode
}
export const PopupFormProvider: React.FunctionComponent<Props> = ({ children }: Props) => (
<PoupFormContext.Provider value={usePopupFormManager()}>{children}</PoupFormContext.Provider>
)
export const usePopupForm = (): UsePopupFormManagerResult => {
const { popupForm, setPopupForm } = useContext(PoupFormContext)
return { popupForm, setPopupForm }
}