Skip to content

Commit

Permalink
Merge branch 'master' into improve-sr-only-utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-epineer authored May 7, 2020
2 parents 801339f + 5567332 commit 605d549
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 35 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
2 changes: 1 addition & 1 deletion resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<header>{{ __('Login') }}</header>
<form method="POST" action="{{ route('kontour.login') }}">
@csrf
@include('kontour::forms.input', ['name' => 'email', 'type' => 'email', 'controlAttributes' => ['required', 'autocomplete' => 'email']])
@include('kontour::forms.email', ['controlAttributes' => ['required']])
@include('kontour::forms.input', ['name' => 'password', 'type' => 'password', 'controlAttributes' => ['required', 'autocomplete' => 'current-password']])
@include('kontour::forms.checkbox', ['name' => 'remember', 'label' => __('Remember Me')])
@component('kontour::buttons.generic')
Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/passwords/email.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<form method="POST" action="{{ route('kontour.password.email') }}">
@csrf
@include('kontour::forms.input', ['name' => 'email', 'type' => 'email', 'controlAttributes' => ['required', 'autocomplete' => 'email']])
@include('kontour::forms.email', ['controlAttributes' => ['required']])
@component('kontour::buttons.generic')
{{ __('Send Password Reset Link') }}
@endcomponent
Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/passwords/reset.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<form method="POST" action="{{ route('kontour.password.request') }}">
@csrf
<input type="hidden" name="token" value="{{ $token }}">
@include('kontour::forms.input', ['name' => 'email', 'type' => 'email', 'controlAttributes' => ['required', 'autocomplete' => 'email']])
@include('kontour::forms.email', ['controlAttributes' => ['required']])
@include('kontour::forms.input', ['name' => 'password', 'type' => 'password', 'controlAttributes' => ['required', 'autocomplete' => 'new-password']])
@include('kontour::forms.input', ['name' => 'password_confirmation', 'type' => 'password', 'controlAttributes' => ['required', 'autocomplete' => 'new-password']])
@component('kontour::buttons.generic')
Expand Down
5 changes: 5 additions & 0 deletions resources/views/forms/email.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@include('kontour::forms.input', ['type' => $type ?? 'email', 'name' => $name ?? 'email',
'controlAttributes' => array_merge(
['autocomplete' => 'email', 'autocapitalize' => 'none', 'autocorrect' => 'off'],
$controlAttributes ?? []
)])
2 changes: 1 addition & 1 deletion resources/views/forms/partials/errors.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@if($errors->hasAny($errorsKeys = $errorsKeys ?? $name))
<ul id="{{ $errorsId }}">
<ul id="{{ $errorsId }}" role="list">
@foreach(\Illuminate\Support\Arr::wrap($errorsKeys) as $key)
@foreach(\Illuminate\Support\Arr::flatten($errors->get($key)) as $message)
<li>{{ $message }}</li>
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
2 changes: 1 addition & 1 deletion resources/views/widgets/crumbtrail.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav aria-label="{{ __('Crumb trail') }}" data-kontour-widget="crumbtrail">
<ol>
<ol role="list">
@foreach($links as $link)
<li{!! url()->full() == $link->getUrl() ? ' aria-current="true"' : '' !!}>{{ $link }}</li>
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion resources/views/widgets/itemHistory.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section data-kontour-widget="itemHistory">
<header>{{ trans('kontour::widgets.itemHistory.title') }}</header>
<ul>
<ul role="list">
@foreach($entries as $entry)
<li lang="en" data-kontour-entry-action="{{ $entry['action'] }}"@if($entry['user']) data-kontour-username="{{ $entry['user']->getDisplayName() }}"@endif>
<span>{{ ucfirst($entry['action']) }}</span>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/widgets/menu.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@include('kontour::buttons.hamburger')
<ul data-kontour-widget="menu">
<ul data-kontour-widget="menu" role="list">
@foreach($links as $heading => $headingLinks)
@if(count($headingLinks))
<li>
<small>{{ $heading }}</small>
<ul>
<ul role="list">
@foreach($headingLinks as $link)
<li{!! preg_match('#'.$link->getUrl().'#', url()->full()) ? ' aria-current="true"' : '' !!}>{{ $link }}</li>
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion resources/views/widgets/message.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<section data-kontour-widget="message">
<ol>
<ol role="list">
@foreach($messages as $message)
<li
data-kontour-message-level="{{ $message['level'] }}"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/widgets/personalRecentVisits.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@if(count($visits))
<aside data-kontour-widget="personalRecentVisits">
<header>{{ trans('kontour::widgets.personalRecentVisits.title') }}</header>
<ul>
<ul role="list">
@foreach($visits as $visit)
@if(url()->full() != $visit->getLink()->getUrl())
<li data-kontour-visit-type="{{ $visit->getType() }}"{!! empty($visit->getLink()->getDescription()) ? ' title="' . e($visit->getLink()->getName()) . '"' : '' !!}>{{ $visit->getLink() }}</li>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/widgets/teamRecentVisits.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@if(count($visits))
<aside data-kontour-widget="teamRecentVisits">
<header>{{ trans('kontour::widgets.teamRecentVisits.title') }}</header>
<ul>
<ul role="list">
@foreach($visits as $visit)
<li data-kontour-visit-type="{{ $visit->getType() }}" data-kontour-username="{{ $visit->getUser()->getDisplayName() }}"{!! empty($visit->getLink()->getDescription()) ? ' title="' . e($visit->getLink()->getName()) . '"' : '' !!}>{{ $visit->getLink() }}</li>
@endforeach
Expand Down
89 changes: 89 additions & 0 deletions tests/Feature/FormViewTests/EmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Kontenta\Kontour\Tests\Feature\FormViewTests;

