const dateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
};
<input name="normalDatepicker" use:datePicker={dateOptions} readonly/>
adding `defaultDate` to options
const defaultDateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
defaultDate: '2023-12-30',
};
<input name="datepickerDefaultDate" use:datePicker={defaultDateOptions} readonly/>
adding `minDate` and/or `maxDate` to options and passing date string or date object as a value
`maxDate` will extended a maximum year you can select in year dropdown if it greater than current year.
const minMaxDateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
minDate: '2019-03',
maxDate: '2033-12'
};
<input name="datepickerMinMaxDate" use:datePicker={minMaxDateOptions} readonly />
adding `disable` to options and passing date string or date object as a value
const disabledOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
disable: ['2023-08-30', '2023-07-21', '2023-06-08', new Date(2023, 11, 30)]
};
<input name="datepickerDisableDate" use:datePicker={disabledOptions} readonly />
in this example is using only `onChange`, `onOpen` and `onClose` but you can adding any flatpickr hook to the options. (you can open console to see the hook is working)
const hookDateOptions = {
...dateOptions,
onChange: (selectedDates, dateStr) => {
console.log('on change hook!');
console.log(selectedDates[0]);
console.log(dateStr);
},
onOpen: (selectedDates, dateStr) => {
console.log('on open hook!');
},
onClose: (selectedDates, dateStr) => {
console.log('on close hook!');
}
};
<input name="datepickerHookDate" use:datePicker={hookDateOptions} readonly />
also like a datepicker with hook you can using svelte's `on` directive with a name of hook too.
you can access to a data of hook by using `event.detail` (you can open console to see the event is working)
const dateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y'
};
const changeHandler = (event) => {
if (event.detail) {
const { selectedDates, dateStr, instance } = event.detail;
console.log('event change!', { selectedDates, dateStr, instance });
}
};
const openHandler = (event) => {
console.log('event open!');
};
const closeHandler = (event) => {
console.log('event close!');
};
<input name="datepickerOnDirectiveDate"
use:datePicker={dateOptions}
on:change={changeHandler}
on:open={openHandler}
on:close={closeHandler}
readonly
/>
You can binding a element and using `._flatpickr` to access to flatpickr instance
const defaultDateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
};
let datepickerElement;
const handdleDatepickerBindClick = () => {
datepickerElement._flatpickr.clear();
};
<input name="normalDatepickerBind" use:datePicker={dateOptions} bind:this={datepickerElement} readonly/>
<button type="button" on:click={handdleDatepickerBindClick}>Clear</button>
You can using a datepicker action on wrapper element such as `div` by passing `wrap: true` as a additional option and adding `data-input` as a attribute to input element.
const wrapOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
wrap:true
};
<div use:datePicker={wrapOptions}>
<input name="wrapperDatepicker" data-input />
<button type="button" title="toggle" data-toggle> toggle </button>
<button type="button" title="clear" data-clear> clear </button>
</div>
In this example will using Thai language by adding `locale: Thai` to options while `Thai` is imported from `flatpickr_plus/dist/l10n/th.js` and using Buddhist era by adding`useLocaleYear: true` to options
const dateOptions = {
dateFormat: 'Z',
altFormat: 'd F Y',
altInput: true,
ariaDateFormat: 'd F Y',
locale: Thai,
useLocaleYear: true
};
import { Thai } from 'flatpickr_plus/dist/l10n/th.js';
<input name="localizeDatepicker" use:datePicker={localizeOptions} readonly />
Adding 'noCalendar' and 'enableTime' to display only time picker.
const dateOptions = {
defaultDate: '12:30',
enableTime: true,
noCalendar: true,
dateFormat: 'H:i',
};
<input name="timedatepicker" use:datePicker={timeOptions} readonly />
you can checking this usage at this REPL