Datepicker

The Datepicker component is a form element that allows users to select a date from a calendar. It is used to input a date in a user-friendly way.

Design system docs Examples Default Datepicker

The default Datepicker is a form element with a label, placeholder, and description. It wraps a dap-ds-calendar into a dropdown component, so all of the calendar's attributes are available.

First, you need to import the day js localization handler plugins, it is recommended to do this in the root component of your application:

import 'dayjs/locale/hu'
import 'dayjs/locale/en'

import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat' // ES 2015
import localeData from 'dayjs/plugin/localeData'
import LocalizedFormat from 'dayjs/plugin/localizedFormat'

dayjs.extend(localeData)
dayjs.extend(LocalizedFormat)
dayjs.extend(customParseFormat)

<div style={{ display: 'flex', minHeight: 200 }}>
<dap-ds-datepicker 
  label="Birth date"
  description="Choose your birth date"
>
</dap-ds-datepicker>
</div>
Sizes
<div style={{ display: 'flex', minHeight: 200 }}>
<dap-ds-stack>
<dap-ds-datepicker 
  size="xs"
  label="Birth date"
  feedback="This is a feedback message"
  feedbacktype="negative"
  description="Choose your birth date"
>
</dap-ds-datepicker>
<dap-ds-datepicker 
  size="sm"
  label="Birth date"
  feedback="This is a feedback message"
  feedbacktype="negative"
  description="Choose your birth date"
>
</dap-ds-datepicker>
<dap-ds-datepicker 
  size="lg"
  label="Birth date"
  feedback="This is a feedback message"
  feedbacktype="negative"
  description="Choose your birth date"
>
</dap-ds-datepicker>
</dap-ds-stack>
</div>
Validation

The datepicker will emit a dds-valid-date custom event if the input is a valid date, and a dds-invalid-date custom event if the input is not a valid date. You can listen for these events to validate the input, and check the error type with the type property of the event detail. By default the datepicker will validate against the default date format of the current locale. You can set a custom date format with the format attribute. The format attribute accepts a string separated with '|' character, or you can set the separator character with the formatSeparator attribute. The displayed format is the first format in the string, and the validation is done against all formats in the string. For hungarian locale the default date format is YYYY.MM.DD., for english it is MM/DD/YYYY. To get the current locale date format you, can extend your DayJs instance with the localeData, LocalizedFormat and customParseFormat plugins. To get the current local date format use: const format = dayjs.Ls[dayjs.locale()].formats.L

HTML

Js

datepicker.addEventListener('dds-valid-date', e => {
  console.log('Valid Value:', e.detail)
  datepicker.feedback = 'valid'
  datepicker.status = 'valid'
  datepicker.feedbackType = 'positive'
})

datepicker.addEventListener('dds-invalid-date', e => {
  console.log('dds-invalid-date', event.detail.value)

  datepicker2.status = 'error'
  datepicker2.feedbackType = 'negative'

  if(event.detail.type === 'invalid') {
    datepicker2.feedback = 'Invalid date'
  }

  if(event.detail.type === 'out-of-range') {
    datepicker2.feedback = 'Date is out of range'
  }
})

datepicker.addEventListener('dds-change', e => {
  console.log('Value:', e.detail)
})

React

import { DapDSButtonReact, DapDSDatePickerReact } from 'dap-design-system/dist/react'
import { Controller, useForm } from 'react-hook-form';
import dayjs from 'dayjs'

function App() {
  const {
    control,
    handleSubmit,
    setValue,
    setError,
    formState: { errors },
  } = useForm()

  const onSubmit = (data: any) => {
    console.log('data', data)
  }
  
  return (
    <div className="App">
    <form onSubmit={handleSubmit(onSubmit)} noValidate>
      <Controller
        name="datepicker"
        control={control}
        render={({ field: { value } }) => (
          <DapDSDatePickerReact
            id="datepicker"
            label="Birth date"
            description="Your birth date"
            required
            name="textInput"
            value={value}
            feedback={errors?.datepicker?.message?.toString()}
            feedbackType={errors?.datepicker ? 'negative' : 'positive'}
            onDdsChange={(e) => {
              console.log(e)
              setValue('datepicker', e.detail.value, { shouldValidate: true })}
            }
            onDdsInvalidDate={(e) => {
              console.log(e)
              if (e.detail.type === 'invalid') {
                setError('datepicker',
                  { message: `Invalid date: ${dayjs.Ls[dayjs.locale()].formats.L}` })
              }

              if (e.detail.type === 'out-of-range') {
                setError('datepicker', { message: 'Date out of range' })
              }
            }}
            onDdsValidDate={(e) => {
              console.log(e)
              setError('datepicker', { message: '' })
            }}
          >
          </DapDSDatePickerReact>
        )}
        rules={{
          validate: {
            required: value => {
              if (!value) return '*Required'
            },
          },
        }}
      />
      <DapDSButtonReact htmlType="submit">Submit</DapDSButtonReact>
    </form>
    </div>
  );
}

export default App;
Localization

The locale property can be used to set the locale of the datepicker. The default locale is the current locale of the browser. The available locales are en and hu.

<div style={{ display: 'flex', minHeight: 200 }}>
<dap-ds-stack>
  <dap-ds-datepicker 
    locale="hu"
    label="Születési dátum"
    feedback="Nem ekkor születtél"
    feedbacktype="negative"
    description="Válassz születési dátumot"
  >
  </dap-ds-datepicker>
  <dap-ds-datepicker 
    locale="en"
    label="Birth date"
    feedback="You were not born then"
    feedbacktype="negative"
    description="Choose your birth date"
  >
  </dap-ds-datepicker>
</dap-ds-stack>
</div>
Importing
import { DapDSDatePicker } from 'dap-design-system/dist/dds'
Importing React
import { DapDSDatePickerReact } from 'dap-design-system/dist/react'
Attributes
PropertyTypeDefaultDescription
currentDateDayjsdayjs()The current visible date of the calendar. Only the month and year are considered.
minDateDayjsdayjs('1901-01-01')The minimum date of the datepicker. This date will be the minimum selectable date.
maxDateDayjsdayjs('2099-12-31')The maximum date of the datepicker. This date will be the maximum selectable date.
disabledDate(date:Dayjs) => boolean(date: Dayjs) => falseThe disabled date of the datepicker. This function will be called for each date to determine if it is disabled.
placement'top-start', 'top' , 'top-end' , 'right-start' , 'right' , 'right-end' , 'bottom-start' , 'bottom' , 'bottom-end' , 'left-start' , 'left' , 'left-end''bottom-start'The placement of the dropdown of the datepicker.
openedbooleanfalseThe open state of the datepicker.
openCalendarOnInputbooleanfalseWheter the calendar should open on typing.
sendEmptyEventOnInputbooleanfalseWheter the calendar should send an empty event on typing.
placeholderstring, nullThe placeholder of the datepicker.
loadingboolean, undefinedfalseThe loading state of the datepicker.
formatstring, undefinedThe format of the datepicker. This is a comma separated list of formats.
formatSeparatorstring`''`
clearButtonstring, undefined'true'Whether the clear button should be shown.
clearButtonAriaLabelstring, undefinedThe aria label of the clear button.
locale'hu', 'en'The locale of the datepicker. By default it uses the global locale from dayjs. It is determined from the browser language.
valueDayjsThe value of the datepicker.
labelstringThe label of the datepicker.
descriptionstringThe description of the datepicker.
size'xs', 'sm' , 'sm'The size of the datepicker.
disabledbooleanThe disabled state of the datepicker.
requiredbooleanThe required state of the datepicker.
readonlybooleanThe readonly state of the datepicker.
autofocusbooleanThe autofocus of the datepicker.
tooltipstringThe tooltip of the datepicker.
tooltipPlacement'top', 'right' , 'bottom' , 'left'The tooltip placement of the datepicker.
feedbackstringThe feedback of the datepicker.
feedbackType'negative', 'positive' , 'warning'The feedback type of the datepicker.
optionalbooleanThe optional state of the datepicker.
optionalLabelstringThe optional label of the datepicker.
subtlebooleanThe weight of the label. Default is false
autocompletestringThe autocomplete of the datepicker.
Slots