use Illuminate\Support\Facades\View;
use Illuminate\Support\MessageBag;
use Kontenta\Kontour\Tests\IntegrationTest;

class EmailTest extends IntegrationTest
{
public static $defaultEmailAttributes = [
'autocomplete' => 'email',
'autocapitalize' => 'none',
'autocorrect' => 'off',
];

public function test_input_type_defaults_to_email()
{
$output = View::make('kontour::forms.email', [
'errors' => new MessageBag,
])->render();

$this->assertRegExp('/<input[\S\s]*type="email"[\S\s]*>/', $output);
}

public function test_input_type_can_be_specified()
{
$output = View::make('kontour::forms.email', [
'errors' => new MessageBag,
'type' => 'text',
])->render();

$this->assertRegExp('/<input[\S\s]*type="text"[\S\s]*>/', $output);
}

public function test_input_name_defaults_to_email()
{
$output = View::make('kontour::forms.email', [
'errors' => new MessageBag,
])->render();

$this->assertRegExp('/<input[\S\s]*name="email"[\S\s]*>/', $output);
}

public function test_input_name_can_be_specified()
{
$output = View::make('kontour::forms.email', [
'name' => 'test',
'errors' => new MessageBag,
])->render();

$this->assertRegExp('/<input[\S\s]*name="test"[\S\s]*>/', $output);
}

public function test_email_input_has_default_attributes()
{
$output = View::make('kontour::forms.email', [
'errors' => new MessageBag,
])->render();

foreach (self::$defaultEmailAttributes as $attribute => $default) {
$this->assertStringContainsString("$attribute=\"$default\"", $output);
}
}

public function test_control_attributes_can_be_set()
{
$output = View::make('kontour::forms.email', [
'errors' => new MessageBag,
'controlAttributes' => [
'autocomplete' => 'off',
'autocapitalize' => 'on',
'autocorrect' => 'on',
'required',
'a' => 'b',
]
])->render();

$this->assertRegExp('/\s+autocomplete="off"\W/', $output);
$this->assertRegExp('/\s+autocapitalize="on"\W/', $output);
$this->assertRegExp('/\s+autocorrect="on"\W/', $output);
$this->assertRegExp('/\s+required\W/', $output);
$this->assertRegExp('/\s+a="b"\W/', $output);

foreach (self::$defaultEmailAttributes as $attribute => $default) {
$this->assertStringNotContainsString("$attribute=\"$default\"", $output);
}
}
}
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
2 changes: 1 addition & 1 deletion tests/Feature/UserlandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_menu_widget()
$response = $this->actingAs($this->user, 'admin')->get(route('userland.index'));

$response->assertSuccessful();
$response->assertSee('<ul data-kontour-widget="menu">', false);
$response->assertSee('<ul data-kontour-widget="menu" role="list">', false);
$response->assertSee('>main<', false);
$response->assertSee('<a href="' . route('userland.index') . '" aria-current="page">Userland Tool</a>', false);
$response->assertSee('>External<', false);
Expand Down

0 comments on commit 605d549

Please sign in to comment.