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

Commit 2120626

Browse files
authored
Merge pull request #13 from protonemedia/label-for-attributes
[Bootstrap 4] Add sensible 'for' attribute to Label component
2 parents 3e66384 + 5551d64 commit 2120626

File tree

9 files changed

+75
-5
lines changed

9 files changed

+75
-5
lines changed

CHANGELOG.md

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

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

5+
## 2.0.0 - 2020-09-09
6+
7+
- Added sensible 'for' attributes to the Bootstrap 4 Label components.
8+
9+
## 1.4.1 - 2020-09-09
10+
11+
- Support for Laravel Livewire 2.0
12+
513
## 1.4.0 - 2020-09-09
614

715
- Support for Laravel 8.0

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
name="{{ $name }}"
1010
@endif
1111

12+
@if($label && !$attributes->get('id'))
13+
id="{{ $id() }}"
14+
@endif
1215

1316
@if($checked)
1417
checked="checked"
1518
@endif
1619
/>
1720

18-
<x-form-label :label="$label" :for="$name" class="form-check-label" />
21+
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" class="form-check-label" />
1922

2023
{!! $help ?? null !!}
2124

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="@if($type === 'hidden') d-none @else form-group @endif">
2-
<x-form-label :label="$label" :for="$name" />
2+
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />
33

44
<div class="input-group">
55
@isset($prepend)
@@ -19,6 +19,10 @@
1919
name="{{ $name }}"
2020
value="{{ $value }}"
2121
@endif
22+
23+
@if($label && !$attributes->get('id'))
24+
id="{{ $id() }}"
25+
@endif
2226
/>
2327

2428
@isset($append)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
@if($checked)
1414
checked="checked"
1515
@endif
16+
17+
@if($label && !$attributes->get('id'))
18+
id="{{ $id() }}"
19+
@endif
1620
/>
1721

18-
<x-form-label :label="$label" :for="$name" class="form-check-label" />
22+
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" class="form-check-label" />
1923

2024
{!! $help ?? null !!}
2125

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="form-group">
2-
<x-form-label :label="$label" :for="$name" />
2+
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />
33

44
<select
55
@if($isWired())
@@ -12,6 +12,10 @@
1212
multiple
1313
@endif
1414

15+
@if($label && !$attributes->get('id'))
16+
id="{{ $id() }}"
17+
@endif
18+
1519
{!! $attributes->merge(['class' => 'form-control ' . ($hasError($name) ? 'is-invalid' : '')]) !!}>
1620
@foreach($options as $key => $option)
1721
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="form-group">
2-
<x-form-label :label="$label" :for="$name" />
2+
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />
33

44
<textarea
55
@if($isWired())
@@ -8,6 +8,10 @@
88
name="{{ $name }}"
99
@endif
1010

11+
@if($label && !$attributes->get('id'))
12+
id="{{ $id() }}"
13+
@endif
14+
1115
{!! $attributes->merge(['class' => 'form-control ' . ($hasError($name) ? 'is-invalid' : '')]) !!}
1216
>@unless($isWired()){!! $value !!}@endunless</textarea>
1317

src/Components/Component.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
abstract class Component extends BaseComponent
1010
{
11+
/**
12+
* ID for this component.
13+
*
14+
* @var string
15+
*/
16+
private $id;
17+
1118
/**
1219
* {@inheritDoc}
1320
*/
@@ -41,4 +48,18 @@ public function isNotWired(): bool
4148
{
4249
return !$this->isWired();
4350
}
51+
52+
/**
53+
* Generates an ID, once, for this component.
54+
*
55+
* @return string
56+
*/
57+
public function id(): string
58+
{
59+
if (!$this->id) {
60+
$this->id = Str::random(4);
61+
}
62+
63+
return $this->id;
64+
}
4465
}

tests/Feature/BootstrapTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,22 @@ public function it_can_add_help_text()
4141
$this->visit('/bootstrap-help')
4242
->seeInElement('.form-text', 'Your username must be 8-20 characters long.');
4343
}
44+
45+
/** @test */
46+
public function it_sets_the_id_on_the_label_or_generates_one()
47+
{
48+
$this->registerTestRoute('bootstrap-label-for');
49+
50+
$page = $this->visit('/bootstrap-label-for')
51+
->seeElement('textarea[id="text_b"]')
52+
->seeElement('label[for="text_b"]');
53+
54+
$inputId = $page->crawler()->filter('input[type="text"]')->attr('id');
55+
56+
$this->assertEquals(4, strlen($inputId));
57+
58+
$page
59+
->seeElement('input[id="' . $inputId . '"]')
60+
->seeElement('label[for="' . $inputId . '"]');
61+
}
4462
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<x-form>
2+
<x-form-input name="input" label="Input" />
3+
<x-form-textarea name="textarea" label="Textarea" id="text_b" />
4+
</x-form>

0 commit comments

Comments
 (0)