New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add select component #52
base: main
Are you sure you want to change the base?
Changes from all commits
4c2f054
5bb8a02
23cbee6
945409d
15f7ed9
5632241
cf5f49c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<select | ||
name="{{ $name }}" | ||
id="{{ $id }}" | ||
{{ $attributes }} | ||
> | ||
@if(!$slot->isEmpty()) | ||
{{ $slot }} | ||
@else | ||
@if($placeholder) | ||
<option value="" hidden>{{ $placeholder }}</option> | ||
@endif | ||
@foreach($options as $value => $label) | ||
<option | ||
value="{{ $value }}" | ||
{!! $isSelected($value) ? 'selected' : '' !!} | ||
>{{ $label }}</option> | ||
@endforeach | ||
@endif | ||
</select> |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,70 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace BladeUIKit\Components\Forms\Inputs; | ||||||
|
||||||
use BladeUIKit\Components\BladeComponent; | ||||||
use http\Exception\InvalidArgumentException; | ||||||
use Illuminate\Contracts\View\View; | ||||||
use Illuminate\Support\Collection; | ||||||
|
||||||
class Select extends BladeComponent | ||||||
{ | ||||||
/** @var string */ | ||||||
public $name; | ||||||
|
||||||
/** @var string|null */ | ||||||
public $id; | ||||||
|
||||||
/** @var array|Collection */ | ||||||
public $options; | ||||||
|
||||||
/** @var string */ | ||||||
public $nameAttribute; | ||||||
|
||||||
/** @var string */ | ||||||
public $valueAttribute; | ||||||
|
||||||
/** @var string|array|null */ | ||||||
public $selected; | ||||||
|
||||||
/** @var string */ | ||||||
public $placeholder; | ||||||
|
||||||
public function __construct(string $name, $options, string $placeholder = '', string $nameAttribute = 'name', string $valueAttribute = 'id', $id = null, $selected = null) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that a more appropriate name for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
{ | ||||||
$this->name = $name; | ||||||
$this->id = $id ?? $name; | ||||||
$this->nameAttribute = $nameAttribute; | ||||||
$this->valueAttribute = $valueAttribute; | ||||||
$this->selected = $selected; | ||||||
$this->placeholder = $placeholder; | ||||||
|
||||||
if ($options instanceof Collection) { | ||||||
$this->options = $options->mapWithKeys(function ($option) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can refactor this
Suggested change
|
||||||
return [$option->{$this->valueAttribute} => $option->{$this->nameAttribute}]; | ||||||
}); | ||||||
} elseif (is_array($options)) { | ||||||
$this->options = $options; | ||||||
} else { | ||||||
throw new InvalidArgumentException('Invalid options'); | ||||||
} | ||||||
} | ||||||
|
||||||
public function isSelected($value): bool | ||||||
{ | ||||||
$selected = old($this->name, $this->selected); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of the |
||||||
|
||||||
if (is_array($selected)) { | ||||||
return in_array($value, $selected); | ||||||
} | ||||||
|
||||||
return ($value === $selected); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function render(): View | ||||||
{ | ||||||
return view('blade-ui-kit::components.forms.inputs.select'); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Components\Forms\Inputs; | ||
|
||
use Tests\Components\ComponentTestCase; | ||
|
||
class SelectTest extends ComponentTestCase | ||
{ | ||
/** @test */ | ||
public function the_component_can_be_rendered() | ||
{ | ||
$template = <<<HTML | ||
<select name="select" id="select"> | ||
<option value="0">Test</option> | ||
</select> | ||
HTML; | ||
$this->assertComponentRenders( | ||
$template, | ||
'<x-select name="select" :options="[\'Test\']" />' | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function selected_attribute_test() | ||
{ | ||
$template = <<<HTML | ||
<select name="select" id="select"> | ||
<option value="test">Test</option> | ||
<option value="test2" selected>Test2</option> | ||
</select> | ||
HTML; | ||
$this->assertComponentRenders( | ||
$template, | ||
'<x-select name="select" :options="[\'test\' => \'Test\', \'test2\' => \'Test2\']" selected="test2" />' | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function specific_attributes_can_be_overwritten() | ||
{ | ||
$template = <<<HTML | ||
<select name="select" id="select" class="p-4"> | ||
<option value="0">Test</option> | ||
</select> | ||
HTML; | ||
$this->assertComponentRenders( | ||
$template, | ||
'<x-select name="select" :options="[\'Test\']" class="p-4" />' | ||
); | ||
} | ||
|
||
public function placeholder_attribute_test() | ||
{ | ||
$template = <<<HTML | ||
<select name="select" id="select"> | ||
<option value="" hidden>Placeholder</option> | ||
<option value="0">Test</option> | ||
</select> | ||
HTML; | ||
$this->assertComponentRenders( | ||
$template, | ||
'<x-select name="select" :options="[\'Test\']" placeholder="Placeholder" />' | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function inputs_can_have_old_values() | ||
{ | ||
$this->flashOld(['select' => 'test2']); | ||
|
||
$template = <<<HTML | ||
<select name="select" id="select"> | ||
<option value="test">Test</option> | ||
<option value="test2" selected>Test2</option> | ||
</select> | ||
HTML; | ||
|
||
$this->assertComponentRenders( | ||
$template, | ||
'<x-select name="select" :options="[\'test\' => \'Test\', \'test2\' => \'Test2\']" />' | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.