Skip to content

Commit

Permalink
Merge pull request protonemedia#13 from protonemedia/label-for-attrib…
Browse files Browse the repository at this point in the history
…utes

[Bootstrap 4] Add sensible 'for' attribute to Label component
  • Loading branch information
pascalbaljet authored Sep 9, 2020
2 parents 3e66384 + 5551d64 commit 2120626
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

## 2.0.0 - 2020-09-09

- Added sensible 'for' attributes to the Bootstrap 4 Label components.

## 1.4.1 - 2020-09-09

- Support for Laravel Livewire 2.0

## 1.4.0 - 2020-09-09

- Support for Laravel 8.0
Expand Down
5 changes: 4 additions & 1 deletion resources/views/bootstrap-4/form-checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
name="{{ $name }}"
@endif

@if($label && !$attributes->get('id'))
id="{{ $id() }}"
@endif

@if($checked)
checked="checked"
@endif
/>

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

{!! $help ?? null !!}

Expand Down
6 changes: 5 additions & 1 deletion resources/views/bootstrap-4/form-input.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="@if($type === 'hidden') d-none @else form-group @endif">
<x-form-label :label="$label" :for="$name" />
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />

<div class="input-group">
@isset($prepend)
Expand All @@ -19,6 +19,10 @@
name="{{ $name }}"
value="{{ $value }}"
@endif

@if($label && !$attributes->get('id'))
id="{{ $id() }}"
@endif
/>

@isset($append)
Expand Down
6 changes: 5 additions & 1 deletion resources/views/bootstrap-4/form-radio.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
@if($checked)
checked="checked"
@endif

@if($label && !$attributes->get('id'))
id="{{ $id() }}"
@endif
/>

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

{!! $help ?? null !!}

Expand Down
6 changes: 5 additions & 1 deletion resources/views/bootstrap-4/form-select.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form-group">
<x-form-label :label="$label" :for="$name" />
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />

<select
@if($isWired())
Expand All @@ -12,6 +12,10 @@
multiple
@endif

@if($label && !$attributes->get('id'))
id="{{ $id() }}"
@endif

{!! $attributes->merge(['class' => 'form-control ' . ($hasError($name) ? 'is-invalid' : '')]) !!}>
@foreach($options as $key => $option)
<option value="{{ $key }}" @if($isSelected($key)) selected="selected" @endif>
Expand Down
6 changes: 5 additions & 1 deletion resources/views/bootstrap-4/form-textarea.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form-group">
<x-form-label :label="$label" :for="$name" />
<x-form-label :label="$label" :for="$attributes->get('id') ?: $id()" />

<textarea
@if($isWired())
Expand All @@ -8,6 +8,10 @@
name="{{ $name }}"
@endif

@if($label && !$attributes->get('id'))
id="{{ $id() }}"
@endif

{!! $attributes->merge(['class' => 'form-control ' . ($hasError($name) ? 'is-invalid' : '')]) !!}
>@unless($isWired()){!! $value !!}@endunless</textarea>

Expand Down
21 changes: 21 additions & 0 deletions src/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

abstract class Component extends BaseComponent
{
/**
* ID for this component.
*
* @var string
*/
private $id;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -41,4 +48,18 @@ public function isNotWired(): bool
{
return !$this->isWired();
}

/**
* Generates an ID, once, for this component.
*
* @return string
*/
public function id(): string
{
if (!$this->id) {
$this->id = Str::random(4);
}

return $this->id;
}
}
18 changes: 18 additions & 0 deletions tests/Feature/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ public function it_can_add_help_text()
$this->visit('/bootstrap-help')
->seeInElement('.form-text', 'Your username must be 8-20 characters long.');
}

/** @test */
public function it_sets_the_id_on_the_label_or_generates_one()
{
$this->registerTestRoute('bootstrap-label-for');

$page = $this->visit('/bootstrap-label-for')
->seeElement('textarea[id="text_b"]')
->seeElement('label[for="text_b"]');

$inputId = $page->crawler()->filter('input[type="text"]')->attr('id');

$this->assertEquals(4, strlen($inputId));

$page
->seeElement('input[id="' . $inputId . '"]')
->seeElement('label[for="' . $inputId . '"]');
}
}
4 changes: 4 additions & 0 deletions tests/Feature/views/bootstrap-label-for.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x-form>
<x-form-input name="input" label="Input" />
<x-form-textarea name="textarea" label="Textarea" id="text_b" />
</x-form>

0 comments on commit 2120626

Please sign in to comment.