Skip to content

Commit

Permalink
update single asset naming conventions e.g. use livewire-select inste…
Browse files Browse the repository at this point in the history
…ad of livewireSelect and add �ssetType parameter to renderAssets method
  • Loading branch information
bfiessinger committed Aug 27, 2021
1 parent 5f8a9cd commit 524f01a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/LivewireSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ public function css($options = null) {
'tailwindcss' => '<link rel="stylesheet" href="' . asset('/vendor/tailwind.css') . '" />',
];

return $this->renderAssets($assets, $options);
return $this->renderAssets('css', $assets, $options);
}

public function js($options = null) {
$assets = [
'livewire' => Livewire::scripts(),
'alpine' => '<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>',
'livewireSelect' => '<script>
'livewire-select' => '<script>
window.livewire.on(\'livewire-select-focus-search\', (data) => {
const el = document.getElementById(`${data.name || \'invalid\'}`);
Expand All @@ -265,7 +265,7 @@ public function js($options = null) {
el.focus();
});
</script>',
'livewireSelectMultiple' => '<script>
'livewire-select-multiple' => '<script>
function livewireSelectMultiSelectDropdown($el) {
// Select the node that will be observed for mutations
var innerSelectTargetNode = $el.querySelector(\'.livewire-select-input\');
Expand Down Expand Up @@ -346,7 +346,7 @@ function livewireSelectMultiSelectDropdown($el) {
</script>'
];

return $this->renderAssets($assets, $options);
return $this->renderAssets('js', $assets, $options);
}

public function render()
Expand Down Expand Up @@ -390,12 +390,20 @@ public function render()
*
* @return string
*/
private function renderAssets($assets = [], $options = null) {
private function renderAssets($assetType = 'js', $assets = [], $options = null) {
if ($options) {
$options = explode(',', $options);
$options = array_map('trim', $options);
if (!in_array('livewireSelect', $options)) {
$options[] = 'livewireSelect';

// include mandatory assets
switch ($assetType) {
case 'js':
if (!in_array('livewire-select', $options)) {
$options[] = 'livewire-select';
}
break;
default:
break;
}

$assetArray = [];
Expand Down

0 comments on commit 524f01a

Please sign in to comment.