Skip to content

Commit

Permalink
Fix button classes (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 19, 2024
1 parent 41470ff commit 1481b88
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 151 deletions.
2 changes: 0 additions & 2 deletions docs/form/input/Button.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ The following methods are available for setting attributes:
| `lang()` | Set the `lang` attribute. |
| `name()` | Set the `name` attribute. |
| `readOnly()` | Set the `readonly` attribute. |
| `step()` | Set the `step` attribute. |
| `style()` | Set the `style` attribute. |
| `tabIndex()` | Set the `tabindex` attribute. |
| `title()` | Set the `title` attribute. |
| `type()` | Set the `type` attribute. |
| `value()` | Set the `value` attribute. |

## Custom methods
Expand Down
6 changes: 3 additions & 3 deletions docs/form/input/ButtonGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Add buttons to the `ButtonGroup` widget using the `buttons` method.

```php
$buttonGroup->buttons(
ButtonGroup::widget()->labelContent('Submit')->type('submit'),
ButtonGroup::widget()->labelContent('Reset')->type('reset')
Submit::widget()->value('Submit'),
Reset::widget()->value('Reset'),
);
```

Expand Down Expand Up @@ -53,7 +53,7 @@ The following methods are available for customizing the `HTML` output:

| Method | Description |
| ---------------------- | ------------------------------------------------------------------------------------------- |
| `buttons()` | Set the `buttons` for the `ButtonGroup` widget. |
| `buttons()` | Set the `button` for the `ButtonGroup` widget. |
| `container()` | Set enabled or disabled for the `container` element. |
| `containerAttributes()`| Set `attributes` for the `container` element. |
| `containerClass()` | Set the `class` attribute for the `container` element. |
Expand Down
13 changes: 0 additions & 13 deletions src/Attribute/Custom/HasWidgetValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,6 @@ private function validateTagName(string $tagName, string ...$compare): void
$this->validateInList($tagName, '%s::class widget must have a tag name of %s.', ...$compare);
}

/**
* Validate if the value is valid type.
*
* @param string $type The type to validate.
* @param string ...$compare The types to compare.
*
* @throws InvalidArgumentException If the value is invalid.
*/
private function validateType(string $type, string ...$compare): void
{
$this->validateInList($type, '%s::class widget must have a type of %s.', ...$compare);
}

/**
* Validate if the value is in a valid list.
*
Expand Down
88 changes: 42 additions & 46 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

namespace PHPForge\Html;

use PHPForge\Html\Attribute\Aria\{
HasAriaControls,
HasAriaDescribedBy,
HasAriaDisabled,
HasAriaExpanded,
HasAriaLabel,
HasRole
};
use PHPForge\Html\Attribute\Custom\{
HasAttributes,
HasContainer,
HasContent,
HasPrefixAndSuffix,
HasTagName,
HasTemplate,
HasWidgetValidation
};
use PHPForge\Html\Attribute\Input\HasName;
use PHPForge\Html\Attribute\{HasClass, HasData, HasId, HasLang, HasStyle, HasTabindex, HasTitle};
use PHPForge\Widget\Element;

/**
Expand All @@ -15,50 +34,30 @@
*/
final class Button extends Element
{
use Attribute\Aria\HasAriaControls;
use Attribute\Aria\HasAriaDescribedBy;
use Attribute\Aria\HasAriaDisabled;
use Attribute\Aria\HasAriaExpanded;
use Attribute\Aria\HasAriaLabel;
use Attribute\Aria\HasRole;
use Attribute\Custom\HasAttributes;
use Attribute\Custom\HasContainer;
use Attribute\Custom\HasContent;
use Attribute\Custom\HasPrefixAndSuffix;
use Attribute\Custom\HasTagName;
use Attribute\Custom\HasTemplate;
use Attribute\Custom\HasWidgetValidation;
use Attribute\HasClass;
use Attribute\HasData;
use Attribute\HasId;
use Attribute\HasLang;
use Attribute\HasStyle;
use Attribute\HasTabindex;
use Attribute\HasTitle;
use Attribute\Input\HasName;
use Attribute\Input\HasType;
use HasAriaControls;
use HasAriaDescribedBy;
use HasAriaDisabled;
use HasAriaExpanded;
use HasAriaLabel;
use HasAttributes;
use HasClass;
use HasContainer;
use HasContent;
use HasData;
use HasId;
use HasLang;
use HasName;
use HasPrefixAndSuffix;
use HasRole;
use HasStyle;
use HasTabindex;
use HasTagName;
use HasTemplate;
use HasTitle;
use HasWidgetValidation;

protected array $attributes = [];

/**
* Set the button type to `submit`.
*
* @return self A new instance of the current class with the specified type value.
*/
public function submit(): self
{
return $this->type('submit');
}

/**
* Set the button type to `reset`.
*
* @return self A new instance of the current class with the specified type value.
*/
public function reset(): self
{
return $this->type('reset');
}
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
Expand All @@ -69,7 +68,6 @@ protected function loadDefaultDefinitions(): array
'id()' => [$this->generateId('button-')],
'template()' => ['{prefix}\n{tag}\n{suffix}'],
'tagName()' => ['button'],
'type()' => ['button'],
];
}

Expand All @@ -81,11 +79,8 @@ protected function loadDefaultDefinitions(): array
protected function run(): string
{
$attributes = $this->attributes;
/** @var string $type */
$type = $attributes['type'] ?? 'button';

$this->validateTagName($this->tagName, 'a', 'button');
$this->validateType($type, 'button', 'reset', 'submit');

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$this->id-help";
Expand All @@ -111,6 +106,7 @@ protected function run(): string
->suffixContainerTag($this->suffixContainerTag)
->tagName($this->tagName)
->template($this->template)
->type($this->type)
->render()
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Input/Base/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
HasTemplate,
HasWidgetValidation
};
use PHPForge\Html\Attribute\Input\{CanBeDisabled, CanBeReadonly, HasForm, HasName, HasType, HasValue};
use PHPForge\Html\Attribute\Input\{CanBeDisabled, CanBeReadonly, HasForm, HasName, HasValue};
use PHPForge\Html\Attribute\{
CanBeAutofocus,
CanBeHidden,
Expand Down Expand Up @@ -50,11 +50,11 @@ abstract class AbstractButton extends Element
use HasTabindex;
use HasTemplate;
use HasTitle;
use HasType;
use HasValue;
use HasWidgetValidation;

protected array $attributes = [];
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
Expand All @@ -65,7 +65,6 @@ protected function loadDefaultDefinitions(): array
'container()' => [true],
'id()' => [$this->generateId('button-')],
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
'type()' => ['button'],
];
}

Expand Down Expand Up @@ -95,6 +94,7 @@ protected function run(): string
->suffixContainerTag($this->suffixContainerTag)
->tagName('input')
->template($this->template)
->type($this->type)
->tokenValues(['{label}' => $this->renderLabelTag($labelFor)])
->render()
);
Expand Down
8 changes: 5 additions & 3 deletions src/Input/Base/AbstractButtonGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PHPForge\Html\Attribute;
use PHPForge\Html\Input\Button;
use PHPForge\Html\Input\Reset;
use PHPForge\Html\Input\Submit;
use PHPForge\Widget\Element;

use function implode;
Expand All @@ -15,7 +17,7 @@ abstract class AbstractButtonGroup extends Element
use Attribute\Custom\HasContainer;

/**
* @psalm-var Button[]
* @psalm-var Button[]|Reset[]|Submit[]
*/
protected array $buttons = [];
protected bool $individualContainer = false;
Expand All @@ -25,9 +27,9 @@ abstract class AbstractButtonGroup extends Element
*
* @param array $values The list of buttons.
*
* @psalm-param Button[] $values
* @psalm-param Button[]|Reset[]|Submit[] $values
*/
public function buttons(Button ...$values): static
public function buttons(Button|Reset|Submit ...$values): static
{
$new = clone $this;
$new->buttons = $values;
Expand Down
3 changes: 2 additions & 1 deletion src/Input/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
final class Reset extends Base\AbstractButton
{
protected string $type = 'reset';

/**
* This method is used to configure the widget with the provided default definitions.
*/
Expand All @@ -20,7 +22,6 @@ protected function loadDefaultDefinitions(): array
'container()' => [true],
'id()' => [$this->generateId('reset-')],
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
'type()' => ['reset'],
];
}
}
3 changes: 2 additions & 1 deletion src/Input/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
final class Submit extends Base\AbstractButton
{
protected string $type = 'submit';

/**
* This method is used to configure the widget with the provided default definitions.
*/
Expand All @@ -20,7 +22,6 @@ protected function loadDefaultDefinitions(): array
'container()' => [true],
'id()' => [$this->generateId('submit-')],
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
'type()' => ['submit'],
];
}
}
20 changes: 0 additions & 20 deletions tests/Button/CustomMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,6 @@ public function testRender(): void
);
}

public function testReset(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-658716145f1d9" type="reset"></button>
HTML,
Button::widget()->id('button-658716145f1d9')->reset()->render()
);
}

public function testSubmit(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-658716145f1d9" type="submit"></button>
HTML,
Button::widget()->id('button-658716145f1d9')->submit()->render()
);
}

public function testSuffix(): void
{
Assert::equalsWithoutLE(
Expand Down
8 changes: 0 additions & 8 deletions tests/Button/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public function testTagName(): void
Button::widget()->tagName('div')->render();
}

public function testType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Button::class widget must have a type of button, reset, submit.');

Button::widget()->type('text')->render();
}

public function testWithoutTagName(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
12 changes: 0 additions & 12 deletions tests/Input/Button/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,6 @@ public function testTitle(): void
);
}

public function testType(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<input id="button-6582f2d099e8b" type="reset">
</div>
HTML,
Button::widget()->id('button-6582f2d099e8b')->type('reset')->render()
);
}

public function testValue(): void
{
Assert::equalsWithoutLE(
Expand Down

0 comments on commit 1481b88

Please sign in to comment.