Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jackbayliss/livewire-select2

A livewire select2 component package to help with using select2 - it does the heavy lifting.
https://github.com/jackbayliss/livewire-select2

component livewire select2

Last synced: 2 months ago
JSON representation

A livewire select2 component package to help with using select2 - it does the heavy lifting.

Awesome Lists containing this project

README

        

# Simple to use Livewire component for Select2

[![Latest Version on Packagist](https://img.shields.io/packagist/v/jackbayliss/livewire-select2.svg?style=flat-square)](https://packagist.org/packages/jackbayliss/livewire-select2)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/jackbayliss/livewire-select2/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/jackbayliss/livewire-select2/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/jackbayliss/livewire-select2/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/jackbayliss/livewire-select2/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/jackbayliss/livewire-select2.svg?style=flat-square)](https://packagist.org/packages/jackbayliss/livewire-select2)

Easy to use Livewire component specifically for Select2.

## Installation

You can install the package via composer:
This currently works on Livewire v3, ie Laravel 11.

I haven't tested 2 yet.

```bash
composer require jackbayliss/livewire-select2
```
## Initial Setup
First of all, ensure you install jQuery, and select2- for example, the below. You can also install these via npm and import it into your app.js.
Main thing is ensuring the scripts are included anywhere you want select2 to work.
```html

```
## Usage
The package is quite straight forward, and can be used directly in a **LIVEWIRE** component, see below the basic usage
```html

```

## Component parameters
#### options (required) - array
The select2 component expects an array of options, this can be for example - you must pass this into the options param as per the above.
```php
public $vehicles = ['Ford','Vauxhall','Seat'];
```

#### onchange (required) - string
The select2 component expects a string of the listener function to call. For example, `triggerMyFunction`- you can see how to do this below.
The onchange function, expects one param. The param returned from this component is an array of data - which includes the following:
##### Name - The name param you passed in, this can be used in your custom function if required - this is null if not supplied.
##### Data - This is the values returned from the select2 change, if you have not set the multiple param, this is a string- otherwise its an array. Please see https://github.com/jackbayliss/livewire-select2?tab=readme-ov-file#base-component if theres any confusion.
> [!IMPORTANT]
> Ensure your function includes one parameter, for example `triggerMyFunction($select2)`

#### name (not required) - string
You can pass this to your component if you need specific logic based on the name.
#### model (not required) - string
You can pass an option value in as model, this then auto selects the option- useful in a case where a selection has already been made.
```html

```
#### How to add a custom class
This can apply a custom CSS class to the Livewire Select2 component, see below for an example

```css

.example{
width: 50% !important;
}

```

```html

```

#### multiple (not required) - blank param
> [!TIP]
> If this is used, the data will be returned as an array, rather than a string.

This allows you to select multiple options, as per select2 usually does and should be used like the following:
```html

```

## Creating a Listener
> [!TIP]
> A listener is required for the onchange param, in order to create a listener, you should do the below. This means, whenever the select2 component is changed - ie an option clicked, it will call the function you defined, and you can do as you wish with the logic.

```html

```

```php

protected $listeners = ['triggerMyFunction'];

public function triggerMyFunction($data){
dd($data['name'],$data['data']);
}

```
> [!IMPORTANT]
> Whatever string you pass to the onchange param, ensure you create a listener and function like the above. **Your function must accept one parameter, this is the array of data returned which includes name and data, name being the name you set on the component (if you did set one) and the data ie the value selected.**

## Example / How it works in practice
#### Base component
I have an initial component, which I am calling as in my blade. The component is below:
```php
vehicles)){
$this->selectedVehicle = $output['data'];
}
}
public function render()
{
return view('livewire.test');
}
}

```
#### The view, is basic and looks like the below - main thing to take away is how the component is used:
```html

{{ $this->selectedVehicle }}

```
You then get the below...

![test-component](https://github.com/jackbayliss/livewire-select2/assets/13621738/1fb1dfaf-cc1d-44b5-82a5-4cff50e7b679)

## Stuck?
I have an example project setup below.. you can simply download it and composer install.

https://github.com/jackbayliss/livewire-select-2-example-proj

## Testing WIP

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [Jack Bayliss](https://github.com/jackbayliss)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.