Date Picker

{note} You're browsing the documentation for an old version of laravel-form-components. Consider upgrading your project to v8. Check your version with the following command:

composer show rawilk/laravel-form-components

The date-picker component offers an integration with the flatpickr datepicker library. By using it, you can simply add a date and/or time picker to your form with one component.

While the date-picker component works out-of-the-box when you've set the directives, we recommend that you install and compile the JavaScript libraries before you deploy to production:

Make sure you import flatpickr as flatpickr in your JavaScript, and make sure it's available globally:

import flatpickr from 'flatpickr';

window.flatpickr = flatpickr;

If you pull the flatpickr package in via npm, you should import the styles into your stylesheet:

@import 'flatpickr/dist/flatpickr.min.css';

{note} Be sure these styles are imported before the styles for this package so we can override the styles for flatpickr correctly.

In its most basic usage, you can use it as a self-closing component and pass it a name:

<x-date-picker name="birthday" />

The component sets a couple of nice defaults for your date picker. The placeholder is automatically set the default format used by flatpickr, but this can easily be changed by passing in the placeholder to the component.

By default, the date picker is set to only open when you click the toggle icon, and it allows you to type in the input as well. There is also a clear icon that gets inserted by default as well. All of these options can be toggled on the component.

By default, a toggle button with a calendar icon is prepended to the input. Clicking this button will show the calendar that flatpickr generates. If you want to prevent the button/icon from being displayed, you can set the value to false:

<x-date-picker :toggle-icon="false" />

{note} If you disable the toggle button, be sure to set click-opens to true on the component.

{tip} You can also change the icon that is used for the toggle button either by setting the toggle-icon attribute, or by changing it globally in the config.

The date picker component provides a way to clear the selected date out of the box. All you need to do is set clearable to true and an icon for clearing the input will be appended to the input.

<x-date-picker name="birthday" clearable />

The icon defaults to heroicon-o-x-circle, but you can easily customize this icon either in the config, or by setting the clear-icon attribute to the icon you want.

You can pass in options to the date picker via the options attribute. This requires you to pass a PHP array with scalar values. Below is an example where we set mode to "multiple" instead of the default "single" value:

<x-date-picker name="birthday" :options="['mode' => 'multiple']" />

For a full reference of all options, please consult the flatpickr documentation.

For convenience, we have added a click-opens (clickOpens option) boolean attribute to the component to easily toggle whether clicking on the input should open the picker. By default, the component sets this value to false to allow typing into the input. If you enable this option, be aware that you might lose the ability to type into the input.

The boolean allow-input (allowInput option) attribute has been added for convenience to easily toggle whether the user is allowed to enter a date directly into the input field. By default, the component sets this value to true.

The boolean enable-time (enableTime option) attribute has been added for convenience to allow for a time picker to be available as well. By default, the component sets this value to false.

If you pass a format (e.g. Y-m-d) attribute to the component, it will set the dateFormat option on flatpickr. This option defines how the date is displayed in the input, but also how it is sent to the server.

{note} Please note that only scalar values are supported. You cannot use any JavaScript language specific options like callbacks.

Since the options attribute only accepts scalar values, the component offers a optionsSlot slot that will allow you to specify any option callbacks you need to:

<x-date-picker name="birthday">
    <x-slot name="optionsSlot">
        onChange: (selectedDates, dateStr, instance) => {
            // ...

In the example above, we are injecting a callback for the onChange event fired by flatpickr into the flatpickr options object. For more information on the callbacks available, please consult the events api.

By default, the date picker component defines a callback for the onOpen event fired by flatpickr. As of version 6.0.1, you may utilize the onOpen slot to add your own callback function if needed on the component.

<x-date-picker name="birthday">
    <x-slot name="onOpen">
        function (selectedDates, dateStr, instance) {
            // do something

Like the other inputs, the date picker can also have leading and trailing addons, however by default you cannot add them. To add leading addons, you must disable the toggle icon, and for trailing addons, you must set clearable to false.

See the input documentation for more information.

Caught a mistake? Suggest an edit on GitHub