Skip to content

Commit

Permalink
feat: Add compability with Alpine 3
Browse files Browse the repository at this point in the history
I added compatibility with Alpine 3, by default it is still loading the
v2.29 of Alpine but this customizable to import Alpine manually.

- I moved the Pickaday and moment dependencias to import them by a CDN
  since with alpine 3 the `init` method is called automatically when the
component is rendered
  • Loading branch information
Gustavinho committed Jun 22, 2021
1 parent a1c5284 commit ede4986
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 56 deletions.
2 changes: 1 addition & 1 deletion public/laravel-views.js

Large diffs are not rendered by default.

30 changes: 3 additions & 27 deletions resources/js/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
import feather from 'feather-icons'
import Pikaday from 'pikaday'

try {
const setUpUiLibraries = () => {
feather.replace()
}

const datePicker = (id) => {
return {
picker: null,
id: null,
dispatch: null,
init (id, dispatch) {
this.id = id
this.dispatch = dispatch
this.picker = new Pikaday({
field: this.$refs[this.id],
format: "YYYY-MM-DD",
onSelect: this.onSelect.bind(this)
})
},
onSelect (date) {
this.dispatch("input", this.picker.toString("YYYY-MM-DD"));
}
}
}

// require('bootstrap')
document.addEventListener("DOMContentLoaded", () => {
setUpUiLibraries()

Livewire.hook('message.processed', () => {
setUpUiLibraries()
})
})

window.laravelViews = {
datePicker
}
} catch (e) {}
} catch (error) {
throw new Error(error)
}
19 changes: 9 additions & 10 deletions resources/views/components/form/datepicker.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
- $id
--}}

@component('laravel-views::components.form.input', [
'label' => $label ?? '',
'name' => $name ?? '',
'placeholder' => $placeholder ?? '',
'value' => $value ?? '',
'model' => $model ?? '',
'id' => $id,
'attributes' => 'x-data="window.laravelViews.datePicker()" x-ref="'.$id.'" x-init="init(\'' . $id . '\', $dispatch)"',
])
@endcomponent
<x-lv-input
:value="$value"
:id="$id"
wire:model="{{ $model }}"
x-data="{ picker: null }"
x-ref="{{ $id }}"
x-init="picker = new Pikaday({ field: $refs['{{ $id }}'], format: 'YYYY-MM-DD', onSelect: () => $dispatch('input', picker.toString('YYYY-MM-DD')) })"
>
</x-lv-input>
19 changes: 8 additions & 11 deletions resources/views/components/form/input.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
- $id
- $attributes
--}}
@props(['label' => null])
<div class="text-left mb-4">
<label class="block">
{{ $label ?? '' }}
</label>
@if ($label)
<label class="block">
{{ $label ?? '' }}
</label>
@endif

<input
{{ $attributes }}
class="block appearance-none w-full bg-white border-gray-300 hover:border-gray-500 px-4 py-2 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-blue-600 focus:border-2 border"
type="text"
name="{{ $name ?? '' }}"
placeholder="{{ $placeholder ?? ''}}"
autocomplete="off"
@if (isset($id))
id="{{ $id ?? ''}}"
@endif
wire:model="{{ $model ?? '' }}"

{!! $attributes ?? null !!}
>
</div>
2 changes: 1 addition & 1 deletion resources/views/components/toolbar/filters.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
</button>
</div>
@endif
</x-lv-drop-down>
</x-lv-drop-down>
@endif
12 changes: 9 additions & 3 deletions src/LaravelViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function components()
'form.input-group' => 'form.input-group',
'icon' => 'icon',
'modal' => 'modal',
'form.checkbox' => 'checkbox'
'form.checkbox' => 'checkbox',
'form.input' => 'input',
];
}

Expand Down Expand Up @@ -80,7 +81,9 @@ public function js($options = '')
$assets = [
'livewire' => Livewire::scripts(),
'alpine' => '<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>',
'laravel-views' => '<script src="' . asset('/vendor/laravel-views.js') . '" type="text/javascript" defer></script>'
'laravel-views' => '<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>
<script src="' . asset('/vendor/laravel-views.js') . '" type="text/javascript" defer></script>'
];

return $this->getCustomizedLinks($assets, $options);
Expand Down Expand Up @@ -116,6 +119,9 @@ private function getCustomizedLinks($assets = [], $options = '')
$links = $assets;
}

return implode("\n", $links);
$linksStr = implode(PHP_EOL, $links);
return <<<HTML
{$linksStr}
HTML;
}
}
12 changes: 9 additions & 3 deletions tests/Unit/LaravelViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function testRenderJsLinks()
$js = LaravelViews::js();
$this->assertEquals(
$js,
\Livewire\Livewire::scripts()."\n".
'<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>'."\n".
\Livewire\Livewire::scripts().PHP_EOL.
'<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>'.PHP_EOL.
'<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'.PHP_EOL.
'<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>'.PHP_EOL.
'<script src="' . asset('/vendor/laravel-views.js') . '" type="text/javascript" defer></script>'
);
}
Expand All @@ -49,14 +51,18 @@ public function testRenderCustomizedJsLinks()

$this->assertEquals(
$js,
'<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'.PHP_EOL.
'<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>'.PHP_EOL.
'<script src="' . asset('/vendor/laravel-views.js') . '" type="text/javascript" defer></script>'
);

$js = LaravelViews::js('laravel-views,alpine');

$this->assertEquals(
$js,
'<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>'."\n".
'<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>'.PHP_EOL.
'<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'.PHP_EOL.
'<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>'.PHP_EOL.
'<script src="' . asset('/vendor/laravel-views.js') . '" type="text/javascript" defer></script>'
);
}
Expand Down

0 comments on commit ede4986

Please sign in to comment.