i have written this code and i am not able to keep the data array of two different identiy type into one row as a field and i have used this code
import React from 'react'
import {Form,Field,ErrorMessage,Formik} from 'formik'
import * as yup from 'yup'
const schema=yup.object().shape({
cmt:yup.string().min(4,'minimun 4 words').required('comment is reqired'),
name:yup.string().required('Name is reqired'),
email:yup.string().required('Email is reqired'),
website:yup.string().required('Enter website url or name'),
})
function ReplyForm() {
const data=[
{type:'text',placeholder:'Write a comment',identiy:'cmt'},
{type:'text',placeholder:'Name',identiy:'name'},
{type:'email',placeholder:'Email',identiy:'email'},
{type:'text',placeholder:'Website',identiy:'website'},
]
return (
<div className='w-full flex justify-center'>
<div className='w-10/12 bg-blue-200'>
<Formik
initialValues={{
cmt:'',
name:'',
email:'',
website:'',
}}
validationSchema={schema}
onSubmit={(values)=>{
console.log(values)
}}
>
{({handlesubmit})=>{
return <Form onSubmit={handlesubmit}>
<div className='w-3/5'>
{data.map((val,i)=>{
if ((val.identiy==='name')||(val.identiy==='email')){
return <div key={i} className='flex justify-between'>
<Field type={val.type} name={val.identiy} placeholder={val.placeholder} className='w-1/2'/>
</div>
}
else{
return <div key={i}>
<Field type={val.type} name={val.identiy} placeholder={val.placeholder}/>
</div>
}
})}
</div>
</Form>
}}
</Formik>
</div>
</div>
)
}
export default ReplyForm
and its output is something like this
but actually I want something like this where name and email will be in one same row just like this
just help me to make those two in one row
2
Answers
Flex doesn’t work like that. U need to wrap name and email inside a flex to make them in a row.
I think you should make the parent node have flex-wrap, and the children node have width 100%, for name and email width 50% (if flex have gap or something make it stretch to next line, decrease width with calc or anyway).
There is an example
It’s better to use CSS Grid.
Code:
Result:
Explanation:
grid
to define yourgrid
.grid-cols-1
to create a grid with 1 equally sized column for mobile devices.gap-6
to change the space between both rows and columns in the grid.sm:grid-cols-2
is to create a grid with 2 equally sized columns for640px
and wider screens.And for those elements that you want to be on the same line (like "comment" and "website") use
sm:col-span-2
to let them take up the whole horizontal space on640px
and wider screens.See playground.