Skip to content

Commit

Permalink
Merge pull request #219 from raveren/main
Browse files Browse the repository at this point in the history
Add autocomplete attribute helper to input, select and textarea
  • Loading branch information
freekmurze authored Mar 23, 2024
2 parents 586dea9 + e62a28d commit 4f418db
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/general-usage/element-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ echo Element::withTag('p')->text('This is the content!');

## `Input`

- `function autocomplete(?$autocomplete)`
- `function autofocus(?$autofocus)`
- `function checked($checked = true)`
- `function disabled($disabled = true)`
Expand Down Expand Up @@ -103,6 +104,7 @@ echo Element::withTag('p')->text('This is the content!');

## `Select`

- `function autocomplete(?$autocomplete)`
- `function autofocus(?$autofocus)`
- `function disabled(?$disabled)`
- `function isReadonly(?$readonly)`
Expand All @@ -118,6 +120,7 @@ echo Element::withTag('p')->text('This is the content!');

## `Textarea`

- `function autocomplete(?$autocomplete)`
- `function autofocus()`
- `function cols(int $cols)`
- `function disabled(?$disabled)`
Expand Down
24 changes: 24 additions & 0 deletions src/Elements/Attributes/Autocomplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\Html\Elements\Attributes;

trait Autocomplete
{
/**
* @param bool|string $autocomplete
*
* @return static
*/
public function autocomplete($autocomplete = true)
{
$value = 'on';
if ($autocomplete === false) {
$value = 'off';
}
if (is_string($autocomplete)) {
$value = $autocomplete;
}

return $this->attribute('autocomplete', $value);
}
}
2 changes: 2 additions & 0 deletions src/Elements/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Html\Elements;

use Spatie\Html\BaseElement;
use Spatie\Html\Elements\Attributes\Autocomplete;
use Spatie\Html\Elements\Attributes\Autofocus;
use Spatie\Html\Elements\Attributes\Disabled;
use Spatie\Html\Elements\Attributes\MinMaxLength;
Expand All @@ -16,6 +17,7 @@
class Input extends BaseElement
{
use Autofocus;
use Autocomplete;
use Disabled;
use MinMaxLength;
use Name;
Expand Down
2 changes: 2 additions & 0 deletions src/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Html\BaseElement;
use Spatie\Html\Elements\Attributes\Autocomplete;
use Spatie\Html\Elements\Attributes\Autofocus;
use Spatie\Html\Elements\Attributes\Disabled;
use Spatie\Html\Elements\Attributes\Name;
Expand All @@ -15,6 +16,7 @@
class Select extends BaseElement
{
use Autofocus;
use Autocomplete;
use Disabled;
use Name;
use Required;
Expand Down
2 changes: 2 additions & 0 deletions src/Elements/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Html\Elements;

use Spatie\Html\BaseElement;
use Spatie\Html\Elements\Attributes\Autocomplete;
use Spatie\Html\Elements\Attributes\Autofocus;
use Spatie\Html\Elements\Attributes\Disabled;
use Spatie\Html\Elements\Attributes\MinMaxLength;
Expand All @@ -14,6 +15,7 @@
class Textarea extends BaseElement
{
use Autofocus;
use Autocomplete;
use Placeholder;
use Name;
use Required;
Expand Down
6 changes: 6 additions & 0 deletions tests/Elements/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
Input::create()->required(false)
);

it('can create a non-autocompleted input')
->assertHtmlStringEqualsHtmlString(
'<input autocomplete="off">',
Input::create()->autocomplete(false)
);

it('can create an input that has autofocus')
->assertHtmlStringEqualsHtmlString(
'<input autofocus>',
Expand Down
8 changes: 8 additions & 0 deletions tests/Elements/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@
Select::create()->autofocus()->options(['value1' => 'text1'])->render()
);

it('can create a non-autocompleted select')
->assertHtmlStringEqualsHtmlString(
'<select autocomplete="off">
<option value="value1">text1</option>
</select>',
Select::create()->autocomplete('off')->options(['value1' => 'text1'])->render()
);

it('can create a select that has autofocus when passing true')
->assertHtmlStringEqualsHtmlString(
'<select autofocus>
Expand Down
20 changes: 20 additions & 0 deletions tests/Elements/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
Textarea::create()->autofocus()
);

it('can create a non-autocompleted textarea')
->assertHtmlStringEqualsHtmlString(
'<textarea autocomplete="off"></textarea>',
Textarea::create()->autocomplete('off')
)
->assertHtmlStringEqualsHtmlString(
'<textarea autocomplete="off"></textarea>',
Textarea::create()->autocomplete(false)
);

it('can create a custom-autocompleted textarea')
->assertHtmlStringEqualsHtmlString(
'<textarea autocomplete="on"></textarea>',
Textarea::create()->autocomplete()
)
->assertHtmlStringEqualsHtmlString(
'<textarea autocomplete="other-field"></textarea>',
Textarea::create()->autocomplete('other-field')
);

it('can create a textarea with a placeholder')
->assertHtmlStringEqualsHtmlString(
'<textarea placeholder="Lorem ipsum"></textarea>',
Expand Down

0 comments on commit 4f418db

Please sign in to comment.