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
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>
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;
import { DapDSDatePicker } from 'dap-design-system/dist/dds'
import { DapDSDatePickerReact } from 'dap-design-system/dist/react'
No slots available.
Property | Type | Default | Description |
---|---|---|---|
value | Dayjs | The value of the datepicker. | |
format | string | The format of the datepicker. | |
currentDate | Dayjs | The current date of the datepicker. | |
minDate | Dayjs | The minimum date of the datepicker. | |
maxDate | Dayjs | The maximum date of the datepicker. | |
disabledDate | Function | The disabled date of the datepicker. | |
placement | PopupPlacement | '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'. |
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. | |
size | 'sm' 'lg' | The size of the datepicker. Default is 'md'. | |
loading | boolean | false | The loading state 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. | |
label | string | The label of the datepicker. | |
description | string | The description of the datepicker. | |
tooltip | string | The tooltip of the datepicker. | |
feedback | string | The feedback of the datepicker. | |
feedbackType | 'error' 'warning' 'info' | 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. | |
clearButton | string | 'true' | Whether the clear button should be shown. |
Event Name | Description |
---|---|
dds-change | Fired when the datepicker value changes. |
dds-input | Fired when the datepicker input value changes. |
dds-valid-date | Fired when the datepicker input value is valid. |
dds-invalid-date | Fired when the datepicker input value is invalid. Type can be 'invalid' or 'out-of-range'. |
dds-clear | Fired when the datepicker is cleared. |
dds-focus | Emitted when the datepicker gains focus. |
dds-blur | Emitted when the datepicker loses focus. |
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. |