DateInput

Free form date input that allows users to type a date.

Import

import { DateInput } from '@tidbcloud/uikit'

Usage

import { useState } from 'react'
import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  const [value, setValue] = useState<Date | null>(null)
  return <DateInput value={value} onChange={setValue} label="Date input" placeholder="Date input" />
}

Value format

Use valueFormat prop to change the dayjs format of the value label:

import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <DateInput valueFormat="YYYY MMM DD" label="Date input" placeholder="Date input" />
}

Allow clear

Set clearable prop to allow removing value from the input:

import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <DateInput clearable defaultValue={new Date()} label="Date input" placeholder="Date input" />
}

Min and max date

Set minDate and maxDate props to define allowed date range:

import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  const today = new Date()
  const nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, today.getDate())
 
  return <DateInput minDate={today} maxDate={nextMonth} label="Date input" placeholder="Date input" />
}

Custom date parser

Use dateParser prop to replace default date parser:

import dayjs from 'dayjs'
import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  const dateParser = (input: string) => {
    if (input === 'WW2') {
      return new Date(1939, 8, 1)
    }
    return dayjs(input, 'DD/MM/YYYY').toDate()
  }
 
  return <DateInput dateParser={dateParser} valueFormat="DD/MM/YYYY" label="Type WW2" placeholder="Type WW2" />
}

Disabled state

import { DateInput } from '@tidbcloud/uikit'
 
function Demo() {
  return <DateInput label="Disabled" placeholder="Date input" disabled />
}

Props

PropTypeDefaultDescription
valuestring | null-Controlled component value
defaultValuestring-Uncontrolled default value
onChange(value: string | null) => void-Called when value changes
valueFormatstring’MMMM D, YYYY’dayjs format for display
dateParser(value: string) => string | null-Custom date parser function
clearablebooleanfalseShow clear button
minDatestring-Minimum allowed date
maxDatestring-Maximum allowed date
excludeDate(date: string) => boolean-Function to exclude dates
fixOnBlurbooleantrueRevert to last valid value on blur
labelReactNode-Input label
descriptionReactNode-Input description
errorReactNode-Error message
disabledbooleanfalseDisable input
placeholderstring-Input placeholder
sizeMantineSize’sm’Input size
localestring’en’dayjs locale