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.

<div style={{ display: 'flex', minHeight: 200 }}>
  <dap-ds-datepicker 
    label="Birth date"
    description="Choose your birth date"
  >
  </dap-ds-datepicker>
</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;
Importing
import { DapDSDatePicker } from 'dap-design-system/dist/dds'
Importing React
import { DapDSDatePickerReact } from 'dap-design-system/dist/react'
Slots

No slots available.

Attributes
PropertyTypeDefaultDescription
valueDayjsThe value of the datepicker.
formatstring The format of the datepicker.
currentDateDayjsThe current date of the datepicker.
minDateDayjsThe minimum date of the datepicker.
maxDateDayjsThe maximum date of the datepicker.
disabledDateFunctionThe disabled date of the datepicker.
placementPopupPlacement'bottom-start'The placement of the datepicker. Can be 'top-start', 'top', 'top-end', 'right-start', 'right', 'right-end', 'bottom-start', 'bottom', 'bottom-end', 'left-start', 'left', or 'left-end'. Default is 'bottom-start'.
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.
size'sm' 'lg'The size of the datepicker. Default is 'md'.
loadingboolean falseThe loading state 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.
labelstringThe label of the datepicker.
descriptionstringThe description of the datepicker.
tooltipstringThe tooltip of the datepicker.
feedbackstringThe feedback of the datepicker.
feedbackType'error' 'warning' 'info'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.
clearButtonstring 'true'Whether the clear button should be shown.
Events
Event NameDescription
dds-changeFired when the datepicker value changes.
dds-inputFired when the datepicker input value changes.
dds-valid-dateFired when the datepicker input value is valid.
dds-invalid-dateFired when the datepicker input value is invalid. Type can be 'invalid' or 'out-of-range'.
dds-clearFired when the datepicker is cleared.
dds-focusEmitted when the datepicker gains focus.
dds-blurEmitted when the datepicker loses focus.
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.