Using React with Chakra UI. Found, that default <form>
tag breaks layout. Adding and deleting <form>
shows that only it affects layout. Components inside <form>
appears like they are in <Flex flexDir='column'>
tag. I need components inside <form>
in a row
direction.
My jsx code and component layout screenshots (in Chakra UI we write inline css).
Component layout with <form>
tag
<Flex id='Send section' borderColor='gray.200' width='100%' height='20%' padding='20px'>
<form onSubmit={handleSubmit(onSubmit)}>
<FormControl isInvalid={Boolean(errors.text)}>
<Textarea
borderRadius='10px'
height='100%'
{...register('text', textValidator)}
/>
{errors.text && <FormErrorMessage>{errors.text.message}</FormErrorMessage>}
</FormControl>
<Flex flexDir='column' justifyContent='end' ml='1dvh'>
<IconButton
borderRadius='10px'
variant='outline'
colorScheme='blue'
aria-label='Attach files'
mb='1dvh'
icon={<BiPaperclip />}
/>
<IconButton
type="submit"
borderRadius='10px'
variant='outline'
colorScheme='blue'
aria-label='Send message'
icon={<BiSend />}
/>
</Flex>
</form>
</Flex>
If i delete this tag everything will be fine.
Component layout without <form>
tag
I also tried <form style={{width: '100%', flexDirection: 'row'}}>
, and now I have full width, but my buttons are still below the <Textarea>
.
Layout with <form style={{width: '100%', flexDirection: 'row'}}>
2
Answers
Passing
style={{width: '100%', display: 'flex'}}
into<form>
helped!The
Flex
component you’re using is a flexible box layout that allows you to control the direction, alignment, and spacing of your elements.From your code, it appears that the
IconButton
components are being displayed below theTextarea
component. This is because you’ve set theflexDir
property of the parentFlex
component to ‘column’, which arranges the child components in a column (vertically).If you want the
IconButton
components to be displayed next to theTextarea
(i.e., in a row), you should set theflexDir
property to ‘row’. Here’s how you can modify your code: