omnivore/packages/web/components/patterns/FormModal.tsx

71 lines
2 KiB
TypeScript
Raw Normal View History

2022-06-01 14:04:58 +00:00
import {
ModalButtonBar,
2022-06-01 14:04:58 +00:00
ModalContent,
ModalOverlay,
ModalRoot,
ModalTitleBar,
2022-06-01 14:04:58 +00:00
} from '../elements/ModalPrimitives'
import { Box, VStack } from '../elements/LayoutPrimitives'
2022-06-01 14:04:58 +00:00
import { StyledText } from '../elements/StyledText'
2022-06-02 13:34:17 +00:00
import { useState } from 'react'
2022-06-03 08:23:05 +00:00
import { FormInputProps, GeneralFormInput } from '../elements/FormElements'
2022-06-01 14:04:58 +00:00
2022-06-01 14:28:46 +00:00
export interface FormModalProps {
2022-06-01 14:04:58 +00:00
inputs?: FormInputProps[]
title: string
acceptButtonLabel?: string
onSubmit: () => void
onOpenChange: (open: boolean) => void
}
export function FormModal(props: FormModalProps): JSX.Element {
2022-06-02 13:34:17 +00:00
const [inputs, setInputs] = useState<FormInputProps[]>(props.inputs || [])
2022-06-01 14:04:58 +00:00
return (
<ModalRoot defaultOpen onOpenChange={props.onOpenChange}>
<ModalOverlay />
2022-06-03 02:42:00 +00:00
<ModalContent
onPointerDownOutside={(event) => {
event.preventDefault()
props.onOpenChange(false)
}}
2022-07-12 01:14:19 +00:00
css={{ overflow: 'auto', px: '24px' }}
2022-06-03 02:42:00 +00:00
>
<VStack>
<ModalTitleBar
title={props.title}
onOpenChange={props.onOpenChange}
/>
2022-06-03 02:42:00 +00:00
<Box css={{ width: '100%' }}>
2022-06-01 14:28:46 +00:00
<form
onSubmit={(event) => {
event.preventDefault()
props.onSubmit()
props.onOpenChange(false)
}}
>
2022-06-02 13:34:17 +00:00
{inputs.map((input, index) => (
2022-07-12 01:14:19 +00:00
<VStack key={index}>
<StyledText
style={'menuTitle'}
css={{ pt: index > 0 ? '10px' : 'unset' }}
>
{input.label}
</StyledText>
2022-07-12 01:14:19 +00:00
<Box css={{ width: '100%' }}>
2022-06-03 08:23:05 +00:00
<GeneralFormInput {...input} />
2022-06-03 02:42:00 +00:00
</Box>
2022-07-12 01:14:19 +00:00
</VStack>
2022-06-01 14:04:58 +00:00
))}
<ModalButtonBar
onOpenChange={props.onOpenChange}
acceptButtonLabel={props.acceptButtonLabel}
/>
2022-06-01 14:04:58 +00:00
</form>
</Box>
</VStack>
</ModalContent>
</ModalRoot>
)
}