skip to Main Content

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
this is the output

but actually I want something like this where name and email will be in one same row just like this
required output

just help me to make those two in one row

2

Answers


  1. 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

    .container{
      width: 200px;
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      gap: 20px;
    }
    .item{
      width: 100%;
      background: #afc;
      height: 50px;
    }
    .item:nth-child(2),
    .item:nth-child(3){
      width: calc(50% - 20px);
    }
    <div class="container">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div>
    Login or Signup to reply.
  2. It’s better to use CSS Grid.

    Code:

    <div class="grid grid-cols-1 gap-6 sm:grid-cols-2 p-20">
        <div class="sm:col-span-2">
            <div class="w-full h-28 bg-red-500 rounded-md text-white text-2xl text-center p-10 font-mono">Large 1</div>
        </div>
        <div>
            <div class="w-full h-14 bg-red-500 rounded-md text-white text-2xl text-center p-3 font-mono">Half</div>
        </div>
        <div>
            <div class="w-full h-14 bg-red-500 rounded-md text-white text-2xl text-center p-3 font-mono">Half</div>
        </div>
        <div class="sm:col-span-2">
            <div class="w-full h-14 bg-red-500 rounded-md text-white text-2xl text-center p-3 font-mono">Middle</div>
        </div>
    </div>
    

    Result:

    preview

    Explanation:

    grid to define your grid. 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 for 640px 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 on 640px and wider screens.

    See playground.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search