Skip to content

Commit 9854ae6

Browse files
committed
Allow setting options per field
1 parent ac8ada3 commit 9854ae6

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ The Flatpickr directive is inserted via a block `loki.directives.flatpickr`. If
3939
</referenceBlock>
4040
```
4141

42+
Instead of a global configuration, options can also be set per field:
43+
```xml
44+
<block name="example" template="Loki_FieldComponents::form/field.phtml">
45+
<arguments>
46+
<argument name="input_type" xsi:type="string">date</argument>
47+
<argument name="field_attributes" xsi:type="array">
48+
<item name="x-flatpickr" xsi:type="array">
49+
<item name="altFormat" xsi:type="string">d-m-Y</item>
50+
</item>
51+
</argument>
52+
</arguments>
53+
</block>
54+
```
55+
4256
For all options, see [https://flatpickr.js.org/options/](https://flatpickr.js.org/options/)
4357

4458
Note that changing the `dateFormat` will also change the value sent to Magento. It is probably best to keep this at its default (`Y-m-d`).
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22
declare(strict_types=1);
33

4-
/** @version 1.0.6 */
5-
64
use Loki\Components\Factory\ViewModelFactory;
75
use Loki\Flatpickr\ViewModel\Options;
86
use Magento\Framework\View\Element\Template;
97

8+
/** @version 1.0.6 */
109
/** @var Template $block */
1110
/** @var ViewModelFactory $viewModelFactory */
1211
/** @var Options $options */
1312
$options = $viewModelFactory->create(Options::class);
1413
?>
1514
<script>
1615
document.addEventListener('alpine:init', () => {
17-
Alpine.directive('flatpickr', (el, {}) => {
18-
flatpickr(el, JSON.parse('<?= /* @noEscape */ $options->toJson($block) ?>'));
16+
Alpine.directive('flatpickr', (el, {expression}) => {
17+
const options = Object.assign(
18+
JSON.parse('<?= /* @noEscape */ $options->toJson($block) ?>'),
19+
expression.length > 0 ? JSON.parse(expression) : {},
20+
);
21+
22+
flatpickr(el, options);
1923
});
2024
});
2125
</script>

0 commit comments

Comments
 (0)