https://github.com/laratipsofficial/laravel-flatpickr
A very simple Laravel package that converts the Javascript Flatpickr to a blade component.
https://github.com/laratipsofficial/laravel-flatpickr
date datetime flatpickr javascript laravel php
Last synced: about 1 month ago
JSON representation
A very simple Laravel package that converts the Javascript Flatpickr to a blade component.
- Host: GitHub
- URL: https://github.com/laratipsofficial/laravel-flatpickr
- Owner: Laratipsofficial
- License: mit
- Created: 2022-02-12T13:26:25.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T23:36:26.000Z (over 1 year ago)
- Last Synced: 2024-04-30T00:37:08.552Z (over 1 year ago)
- Topics: date, datetime, flatpickr, javascript, laravel, php
- Language: PHP
- Homepage: https://www.youtube.com/c/laratips
- Size: 1.12 MB
- Stars: 61
- Watchers: 2
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# A Laravel clone of the Javascript Flatpickr (Date picker) library
[](https://packagist.org/packages/asdh/laravel-flatpickr)
[](https://github.com/Laratipsofficial/laravel-flatpickr/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/Laratipsofficial/laravel-flatpickr/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/asdh/laravel-flatpickr)Using this package you can add a beautiful date or datetime picker into your project without touching any javascript with the power or laravel component. It is just a laravel component wrapper for the [Flatpickr](https://flatpickr.js.org/) javascript library.
![]()
Created with ❤️ from Nepal 🇳🇵
## Support
You can support me by subscribing to my [YouTube channel - Laratips](https://www.youtube.com/c/Laratips).
If you want me to continue developing this package and want me to develop other similar packages, then you help me financially by sending few bucks to my [Wise](https://wise.com/invite/ath/ashishd233) account in Nepalese 🇳🇵 currency.
My Wise email: ashish.dhamala2015@gmail.com
If you decide to support me, the please send me your twitter handle in mail so that I can shout-out about you on twitter.
## Installation
You can install the package via composer:
```bash
composer require asdh/laravel-flatpickr
```You can publish the config file with:
```bash
php artisan vendor:publish --tag="flatpickr-config"
```You can publish the assets with:
```bash
php artisan vendor:publish --tag="flatpickr-assets"
```This is the contents of the published config file:
```php
return [
/**
* The url to be used to serve css file.
* If null, it will use the one shipped with package.
*/
'css_url' => env('FLATPICKR_CSS_URL', null),/**
* The url to be used to serve js file.
* If null, it will use the one shipped with package.
*/
'js_url' => env('FLATPICKR_JS_URL', null),/**
* Determines if the styles shipped with the package should be used.
* Setting it to false will remove the styling for the component.
* The flatpickr css will be untouched.
*/
'use_style' => env('FLATPICKR_USE_STYLE', true),/**
* The language that flatpickr will use.
* If no value is passed, it will be the default one.
*/
'locale' => env('FLATPICKR_LOCALE', null),
];
```## Usage
You need to include the css and js that ships with the package in your html or blade file.
### Adding Css
Include this style at the head section of your page:
```php
@include('flatpickr::components.style')
```Or you can use laravel blade component syntax:
```html
```
If you want to use different `url` for the css then you can change it from the .env file:
```env
FLATPICKR_CSS_URL=https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.9/flatpickr.min.css
```You can even change the `url` from the component itself:
```html
```
The `url` passed form the component will take more priority over the config file.
### Adding Js
Similarly include this script at the bottom of your page:
```php
@include('flatpickr::components.script')
```Or you can use laravel blade component syntax:
```html
```
If you want to use different `url` for the js then you can change it from the .env file:
```env
FLATPICKR_JS_URL=https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.9/flatpickr.min.js
```You can even change the `url` from the component itself:
```html
```
The `url` passed form the component will take more priority over the config file.
### Locale
If you want to use a different locale then you can change it from the .env file:
```env
FLATPICKR_LOCALE=de
```You can see the [available locales from here.](https://github.com/flatpickr/flatpickr/tree/master/src/l10n)
### Using the component
Add it to your page.
```html
```
Yes, it's that simple. Now you have a beautiful looking date picker in your page without touching a single javascript at all.
![]()
## Component props
I have made different `props` for this component that will be converted into the `config` options of the `flatpickr`. Make sure to look into the [config options](https://flatpickr.js.org/options) of the flatpickr.
Most of the description of the props written here are taken from the flatpickr documentation page.
### id
**Type:** string
Set the id of the component. It will apply id to the underlying `input` tag. If no id is provided, it will use the autogenerated id.
**Example:**
```html
```
### dateFormat
**Type:** string
A string of characters which are used to define how the date will be displayed in the input box. Please check the [flatpickr documentation](https://flatpickr.js.org/formatting/) page for the supported characters. By default the date format is `Y-m-d` but you can change it to other formats to like `d/m/Y`.
**Example:**
```html
```
### altFormat
**Type:** string
Exactly the same as date format, but for the altInput field. If you want different format to be visible for the user then you can use this. By default it will use the same format as that of `dateFormat`.
**Example:**
```html
```
### minDate
**Type:** string|Carbon
The minimum date that a user can start picking from.
You can pass a `Carbon` instance or a date format in `string` that is supported by `Carbon` or `DateTime`.
**Example:**
```html
OR
OR
```
### maxDate
**Type:** string|Carbon
The maximum date that a user can pick to.
You can pass a `Carbon` instance or a date format in `string` that is supported by `Carbon` or `DateTime`.
**Example:**
```html
OR
OR
```
### showTime
**Type:** bool
Shows the time picker.
**Example:**
```html
```
### timeFormat
**Type:** string
A string of characters which are used to define how the time will be displayed in the input box. Please check the [flatpickr documentation](https://flatpickr.js.org/formatting/) page for the supported characters. By default the time format is `H-i` but you can change it to other formats to like `h:i`.
**Example:**
```html
```
When you use `show-time` prop with `alt-format`, make sure to write both date and time format in the `alt-format` like this:
**Example:**
```html
```
### minTime
**Type:** string
The minimum time that a user can start picking from.
**Example:**
```html
```
### maxTime
**Type:** string
The maximum time that a user can pick to.
**Example:**
```html
```
### time24hr
**Type:** bool
Displays time picker in 24 hour mode without AM/PM selection when enabled. By default it is set to true. To show in 12 hour mode, set it to false.
**Example:**
Displays the time picker in 12 hour mode with am and pm.
```html
```
### firstDayOfWeek
**Type:** int
Sets when the first day of the calendar should start. By default it is 0 (Sunday).
**Example:**
It sets the first day of the week as Monday.
```html
```
### disableWeekend
**Type:** bool
Disable the weekend in the calendar. Saturday and Sunday are disabled.
**Example:**
```html
```
![]()
### disable
**Type:** array
Disable the provided dates. It can be array of `date string` or `Carbon`.
**Example:**
```html
OR
```
### enable
**Type:** array
Only enable the provided dates. It can be array of `date string` or `Carbon`. All the other dates other than provided in the `enable` array will be disabled when used.
**Example:**
```html
OR
```
### multiple
**Type:** bool
Sets the calendar mode to `multiple`. You will be able to select multiple dates.
**Example:**
```html
```
![]()
### range
**Type:** bool
Sets the calendar mode to `range`. You will be able to select range of dates. If you use both `multiple` and `range`, the mode will be set to `range`.
By default 2 months will be visible in the dropdown when the mode is `range`. You can change that using `visibleMonths` prop.
**Example:**
```html
```
![]()
### visibleMonths
**Type:** int
The number of months to be shown at the same time when displaying the calendar.
**Example:**
```html
```
![]()
### inline
**Type:** bool
Displays the calendar inline.
**Example:**
```html
```
### showWeekNumbers
**Type:** bool
Shows week numbers in calendar.
**Example:**
```html
```
### value
**Type:** string|Carbon|array
Sets the initial selected date(s).
**Example:**
For a single picker, pass normal date string or `Carbon` instance.
```html
OR
```
**Example**
For multiple picker, pass the data as array.
```html
OR
```
**Example**
For range picker, pass the 2 dates as string separated by `to` in between. Or pass 2 dates as array and it will select all the dates between and including them.
```html
OR
```
### clearable
**Type:** bool
Shows a clear icon on the right side of the date picker. Clicking on it will clear the selected value.
**Example:**
```html
```
![]()
## Event Hooks
You can pass all the event hooks present in the `flatpickr` library like `onChange`, `onOpen`, `onClose`, etc. Please check all the hooks available in their [documentation page](https://flatpickr.js.org/events/#hooks).
**Example:**
```html
function handleChange(selectedDates, dateStr, instance) {
console.log({ selectedDates, dateStr, instance });
}```
## Testing
```bash
composer test
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Laratips](https://github.com/Laratipsofficial)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.