Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
How do you create a date field with react-hook-form that has 3 inputs (month, day, year)?

I get how to use react-hook-form's Controller in creating an input.

import styles from './index.module.css'
import { Controller } from 'react-hook-form'

export default function InputField({
  label,
  placeholder,
  type,
  value,
  inputId,
  control,
  name,
  ...props
}) {
  return (
    <div className={styles.field}>
      <label htmlFor={inputId} className={styles.label}>{label}</label>
      <Controller
        name={name}
        control={control}
        render={({ field }) => <input id={inputId} {...field} className={styles.input} placeholder={placeholder} />}
      />
    </div>
  )
}

However, how do you create a "date field", which has a select box for the month, the day, and the year? In theory, we want to have the form spit out a JSON date object, which looks like this:

JSON.stringify(new Date)
'"2021-11-04T06:47:07.567Z"'

So we want the handleSubmit to receive { "aDateField": "2021-11-04T06:47:07.567Z" } sort of thing. But the implementation of the Controller is 3 select boxes, which may be named something like aDateField[month], aDateField[day], and aDateField[year] or may not have any names themselves, as they are not used in the final JSON output. How do you do this with react-hook-form?

The architecture of the HTML will really be 2 select boxes and an input (for the year, so you don't have to scroll through potentially thousands of years, going back to 2000 BCE).

<select>
  <option>Month...</option>
  <option value="0">January</option>
  <option value="1">February</option>
  ...
</select>
<select>
  <option>Day...</option>
  <option value="0">1</option>
  <option value="1">2</option>
  ...
</select>
<input name="year" />

I am confused as to how to properly serialize the sub-inputs into a final output JSON value, following the Control react-hook-form paradigm.

To keep it simple, you could just as well use 3 text inputs for the values instead of select boxes for now.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda