Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Laravel Simple Select inputs component for Blade and Livewire.

License

NotificationsYou must be signed in to change notification settings

laravel-shift/laravel-simple-select

 
 

Repository files navigation

Laravel Simple Select inputs component for Blade and Livewire.

Latest Version on PackagistTotal DownloadsGitHub Actions

DEMO PREVIEW

preview

Table of Contents


Installation

You can install the package via composer:

composer require victorybiz/laravel-simple-select

OPTIONAL: To customize the component, you should publish the configuration file using the vendor:publish Artisan command. The configuration file will be placed in your application's config directory and view file in views directory respectively:

# Publish the config filephp artisan vendor:publish --tag=simple-select:config
# Publish the view filephp artisan vendor:publish --tag=simple-select:views

Requirements

This package use the following packages.

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

JavaScript Dependencies

For any external JavaScript dependency, we recommend you install them through npm or yarn, and then require them in your project's JavaScript. To install each of the dependencies this package makes use of, run this command in the terminal:

npm install -D alpinejs @popperjs/core
importAlpinefrom'alpinejs'import{createPopper}from"@popperjs/core";window.Alpine=Alpine;Alpine.start()window.createPopper=createPopper;

If you’re using the compiled JavaScript, don’t forget to include CDN versions of the JavaScript Dependencies before it.

Usage

Simple Select

@php// Basic Arrays$options = ['Nigeria','United Kingdom','United States'];// Above will output Option Value e.g Nigeria// Above will output Option Text e.g Nigeria// OR// Associative Arrays$options = [  ['value' =>'NG','text' =>'Nigeria'],  ['value' =>'GB','text' =>'United Kingdom'],  ['value' =>'US','text' =>'United States']];// Above will output Option Value e.g NG// Above will output Option Text e.g Nigeria// OR// Using Associative Arrays data from a Model/Database,// ensure to customize the field names with value-field="code" and text-field="name" properties of the component.$options = [  ['code' =>'NG','name' =>'Nigeria'],  ['code' =>'GB','name' =>'United Kingdom'],  ['code' =>'US','name' =>'United States']];// OR$options = [  ['code' =>'NG','name' =>'Nigeria','flag' =>'https://www.countryflags.io/ng/shiny/32.png'],  ['code' =>'GB','name' =>'United Kingdom','flag' =>'https://www.countryflags.io/gb/shiny/32.png'],  ['code' =>'US','name' =>'United States','flag' =>'https://www.countryflags.io/us/shiny/32.png']];// Above will output Option Value e.g NG// Above will output Option Text e.g Nigeria@endphp
<x-simple-selectname="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"/>

Custom Option Slot

<x-simple-selectname="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"><x-slotname="customOption"><imgclass="float-left mr-2 -mt-1":src="option.flag"><spanx-text="option.name"></span></x-slot></x-simple-select><x-simple-selectname="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"><x-slotname="customOption"><imgclass="float-left mr-2 -mt-1":src="`https://www.countryflags.io/${option.code?.toLowerCase()}/shiny/32.png`"><spanx-text="option.name"></span></x-slot></x-simple-select>

Custom Selected Slot

<x-simple-selectname="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"><x-slotname="customSelected"><imgclass="float-left mr-2":src="`https://www.countryflags.io/${option.code?.toLowerCase()}/shiny/24.png`"><spanx-text="option.name"></span></x-slot><x-slotname="customOption"><imgclass="float-left mr-2 -mt-1":src="`https://www.countryflags.io/${option.code?.toLowerCase()}/shiny/32.png`"><spanx-text="option.name"></span></x-slot></x-simple-select>

Custom Icon Slots

<x-simple-selectname="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"><x-slotname="customOption"><imgclass="float-left mr-2 -mt-1":src="option.flag"><spanx-text="option.name"></span></x-slot><x-slotname="customDeselectOptionIcon"><!-- Heroicon solid/x-circle --><svgxmlns="http://www.w3.org/2000/svg"viewBox="0 0 24 24"class = 'h-4 fill-current'><pathd="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 16.538l-4.592-4.548 4.546-4.587-1.416-1.403-4.545 4.589-4.588-4.543-1.405 1.405 4.593 4.552-4.547 4.592 1.405 1.405 4.555-4.596 4.591 4.55 1.403-1.416z"/></svg></x-slot></x-simple-select>

Dependent Selects

If you have a custom select whose options depend on the selection of another select, or just some kind of condition to be met, you can listen to the updated event of the livewire model of the main select to update the options in the dependent select.

// Expected data in Database// Model Country::class$countries = [  ['code' =>'NG','name' =>'Nigeria'],  ['code' =>'GB','name' =>'United Kingdom'],  ['code' =>'US','name' =>'United States']];// Model State::class$states = [  ['id' =>1,'country_code' =>'NG','name' =>'Abuja'],  ['id' =>2,'country_code' =>'NG','name' =>'Edo'],  ['id' =>3,'country_code' =>'NG','name' =>'Lagos'],  ['id' =>4,'country_code' =>'US','name' =>'Alaska'],  ['id' =>5,'country_code' =>'US','name' =>'Califonia'],  ['id' =>6,'country_code' =>'US','name' =>'Florida'],  ['id' =>7,'country_code' =>'GB','name' =>'Belfast'],  ['id' =>8,'country_code' =>'GB','name' =>'London'],// ...];

Create a livewire component as the form page

<?phpnamespaceApp\Http\Livewire;useLivewire\Component;class CreateUserextends Component{public$countries = [];public$states = [];public$name;public$country;public$state;protectedfunctionrules()    {//    }publicfunctionupdated($propertyName)    {$this->validateOnly($propertyName);    }publicfunctionstore()    {$this->validate();// Store the data    }publicfunctionmount()    {$this->countries = \App\Models\Country::orderBy('name')->get()->toArray();                 }publicfunctionupdatedCountry($countryCode)    {if ($countryCode) {$this->states = \App\Models\State::where('country_code',$countryCode)->orderBy('name')->get()->toArray();          }else {$this->states = [];                    }$this->state =null;    }publicfunctionrender()    {returnview('livewire.create-user');    }}

In your component view

<formwire:submit.prevent="store"><labelfor="name">Name</label><divclass="mt-1"><inputwire:model="name"name="name"id="name"placeholder="Enter name"class="form-input"/></div><labelfor="country">Country</label><divclass="mt-1"><x-simple-selectwire:model="country"name="country"id="country":options="$options"value-field='code'text-field='name'placeholder="Select Country"search-input-placeholder="Search Country":searchable="true"class="form-select"/></div><labelfor="state">State</label><divclass="mt-1"><x-simple-selectwire:model="state"name="state"id="state":options="$states"value-field='id'text-field='name'placeholder="Select State"search-input-placeholder="Search State":searchable="true"class="form-select"/></div></form>

Event Listener

window.addEventListener('select',function(option){console.log(option.detail.value);// Select option value(s)console.log(option.detail.name);// The select element nameconsole.log(option.detail.id);// The select element ID});

Positioning

The simple-select component makes use ofPopper.js for positioning the select menu. This should remove the need for fixed positioning the select menu now. In addition to positioning the menu when opened, Popper.js will also re-position the menu as needed when the page is scrolled.

Props / Attributes

NameTypeDefaultRequiredDescription
idInteger||StringYesUsed to identify the component in events.
nameInteger||StringYesSpecifies a name for component.
optionsArrayYesArray of available options: Objects, Strings or Integers. If array of objects, visible text/label will default tooption.text and value default tooption.value.
value-fieldString'value'NoArray key for option value ifoptions is an associative array.
text-fieldString'text'NoArray key for option text ifoptions is an associative array.
valueArray||String||IntegernullNoPresets the selected options.
placeholderString'Select Option'NoEquivalent to theplaceholder attribute on a<select> input.
searchableBooleantrueNoShow / hide options search input.
search-input-placeholderString'Search...'NoEquivalent to theplaceholder attribute on a<input>.
clearableBooleanfalseNoEnable support for clearable selection. Use only for a nonmultiple select.
classStringNoEquivalent to theclass attribute on a<select> input.
multipleBooleanfalseNoEquivalent to themultiple attribute on a<select> input. This also enable multiple options tagging if set.
max-selectionIntegerNoLimit number of allowed selected options.
requiredBooleanfalseNoEquivalent to therequired attribute on a<select> input.
disabledBooleanfalseNoEquivalent to thedisabled attribute on a<select> input.
no-optionsString'No option data.'NoMessage to show when options list is empty.
no-resultString'No results match your search.'NoMessage to show when no option.
on-selectString'select'NoCustomize event name of an event emitted after selecting an option.

Slots / Custom Display

NameDescription
customOptionSlot for custom option text template. Seeexample above.
customSelectedSlot for custom selected template. Seeexample above.
customDeselectOptionIconSlot for custom deselect option icon markup. Seeexample above.
customCaretDownIconSlot for custom caret down icon markup. Seeexample above.
customCaretUpIconSlot for custom caret up icon markup. Seeexample above.

Events

NameListen toDescription
SelectselectEmitted after selecting an option. Seeexample above.

Testing

composertest

Changelog

Please seeCHANGELOG for more information what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

Security

If you discover any security related issues, please emaillavictorybiz@gmail.com instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using theLaravel Package Boilerplate.

About

Laravel Simple Select inputs component for Blade and Livewire.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Blade82.5%
  • PHP17.5%

[8]ページ先頭

©2009-2025 Movatter.jp