Skip to content

Commit

Permalink
Merge pull request #169 from kontenta/improve-aria-describedby
Browse files Browse the repository at this point in the history
Improve aria-describedby
  • Loading branch information
erik-epineer authored May 7, 2020
2 parents c3c2ed5 + a641b3b commit 88916b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/form-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ The `autofocusControlId` should be explicitly passed to each input when using
## Common parameters

All inputs need at least the `$name` parameter
and optional `$placeholder` and `$ariaDescribedById` parameters.
and optional `$placeholder` parameters.

All form views take a `$controlAttributes` `array` that can be used to set any
additional HTML attributes on the form control element.
This can be useful for setting
`required`, `disabled`, `readonly`, `autocomplete`,
`required`, `disabled`, `readonly`, `autocomplete`, `aria-describedby`,
and other attributes specific to the
[different input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_<input>_types).

Expand Down
31 changes: 15 additions & 16 deletions resources/views/forms/partials/inputAttributes.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
@include('kontour::forms.partials.nameAttribute')

id="{{ $controlId }}"
@if($errors->hasAny($errorsKeys ?? $name))
aria-invalid="true"
aria-describedby="{{ $errorsId }}"
@elseif(isset($ariaDescribedById))
aria-describedby="{{ $ariaDescribedById }}"
@endif
@if(!empty($autofocusControlId) and $autofocusControlId == $controlId)
autofocus
@endif
@if(!empty($placeholder))
placeholder="{{ $placeholder }}"
@endif
@if(isset($controlAttributes) and is_iterable($controlAttributes))
<?php
$controlAttributes['id'] = $controlId;
unset($controlAttributes['name']);
if($errors->hasAny($errorsKeys ?? $name)) {
$controlAttributes['aria-invalid'] = "true";
$controlAttributes['aria-describedby'] = array_merge([$errorsId], [$controlAttributes['aria-describedby'] ?? []]);
}
if(!empty($autofocusControlId) and $autofocusControlId == $controlId and !in_array('autofocus', $controlAttributes)) {
$controlAttributes[] = 'autofocus';
}
if(!empty($placeholder)) {
$controlAttributes['placeholder'] = $placeholder;
}
?>
@foreach($controlAttributes as $attributeName => $attributeValue)
@include('kontour::partials.attribute')
@endforeach
@endif
@endforeach
9 changes: 4 additions & 5 deletions tests/Feature/FormViewTests/InputAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,23 @@ public function test_aria_describedby_can_be_set()
'name' => 'testName',
'controlId' => 'testId',
'errors' => new MessageBag,
'ariaDescribedById' => 'descriptionId',
'controlAttributes' => ['aria-describedby' => 'descriptionId'],
])->render();

$this->assertRegExp('/\s+aria-describedby="descriptionId"\W/', $output);
}

public function test_error_input_overwriting_aria_describedby()
public function test_error_input_prepending_aria_describedby()
{
$output = View::make('kontour::forms.partials.inputAttributes', [
'name' => 'testName',
'controlId' => 'testId',
'errors' => new MessageBag(['testName' => ['A message']]),
'errorsId' => 'errorsId',
'ariaDescribedById' => 'descriptionId',
'controlAttributes' => ['aria-describedby' => 'descriptionId'],
])->render();

$this->assertRegExp('/\s+aria-describedby="errorsId"\W/', $output);
$this->assertNotRegExp('/aria-describedby="descriptionId"/', $output);
$this->assertRegExp('/\s+aria-describedby="errorsId\sdescriptionId"\W/', $output);
}

public function test_control_attributes()
Expand Down

0 comments on commit 88916b3

Please sign in to comment.