No slots available.

Events
Event NameDescriptionType
dds-changeFired when the datepicker value changes.{value: string }
dds-inputFired when the datepicker input value changes.{value: string }
dds-valid-dateFired when the datepicker input value is valid. Happens on manual input typing.{value: string }
dds-invalid-dateFired when the datepicker input value is invalid. Happens on manual input typing.{value: string, type: 'invalid' , 'out-of-range' }
dds-clearFired when the datepicker is cleared.{void }
dds-focusEmitted when the datepicker gains focus.{void }
dds-blurEmitted when the datepicker loses focus.{void }
CSS Parts
Part NameDescription
baseThe main datepicker container.
calendarThe calendar of the datepicker.
triggerThe trigger button of the datepicker.
labelThe label of the datepicker.
descriptionThe description of the datepicker.
feedbackThe feedback of the datepicker.
tooltipThe tooltip of the datepicker.
inputThe input of the datepicker.
clear-buttonThe clear button of the datepicker.
CSS Custom Properties
Property NameDescription
--dds-combobox-backgroundThe background color of the combobox. Defaults to var(--dds-fields-background-default).
--dds-combobox-border-colorThe border color of the combobox. Defaults to var(--dds-border-neutral-base).
--dds-combobox-border-widthThe border width of the combobox. Defaults to var(--dds-border-width-base, 1px).
--dds-combobox-border-radiusThe border radius of the combobox. Defaults to var(--dds-radius-base).
--dds-combobox-text-colorThe text color of the combobox. Defaults to var(--dds-text-neutral-base).
--dds-combobox-placeholder-colorThe placeholder text color. Defaults to var(--dds-text-neutral-subtle).
--dds-combobox-disabled-backgroundThe background color when disabled. Defaults to var(--dds-background-neutral-stronger).
--dds-combobox-disabled-textThe text color when disabled. Defaults to var(--dds-text-neutral-disabled).
--dds-combobox-error-borderThe border color for error state. Defaults to var(--dds-border-negative-base).
--dds-combobox-success-borderThe border color for success state. Defaults to var(--dds-border-positive-base).
--dds-combobox-icon-colorThe color of the icons. Defaults to var(--dds-text-icon-neutral-subtle).
--dds-combobox-clear-icon-colorThe color of the clear icon. Defaults to var(--dds-button-subtle-icon-neutral-enabled).
--dds-combobox-popup-backgroundThe background color of the popup. Defaults to var(--dds-background-neutral-subtle).
--dds-combobox-popup-shadowThe box shadow of the popup. Defaults to 0 4px 6px -1px rgb(0 0 0 / 8%), 0 2px 4px -1px rgb(0 0 0 / 6%).
--dds-combobox-padding-xsThe padding for extra small size. Defaults to var(--dds-spacing-200).
--dds-combobox-padding-smThe padding for small size. Defaults to var(--dds-spacing-200).
--dds-combobox-padding-lgThe padding for large size. Defaults to var(--dds-spacing-300).
--dds-combobox-padding-horizontalThe horizontal padding. Defaults to var(--dds-spacing-300).
--dds-combobox-padding-verticalThe vertical padding. Defaults to var(--dds-spacing-200).
--dds-combobox-gapThe gap between elements. Defaults to var(--dds-spacing-100).
--dds-combobox-icon-gapThe gap between icons. Defaults to var(--dds-spacing-200).
--dds-combobox-action-gapThe gap between action elements. Defaults to var(--dds-spacing-200).
--dds-combobox-action-paddingThe padding for action elements. Defaults to var(--dds-spacing-300).
--dds-combobox-clear-icon-widthThe width of the clear icon. Defaults to var(--dds-spacing-800).
--dds-combobox-dropdown-icon-rightThe right position of the dropdown icon. Defaults to var(--dds-spacing-600).
--dds-combobox-min-widthThe minimum width of the combobox. Defaults to 7.5rem.