Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 643f2bb

Browse files
authored
Select placeholder (#68)
1 parent 2788d35 commit 643f2bb

File tree

9 files changed

+95
-2
lines changed

9 files changed

+95
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-form-components` will be documented in this file
44

5+
## 3.2.0 - 2021-11-01
6+
7+
- Support for `select` placeholder
8+
59
## 3.1.0 - 2021-11-01
610

711
- Support for Date Casting

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,23 @@ If you want a select element where multiple options can be selected, add the `mu
318318
<x-form-select name="country_code[]" :options="$countries" multiple :default="['be', 'nl']" />
319319
```
320320

321+
You may add a `placeholder` attribute to the select element. This will prepend a disabled option.
322+
323+
This feature was added in v3.2.0. If you're upgrading from a previous version *and* you published the Blade views, you should republish them *or* update them manually.
324+
325+
```blade
326+
<x-form-select name="country_code" placeholder="Choose..." />
327+
```
328+
329+
Rendered HTML:
330+
331+
```html
332+
<select>
333+
<option value="" disabled>Choose...</option>
334+
<!-- other options... -->
335+
</select>
336+
```
337+
321338
#### Using Eloquent relationships
322339

323340
This package has built-in support for `BelongsToMany`, `MorphMany`, and `MorphToMany` relationships. To utilize this feature, you must add both the `multiple` and `many-relation` attribute to the select element.

resources/views/bootstrap-4/form-select.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@
1212
multiple
1313
@endif
1414

15+
@if($placeholder)
16+
placeholder="{{ $placeholder }}"
17+
@endif
18+
1519
@if($label && !$attributes->get('id'))
1620
id="{{ $id() }}"
1721
@endif
1822

1923
{!! $attributes->merge(['class' => 'form-control ' . ($hasError($name) ? 'is-invalid' : '')]) !!}>
24+
25+
@if($placeholder)
26+
<option value="" disabled @if($nothingSelected()) selected="selected" @endif>
27+
{{ $placeholder }}
28+
</option>
29+
@endif
30+
2031
@forelse($options as $key => $option)
2132
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>
2233
{{ $option }}

resources/views/bootstrap-5/form-select.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@
1515
multiple
1616
@endif
1717

18+
@if($placeholder)
19+
placeholder="{{ $placeholder }}"
20+
@endif
21+
1822
@if($label && !$attributes->get('id'))
1923
id="{{ $id() }}"
2024
@endif
2125

2226
{!! $attributes->merge(['class' => 'form-select' . ($hasError($name) ? ' is-invalid' : '')]) !!}
2327
>
28+
29+
@if($placeholder)
30+
<option value="" disabled @if($nothingSelected()) selected="selected" @endif>
31+
{{ $placeholder }}
32+
</option>
33+
@endif
34+
2435
@forelse($options as $key => $option)
2536
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>
2637
{{ $option }}

resources/views/tailwind-2/form-select.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
multiple
1414
@endif
1515

16+
@if($placeholder)
17+
placeholder="{{ $placeholder }}"
18+
@endif
19+
1620
{!! $attributes->merge([
1721
'class' => ($label ? 'mt-1' : '') . ' block w-full'
1822
]) !!}>
23+
24+
@if($placeholder)
25+
<option value="" disabled @if($nothingSelected()) selected="selected" @endif>
26+
{{ $placeholder }}
27+
</option>
28+
@endif
29+
1930
@forelse($options as $key => $option)
2031
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>
2132
{{ $option }}

resources/views/tailwind/form-select.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
multiple
1414
@endif
1515

16+
@if($placeholder)
17+
placeholder="{{ $placeholder }}"
18+
@endif
19+
1620
{!! $attributes->merge([
1721
'class' => ($label ? 'mt-1' : '') . ' block w-full ' . ($multiple ? 'form-multiselect' : 'form-select')
1822
]) !!}>
23+
24+
@if($placeholder)
25+
<option value="" disabled @if($nothingSelected()) selected="selected" @endif>
26+
{{ $placeholder }}
27+
</option>
28+
@endif
29+
1930
@forelse($options as $key => $option)
2031
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>
2132
{{ $option }}

src/Components/FormSelect.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FormSelect extends Component
1717
public $selectedKey;
1818
public bool $multiple;
1919
public bool $floating;
20+
public string $placeholder;
2021

2122
/**
2223
* Create a new component instance.
@@ -32,12 +33,14 @@ public function __construct(
3233
bool $multiple = false,
3334
bool $showErrors = true,
3435
bool $manyRelation = false,
35-
bool $floating = false
36+
bool $floating = false,
37+
string $placeholder = ''
3638
) {
3739
$this->name = $name;
3840
$this->label = $label;
3941
$this->options = $options;
4042
$this->manyRelation = $manyRelation;
43+
$this->placeholder = $placeholder;
4144

4245
if ($this->isNotWired()) {
4346
$inputName = Str::before($name, '[]');
@@ -64,4 +67,13 @@ public function isSelected($key): bool
6467

6568
return in_array($key, Arr::wrap($this->selectedKey));
6669
}
70+
71+
public function nothingSelected(): bool
72+
{
73+
if ($this->isWired()) {
74+
return false;
75+
}
76+
77+
return is_array($this->selectedKey) ? empty($this->selectedKey) : is_null($this->selectedKey);
78+
}
6779
}

tests/Feature/SelectSlotTest.php renamed to tests/Feature/SelectTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
66

7-
class SelectSlotTest extends TestCase
7+
class SelectTest extends TestCase
88
{
99
/** @test */
1010
public function it_shows_the_slot_if_the_options_are_empty()
@@ -16,4 +16,15 @@ public function it_shows_the_slot_if_the_options_are_empty()
1616
->seeElement('option[value="b"]')
1717
->seeElement('option[value="c"]');
1818
}
19+
20+
/** @test */
21+
public function it_can_render_a_placeholder()
22+
{
23+
$this->registerTestRoute('select-placeholder');
24+
25+
$this->visit('/select-placeholder')
26+
->seeElement('option[value=""][selected="selected"]')
27+
->seeElement('option[value="a"]')
28+
->seeElement('option[value="b"]');
29+
}
1930
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<x-form>
2+
<x-form-select name="select" placeholder="Choose..." :options="['a' => 'a', 'b' => 'b']"></x-form-select>
3+
4+
<x-form-submit />
5+
</x-form>

0 commit comments

Comments
 (0)