-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Options component * Add Options component - Fix style issues * Add Options component - Add test case * Add Options component - Make test case compatible with L7 * [Components/Options]: Add code improvements and support to handle disabled options. * [Component/Options]: Add support for empty option. * [Components/Options]: Add support to empty option and placeholder. Co-authored-by: Diego Smania <[email protected]>
- Loading branch information
Showing
4 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{{-- Empty option --}} | ||
@if(isset($emptyOption)) | ||
|
||
<option value> | ||
{{ is_string($emptyOption) ? $emptyOption : '' }} | ||
</option> | ||
|
||
{{-- Placeholder option --}} | ||
@elseif(isset($placeholder)) | ||
|
||
<option class="d-none" value> | ||
{{ is_string($placeholder) ? $placeholder : '' }} | ||
</option> | ||
|
||
@endif | ||
|
||
{{-- Other options --}} | ||
@foreach($options as $key => $value) | ||
|
||
<option value="{{ $key }}" | ||
@if($isSelected($key)) selected @endif | ||
@if($isDisabled($key)) disabled @endif> | ||
{{ $value }} | ||
</option> | ||
|
||
@endforeach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace JeroenNoten\LaravelAdminLte\Components\Form; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\View\Component; | ||
|
||
class Options extends Component | ||
{ | ||
/** | ||
* The list of options as key value pairs. | ||
* | ||
* @var array | ||
*/ | ||
public $options; | ||
|
||
/** | ||
* The list of selected option keys. | ||
* | ||
* @var array | ||
*/ | ||
public $selected; | ||
|
||
/** | ||
* The list of disabled option keys. | ||
* | ||
* @var array | ||
*/ | ||
public $disabled; | ||
|
||
/** | ||
* Whether to use strict comparison between the option keys and the keys of | ||
* the selected/disabled options. | ||
* | ||
* @var bool | ||
*/ | ||
public $strict; | ||
|
||
/** | ||
* Whether to add a selectable empty option to the list of options. If the | ||
* value is a string, it will be used as the option label, otherwise no | ||
* label will be available for the empty option. | ||
* | ||
* @var bool|string | ||
*/ | ||
public $emptyOption; | ||
|
||
/** | ||
* Whether to add a placeholder (non-selectable option) to the list of | ||
* options. If the value is a string, it will be used as the placeholder | ||
* label, otherwise no label will be available for the placeholder. | ||
* | ||
* @var bool|string | ||
*/ | ||
public $placeholder; | ||
|
||
/** | ||
* Create a new component instance. | ||
*/ | ||
public function __construct( | ||
$options, $selected = null, $disabled = null, | ||
$strict = null, $emptyOption = null, $placeholder = null | ||
) { | ||
$this->options = Arr::wrap($options); | ||
$this->selected = Arr::wrap($selected); | ||
$this->disabled = Arr::wrap($disabled); | ||
$this->strict = isset($strict); | ||
$this->emptyOption = $emptyOption; | ||
$this->placeholder = $placeholder; | ||
} | ||
|
||
/** | ||
* Determines if an option's key is on selected state. | ||
* | ||
* @param string $key The option's key. | ||
* @return bool | ||
*/ | ||
public function isSelected($key) | ||
{ | ||
return in_array($key, $this->selected, $this->strict); | ||
} | ||
|
||
/** | ||
* Determines if an option's key is on disabled state. | ||
* | ||
* @param string $key The option's key. | ||
* @return bool | ||
*/ | ||
public function isDisabled($key) | ||
{ | ||
return in_array($key, $this->disabled, $this->strict); | ||
} | ||
|
||
/** | ||
* Get the view / contents that represent the component. | ||
* | ||
* @return \Illuminate\View\View|string | ||
*/ | ||
public function render() | ||
{ | ||
return view('adminlte::components.form.options'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters