百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

livewire-select

> 编程语言
开源

依赖和(或)可搜索的选定输入的活线组件

515 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

依赖和(或)可搜索的选定输入的活线组件

Livewire Select

Livewire component for dependant and/or searchable select inputs

Preview

Installation

You can install the package via composer:

bash
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.

bash
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.

php
class CarModelSelect extends LivewireSelect
{
    //
}

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

php
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:

php
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

blade

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

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.

…

and define it in the view like this

blade

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.

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.

blade

It should look something like this

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.

blade

Your input will look something like this

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

bash
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

  • Andrés Santibáñez
  • All Contributors

License

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言