• Stars
    star
    499
  • Rank 88,341 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Livewire component for dependant and/or searchable select inputs

Livewire Select

Livewire component for dependant and/or searchable select inputs

Preview

preview

Installation

You can install the package via composer:

composer require asantibanez/livewire-select

Requirements

This package uses livewire/livewire (https://laravel-livewire.com/) under the hood.

It also uses TailwindCSS (https://tailwindcss.com/) for base styling.

Please make sure you include both of these dependencies before using this component.

Usage

In order to use this component, you must create a new Livewire component that extends from LivewireSelect

You can use make:livewire to create a new component. For example.

php artisan make:livewire CarModelSelect

In the CarModelSelect class, instead of extending from the base Livewire Component class, extend from LivewireSelect class. Also, remove the render method. You'll have a class similar to this snippet.

class CarModelSelect extends LivewireSelect
{
    //
}

In this class, you must override the following methods to provide options for your select input

public function options($searchTerm = null) : Collection 
{
    //
}

options() must return a collection of keyed values array items that must have at least the following keys: value and description. For example:

public function options($searchTerm = null) : Collection 
{
    return collect([
        [
            'value' => 'honda',
            'description' => 'Honda',
        ],
        [
            'value' => 'mazda',
            'description' => 'Mazda',
        ],
        [
            'value' => 'tesla',
            'description' => 'Tesla',
        ],       
    ]);
}

To render the component in a view, just use the Livewire tag or include syntax

<livewire:car-brand-select
   name="car_brand_id"
   :value="$initialValue" // optional
   placeholder="Choose a Car Brand" // optional
/>

You'll see on screen a select input with some custom styles with your defined values

preview

Nothing fancy there. Now, let's make another select input depend on its value.

Create another component following the same process above. In this case, we will create a CarModelSelect with the following options() method.

// In CarModelSelect component
public function options($searchTerm = null) : Collection 
{
    $modelsByBrand = [
        'tesla' => [
            ['value' => 'Model S', 'description' => 'Model S'],
            ['value' => 'Model 3', 'description' => 'Model 3'],
            ['value' => 'Model X', 'description' => 'Model X'],
        ],
        'honda' => [
            ['value' => 'CRV', 'description' => 'CRV'],
            ['value' => 'Pilot', 'description' => 'Pilot'],
        ],
        'mazda' => [
            ['value' => 'CX-3', 'description' => 'CX-3'],
            ['value' => 'CX-5', 'description' => 'CX-5'],
            ['value' => 'CX-9', 'description' => 'CX-9'],
        ],
    ];

    $carBrandId = $this->getDependingValue('car_brand_id');

    if ($this->hasDependency('car_brand_id') && $carBrandId != null) {
        return collect(data_get($modelsByBrand, $carBrandId, []));
    }

    return collect([
        ['value' => 'Model S', 'description' => 'Tesla - Model S'],
        ['value' => 'Model 3', 'description' => 'Tesla - Model 3'],
        ['value' => 'Model X', 'description' => 'Tesla - Model X'],
        ['value' => 'CRV', 'description' => 'Honda - CRV'],
        ['value' => 'Pilot', 'description' => 'Honda - Pilot'],
        ['value' => 'CX-3', 'description' => 'Mazda - CX-3'],
        ['value' => 'CX-5', 'description' => 'Mazda - CX-5'],
        ['value' => 'CX-9', 'description' => 'Mazda - CX-9'],
    ]);
} 

and define it in the view like this

<livewire:car-model-select
    name="car_model_id"
    placeholder="Choose a Car Model"
    :depends-on="['car_brand_id']"
/>

With these two snippets we have defined a select input that depends-on another select input with name car_brand_id. With this definition, we tell our component to listen to any updates on our car_brand_id input and be notified on changes.

preview

Notice in the options() method the use of two other helper methods: getDependingValue and hasDependency.

getDependingValue('token') will return the value of another field, in this case car_brand_id. If car_brand_id has no value, then it will return null.

hasDependency('token') allows us to check if our component has been specified to depend on other component values. This allows us to reuse the component by checking if a dependency has been specified in our layouts.

For example if we define the same component without the :depends-on attribute, we can use the component and return all car models.

<livewire:car-model-select
    name="car_model_id"
    placeholder="Choose a Car Model"
/>

It should look something like this

preview

Searchable inputs

You can define the searchable attribute on the component to change the behavior of your inputs. With :searchable="true" your component will change its behavior to allow searching the options returned in the options() method.

<livewire:car-model-select
   name="car_model_id"
   placeholder="Choose a Car Model"
   :searchable="true"
/>

Your input will look something like this

preview

To filter the options in the dropdown, you can use the $searchTerm parameter in the options() method.

Customizing the UI

// TODO 😬

Advanced behavior

// TODO 😬

AlpineJs support

Add AlpineJs for arrow-keys navigation, enter key for selection, enter/space keys for reset. 😎

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

livewire-calendar

Laravel Livewire component to show Events in a good looking monthly calendar
PHP
883
star
2

livewire-charts

Neat Livewire Charts for your Laravel projects
PHP
792
star
3

laravel-eloquent-state-machines

State Machines for your Laravel Eloquent models
PHP
523
star
4

laravel-blade-sortable

Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.
PHP
408
star
5

livewire-status-board

Livewire component to show records according to their current status
PHP
338
star
6

livewire-resource-time-grid

Laravel Livewire component to show Events by time and resource in a good looking grid
PHP
220
star
7

laravel-subscribable-notifications

Laravel Subscribable Notifications
PHP
142
star
8

Patio

A minimalistic Android view widget for selecting multiple images
Java
106
star
9

livewire-charts-demo

Livewire Charts demo app
PHP
69
star
10

Ranger

Android horizontally scrolled DatePicker
Java
57
star
11

livewire-dependant-select-demo

Laravel Livewire demo of multiple selects depending on each other values
PHP
48
star
12

Quota

Quota widget for Android
Java
31
star
13

livewire-calendar-demo

livewire-calendar component demo
PHP
23
star
14

laravel-eloquent-state-machines-demo

Demo repository for Laravel Eloquent State Machines package
PHP
17
star
15

OAuthWebView

WebViews for OAuth Authentication
Java
12
star
16

laravel-blade-sortable-demo

Demos for asantibanez/laravel-blade-sortable
PHP
11
star
17

livewire-wire-model-file-demo

Livewire lifecycle hook example for wire:model in file inputs
PHP
11
star
18

livewire-select-demo

Laravel app showcasing asantibanez/livewire-select component
PHP
10
star
19

practical

Practical ActiveRecord for DynamoDB
JavaScript
10
star
20

livewire-status-board-demo

Livewire Status Board demo app
PHP
7
star
21

Miveo

Vimeo SUPER AWESOME Android App
Java
4
star
22

laravel-inertia-infinite-scroll-demo

Demo for Infinite Scroll Feed in Laravel and InertiaJs
PHP
4
star
23

udacity-spotify-streamer-stage-2

Java
3
star
24

udacity-build-it-bigger

Udacity Build It Bigger Project
Java
2
star
25

udacity-go-ubiquitous

Project 6 of Udacity's Android Nanodegree program
Java
1
star
26

udacity-make-your-app-material

Project 5 of Udacity's Android Nanodegree program.
Java
1
star
27

vue-router-example

Vue.js project that uses Vue-Router
JavaScript
1
star
28

lienzo

Lienzo is a Horizontal/Vertical RecyclerView gallery powered by Picasso
Java
1
star
29

MarkdownViewer

Android Markdown View
Java
1
star
30

android-google-maps-test

Google Maps instructions for Android project
Java
1
star
31

laravel-paypal-server-checkout-test

PHP
1
star
32

livewire-charts-demo-livewire-v3

Livewire Charts Demo in Livewire v3
PHP
1
star
33

golden-layout-vue

Example of Golden Layout + Vue and Vuex integration
Vue
1
star
34

laravel-ecuadorian-taxpayer-validation-rule

Ecuadorian Taxpayer Validation Rule for Laravel
PHP
1
star
35

whatsapp-clone

WhatsApp Web UI clone using TailwindCss
Vue
1
star