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

103 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-06-01 14:04:58 +00:00
import {
ModalContent,
ModalOverlay,
ModalRoot,
} from '../elements/ModalPrimitives'
import { Box, HStack, VStack } from '../elements/LayoutPrimitives'
import { Button } from '../elements/Button'
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-03 02:42:00 +00:00
import { CrossIcon } from '../elements/images/CrossIcon'
import { theme } from '../tokens/stitches.config'
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)
}}
css={{ overflow: 'auto', p: '0' }}
>
<VStack>
<HStack
distribution="between"
alignment="center"
css={{ width: '100%' }}
>
<StyledText style="modalHeadline" css={{ p: '16px' }}>
{props.title}
</StyledText>
<Button
css={{ pt: '16px', pr: '16px' }}
style="ghost"
onClick={() => {
props.onOpenChange(false)
}}
>
<CrossIcon
size={20}
strokeColor={theme.colors.grayText.toString()}
/>
</Button>
</HStack>
<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-06-03 02:42:00 +00:00
<HStack key={index} css={{ padding: '10px 0 0 10px' }}>
<Box
css={{
p: '0',
width: '25%',
paddingLeft: '16px',
paddingTop: '5px',
2022-06-02 04:32:13 +00:00
}}
2022-06-03 02:42:00 +00:00
>
<StyledText style={'menuTitle'}>
2022-06-03 02:42:00 +00:00
{input.label}
</StyledText>
</Box>
<Box css={{ width: '100%', marginRight: '20px' }}>
2022-06-03 08:23:05 +00:00
<GeneralFormInput {...input} />
2022-06-03 02:42:00 +00:00
</Box>
2022-06-01 14:04:58 +00:00
</HStack>
))}
2022-06-03 02:42:00 +00:00
<HStack
alignment="end"
distribution="end"
2022-06-03 02:42:00 +00:00
css={{
width: '100%',
padding: '32px 22px 20px 0',
2022-06-03 02:42:00 +00:00
}}
>
<Button style={'ctaDarkYellow'}>
{props.acceptButtonLabel || 'Submit'}
</Button>
2022-06-01 14:04:58 +00:00
</HStack>
</form>
</Box>
</VStack>
</ModalContent>
</ModalRoot>
)
}