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.
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)
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;
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
.
import { DapDSDatePicker } from 'dap-design-system/dist/dds'
import { DapDSDatePickerReact } from 'dap-design-system/dist/react'
Property | Type | Default | Description |
---|---|---|---|
currentDate | Dayjs | dayjs() | The current visible date of the calendar. Only the month and year are considered. |
minDate | Dayjs | dayjs('1901-01-01') | The minimum date of the datepicker. This date will be the minimum selectable date. |
maxDate | Dayjs | dayjs('2099-12-31') | The maximum date of the datepicker. This date will be the maximum selectable date. |
disabledDate | (date:Dayjs) => boolean | (date: Dayjs) => false | The 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. |
opened | boolean | false | The open state of the datepicker. |
openCalendarOnInput | boolean | false | Wheter the calendar should open on typing. |
sendEmptyEventOnInput | boolean | false | Wheter the calendar should send an empty event on typing. |
placeholder | string , null | The placeholder of the datepicker. | |
loading | boolean , undefined | false | The loading state of the datepicker. |
format | string , undefined | The format of the datepicker. This is a comma separated list of formats. | |
formatSeparator | string | `' | '` |
clearButton | string , undefined | 'true' | Whether the clear button should be shown. |
clearButtonAriaLabel | string , undefined | The 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. | |
value | Dayjs | The value of the datepicker. | |
label | string | The label of the datepicker. | |
description | string | The description of the datepicker. | |
size | 'xs' , 'sm' , 'sm' | The size of the datepicker. | |
disabled | boolean | The disabled state of the datepicker. | |
required | boolean | The required state of the datepicker. | |
readonly | boolean | The readonly state of the datepicker. | |
autofocus | boolean | The autofocus of the datepicker. | |
tooltip | string | The tooltip of the datepicker. | |
tooltipPlacement | 'top' , 'right' , 'bottom' , 'left' | The tooltip placement of the datepicker. | |
feedback | string | The feedback of the datepicker. | |
feedbackType | 'negative' , 'positive' , 'warning' | The feedback type of the datepicker. | |
optional | boolean | The optional state of the datepicker. | |
optionalLabel | string | The optional label of the datepicker. | |
subtle | boolean | The weight of the label. Default is false | |
autocomplete | string | The autocomplete of the datepicker. |
No slots available.
Event Name | Description | Type |
---|---|---|
dds-change | Fired when the datepicker value changes. | {value: string } |
dds-input | Fired when the datepicker input value changes. | {value: string } |
dds-valid-date | Fired when the datepicker input value is valid. Happens on manual input typing. | {value: string } |
dds-invalid-date | Fired when the datepicker input value is invalid. Happens on manual input typing. | {value: string, type: 'invalid' , 'out-of-range' } |
dds-clear | Fired when the datepicker is cleared. | {void } |
dds-focus | Emitted when the datepicker gains focus. | {void } |
dds-blur | Emitted when the datepicker loses focus. | {void } |
Part Name | Description |
---|---|
base | The main datepicker container. |
calendar | The calendar of the datepicker. |
trigger | The trigger button of the datepicker. |
label | The label of the datepicker. |
description | The description of the datepicker. |
feedback | The feedback of the datepicker. |
tooltip | The tooltip of the datepicker. |
input | The input of the datepicker. |
clear-button | The clear button of the datepicker. |
Property Name | Description |
---|---|
--dds-combobox-background | The background color of the combobox. Defaults to var(--dds-fields-background-default). |
--dds-combobox-border-color | The border color of the combobox. Defaults to var(--dds-border-neutral-base). |
--dds-combobox-border-width | The border width of the combobox. Defaults to var(--dds-border-width-base, 1px). |
--dds-combobox-border-radius | The border radius of the combobox. Defaults to var(--dds-radius-base). |
--dds-combobox-text-color | The text color of the combobox. Defaults to var(--dds-text-neutral-base). |
--dds-combobox-placeholder-color | The placeholder text color. Defaults to var(--dds-text-neutral-subtle). |
--dds-combobox-disabled-background | The background color when disabled. Defaults to var(--dds-background-neutral-stronger). |
--dds-combobox-disabled-text | The text color when disabled. Defaults to var(--dds-text-neutral-disabled). |
--dds-combobox-error-border | The border color for error state. Defaults to var(--dds-border-negative-base). |
--dds-combobox-success-border | The border color for success state. Defaults to var(--dds-border-positive-base). |
--dds-combobox-icon-color | The color of the icons. Defaults to var(--dds-text-icon-neutral-subtle). |
--dds-combobox-clear-icon-color | The color of the clear icon. Defaults to var(--dds-button-subtle-icon-neutral-enabled). |
--dds-combobox-popup-background | The background color of the popup. Defaults to var(--dds-background-neutral-subtle). |
--dds-combobox-popup-shadow | The 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-xs | The padding for extra small size. Defaults to var(--dds-spacing-200). |
--dds-combobox-padding-sm | The padding for small size. Defaults to var(--dds-spacing-200). |
--dds-combobox-padding-lg | The padding for large size. Defaults to var(--dds-spacing-300). |
--dds-combobox-padding-horizontal | The horizontal padding. Defaults to var(--dds-spacing-300). |
--dds-combobox-padding-vertical | The vertical padding. Defaults to var(--dds-spacing-200). |
--dds-combobox-gap | The gap between elements. Defaults to var(--dds-spacing-100). |
--dds-combobox-icon-gap | The gap between icons. Defaults to var(--dds-spacing-200). |
--dds-combobox-action-gap | The gap between action elements. Defaults to var(--dds-spacing-200). |
--dds-combobox-action-padding | The padding for action elements. Defaults to var(--dds-spacing-300). |
--dds-combobox-clear-icon-width | The width of the clear icon. Defaults to var(--dds-spacing-800). |
--dds-combobox-dropdown-icon-right | The right position of the dropdown icon. Defaults to var(--dds-spacing-600). |
--dds-combobox-min-width | The minimum width of the combobox. Defaults to 7.5rem. |