Skip to content

Commit d77545d

Browse files
authored
Add locale and defaultDate options for Flatpickr (#1766)
* Add locale and defaultDate options for Flatpickr * Add "locale" * Adjust DateRangeFilterTest * Add CDN Option * Update documentation --------- Co-authored-by: lrljoe <[email protected]>
1 parent 73d761b commit d77545d

File tree

5 files changed

+106
-18
lines changed

5 files changed

+106
-18
lines changed

config/livewire-tables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
'dateFormat' => 'Y-m-d', // Date format that will be received by the filter
7373
'earliestDate' => null, // The earliest acceptable date
7474
'latestDate' => null, // The latest acceptable date
75+
'locale' => 'en', // The default locale
7576
],
7677
],
7778

docs/filter-types/filters-daterange.md

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function filters(): array
3131
'earliestDate' => '2020-01-01', // The earliest acceptable date
3232
'latestDate' => '2023-08-01', // The latest acceptable date
3333
'placeholder' => 'Enter Date Range', // A placeholder value
34+
'locale' => 'en',
3435
])
3536
->setFilterPillValues([0 => 'minDate', 1 => 'maxDate']) // The values that will be displayed for the Min/Max Date Values
3637
->filter(function (Builder $builder, array $dateRange) { // Expects an array.
@@ -42,31 +43,85 @@ public function filters(): array
4243
}
4344
```
4445

46+
## Configuration
47+
By default, this filter will inject the Flatpickr JS Library and CSS. However, you can customise this behaviour using the configuration file.
48+
49+
### Option 1 - The default behaviour:
50+
```
51+
'inject_third_party_assets_enabled' => true,
52+
```
4553

54+
### Option 2 - Bundled
55+
If you choose to bundle the Tables JS/CSS (recommended) by adding the following to your build process:
4656

47-
## Configuration
48-
By default, this filter will use a CDN to include the Flatpickr JS Library and CSS. However, you can customise this behaviour using the configuration file.
57+
```
58+
'vendor/rappasoft/laravel-livewire-tables/resources/js/laravel-livewire-tables-thirdparty.min.js';
59+
```
60+
61+
or in your app.js
4962

50-
### Option 1 - The default CDN behaviour:
5163
```
52-
'published_third_party_assets' => false,
53-
'remote_third_party_assets' => true,
64+
import '../../vendor/rappasoft/livewire-tables/resources/js/laravel-livewire-tables-thirdparty.min.js';
5465
```
5566

56-
### Option 2 - Publish included version
57-
You may publish the included version of Flatpickr. To do so, run:
67+
Then you should disable injection to avoid conflicts:
68+
5869
```
59-
php artisan vendor:publish --tag=livewire-tables-public
70+
'inject_third_party_assets_enabled' => false,
6071
```
61-
This will publish the tested version of Flatpickr to your public directory. You should then set
72+
73+
### Option 3 - CDN
74+
You must ensure that Flatpickr is present PRIOR to the tables loading. For example, to add Flatpickr with the Spanish locale, ensure that the following is present in your Layout head section. Noting the "async" presence to ensure that the script is present before a page renders.
75+
76+
It is typically recommended not to utilise the CDN approach, as changes to core code may impact behaviour, and you may need to implement changes to your CSP if present.
77+
78+
If using the CDN approach, ensure the following config matches:
6279
```
63-
'published_third_party_assets' => true,
64-
'remote_third_party_assets' => false,
80+
'inject_third_party_assets_enabled' => false,
6581
```
6682

67-
### Option 3 - Locally Installed
68-
If you have a locally installed version of Flatpickr already, you can set both options to false, and your local version will be used instead.
83+
Then include the following in your layout:
6984
```
70-
'published_third_party_assets' => false,
71-
'remote_third_party_assets' => false,
85+
// Flatpickr Core Script
86+
<script src="https://npmcdn.com/flatpickr" async></script>
87+
88+
// Flatpickr Core CSS
89+
<link rel="stylesheet" href="https://npmcdn.com/flatpickr/dist/flatpickr.min.css">
90+
```
91+
92+
### Option 4 - Locally Installed
93+
If you have a locally installed version of Flatpickr already, you can set injection to false, and your local version will be used instead.
94+
```
95+
'inject_third_party_assets_enabled' => false,
96+
```
97+
98+
## Localisation (BETA)
99+
The default installation includes only the English (en) locale.
100+
101+
### Bundling
102+
Should you wish to localise, you must include the Flatpickr locale files in your build pipeline. This applies to only the specific locales that you require in your app.js (requires adding the flatpickr library to your package.json by executing "npm i flatpickr --save")
103+
```
104+
import { Arabic } from "../imports/flatpickr/l10n/ar.js";
105+
import { Catalan } from "../imports/flatpickr/l10n/cat.js";
106+
import { Danish } from "../imports/flatpickr/l10n/da.js";
107+
import { German } from "../imports/flatpickr/l10n/de.js";
108+
import { Spanish } from "../imports/flatpickr/l10n/es.js";
109+
import { French } from "../imports/flatpickr/l10n/fr.js";
110+
import { Indonesian } from "../imports/flatpickr/l10n/id.js";
111+
import { Italian } from "../imports/flatpickr/l10n/it.js";
112+
import { Malaysian } from "../imports/flatpickr/l10n/ms.js";
113+
import { Dutch } from "../imports/flatpickr/l10n/nl.js";
114+
import { Portuguese } from "../imports/flatpickr/l10n/pt.js";
115+
import { Russian } from "../imports/flatpickr/l10n/ru.js"
116+
import { Thai } from "../imports/flatpickr/l10n/th.js"
117+
import { Turkish } from "../imports/flatpickr/l10n/tr.js"
118+
import { Ukrainian } from "../imports/flatpickr/l10n/uk.js"
119+
```
120+
121+
### CDN
122+
You can also add locales using the Flatpickr CDN, ensuring that these are loaded before the page renders.
123+
124+
For example to add German (de), ensure that the following is in the "head" section of your layout, ideally before your app.js
72125
```
126+
<script src="https://npmcdn.com/flatpickr/dist/l10n/de.js" async></script>
127+
```

resources/js/laravel-livewire-tables.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ document.addEventListener('alpine:init', () => {
117117
mode: 'range',
118118
clickOpens: true,
119119
allowInvalidPreload: true,
120-
defaultDate: [],
120+
defaultDate: filterConfig['defaultDate'] ?? [],
121121
ariaDateFormat: filterConfig['ariaDateFormat'],
122122
allowInput: filterConfig['allowInput'],
123123
altFormat: filterConfig['altFormat'],
124124
altInput: filterConfig['altInput'],
125125
dateFormat: filterConfig['dateFormat'],
126-
locale: 'en',
126+
locale: filterConfig['locale'] ?? 'en',
127127
minDate: filterConfig['earliestDate'],
128128
maxDate: filterConfig['latestDate'],
129129
onOpen: function () {

resources/js/laravel-livewire-tables.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)