Skip to content

Commit

Permalink
Sync Button class with the latest changes from the package. (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Jan 21, 2025
1 parent 3dc4609 commit e9f8a58
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
64 changes: 18 additions & 46 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Yiisoft\Html\Tag\Button as ButtonTag;
use Yiisoft\Html\Tag\Input;

use function array_merge;
use function array_filter;

/**
* Button renders a bootstrap button.
*
Expand All @@ -29,7 +26,7 @@ final class Button extends \Yiisoft\Widget\Widget
private const NAME = 'btn';
private array $attributes = [];
private ButtonVariant|null $buttonVariant = ButtonVariant::SECONDARY;
private array $cssClass = [];
private array $cssClasses = [];
private bool $disabled = false;
private bool|string $id = true;
private string|Stringable $label = '';
Expand Down Expand Up @@ -147,7 +144,7 @@ public function active(bool $value = true): self

$new = $this->toggle($dataBsToggle);
$new->attributes['aria-pressed'] = $ariaPressed;
$new->cssClass['active'] = $activeClass;
$new->cssClasses['active'] = $activeClass;

return $new;
}
Expand All @@ -162,7 +159,7 @@ public function active(bool $value = true): self
public function addAttributes(array $values): self
{
$new = clone $this;
$new->attributes = array_merge($this->attributes, $values);
$new->attributes = [...$this->attributes, ...$values];

return $new;
}
Expand All @@ -173,7 +170,7 @@ public function addAttributes(array $values): self
* Multiple classes can be added by passing them as separate arguments. `null` values are filtered out
* automatically.
*
* @param string|null ...$value One or more CSS class names to add. Pass `null` to skip adding a class.
* @param string|null ...$values One or more CSS class names to add. Pass `null` to skip adding a class.
* For example:
*
* ```php
Expand All @@ -184,13 +181,10 @@ public function addAttributes(array $values): self
*
* @link https://html.spec.whatwg.org/#classes
*/
public function addClass(string|null ...$value): self
public function addClass(string|null ...$values): self
{
$new = clone $this;
$new->cssClass = array_merge(
$new->cssClass,
array_filter($value, static fn ($v) => $v !== null)
);
$new->cssClasses = [...$this->cssClasses, ...$values];

return $new;
}
Expand Down Expand Up @@ -256,7 +250,7 @@ public function attributes(array $values): self
* Multiple classes can be added by passing them as separate arguments. `null` values are filtered out
* automatically.
*
* @param string|null ...$value One or more CSS class names to set. Pass `null` to skip setting a class.
* @param string|null ...$values One or more CSS class names to set. Pass `null` to skip setting a class.
* For example:
*
* ```php
Expand All @@ -265,10 +259,10 @@ public function attributes(array $values): self
*
* @return self A new instance with the specified CSS classes set.
*/
public function class(string|null ...$value): self
public function class(string|null ...$values): self
{
$new = clone $this;
$new->cssClass = array_filter($value, static fn ($v) => $v !== null);
$new->cssClasses = $values;

return $new;
}
Expand All @@ -281,7 +275,7 @@ public function class(string|null ...$value): self
public function disableTextWrapping(): self
{
$new = clone $this;
$new->cssClass['text-nowrap'] = 'text-nowrap';
$new->cssClasses['text-nowrap'] = 'text-nowrap';

return $new;
}
Expand Down Expand Up @@ -345,7 +339,7 @@ public function label(string|Stringable $value, bool $encode = true): self
public function largeSize(): self
{
$new = clone $this;
$new->cssClass['size'] = 'btn-lg';
$new->cssClasses['size'] = 'btn-lg';

return $new;
}
Expand All @@ -358,7 +352,7 @@ public function largeSize(): self
public function normalSize(): self
{
$new = clone $this;
$new->cssClass['size'] = null;
$new->cssClasses['size'] = null;

return $new;
}
Expand All @@ -371,7 +365,7 @@ public function normalSize(): self
public function smallSize(): self
{
$new = clone $this;
$new->cssClass['size'] = 'btn-sm';
$new->cssClasses['size'] = 'btn-sm';

return $new;
}
Expand All @@ -395,12 +389,9 @@ public function toggle(string|null $value = 'button'): self
}

/**
* Sets the button type. The following options are allowed:
* - `ButtonType::LINK`: A link button.
* - `ButtonType::RESET`: A reset button.
* - `ButtonType::RESET_INPUT`: A reset button input.
* - `ButtonType::SUBMIT`: A submit button.
* - `ButtonType::SUBMIT_INPUT`: A submit button input.
* Sets the button type.
*
* @param ButtonType $value The button type.
*/
public function type(ButtonType $value): self
{
Expand Down Expand Up @@ -432,26 +423,7 @@ public function url(string|null $value): self
}

/**
* Set the button variant. The following options are allowed:
*
* - `ButtonVariant::PRIMARY`: Primary button.
* - `ButtonVariant::SECONDARY`: Secondary button.
* - `ButtonVariant::SUCCESS`: Success button.
* - `ButtonVariant::DANGER`: Danger button.
* - `ButtonVariant::WARNING`: Warning button.
* - `ButtonVariant::INFO`: Info button.
* - `ButtonVariant::LIGHT`: Light button.
* - `ButtonVariant::DARK`: Dark button.
* - `ButtonVariant::LINK`: Link button.
* - `ButtonVariant::OUTLINE_PRIMARY`: Primary outline button.
* - `ButtonVariant::OUTLINE_SECONDARY`: Secondary outline button.
* - `ButtonVariant::OUTLINE_SUCCESS`: Success outline button.
* - `ButtonVariant::OUTLINE_DANGER`: Danger outline button.
* - `ButtonVariant::OUTLINE_WARNING`: Warning outline button.
* - `ButtonVariant::OUTLINE_INFO`: Info outline button.
* - `ButtonVariant::OUTLINE_LIGHT`: Light outline button.
* - `ButtonVariant::OUTLINE_DARK`: Dark outline button.
* - `null`: No variant set.
* Set the button variant.
*
* @param ButtonVariant $value The button variant. If `null`, the variant will not be set.
*
Expand Down Expand Up @@ -488,7 +460,7 @@ public function render(): string
Html::addCssClass($attributes, [self::NAME, $this->buttonVariant?->value, $classes]);

$attributes = $this->setAttributes($attributes);
$tag = $tag->addAttributes($attributes)->addClass(...$this->cssClass)->id($id);
$tag = $tag->addAttributes($attributes)->addClass(...$this->cssClasses)->id($id);

if ($tag instanceof Input) {
if ($this->label !== '') {
Expand Down
17 changes: 16 additions & 1 deletion src/ButtonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@
namespace Yiisoft\Yii\Bootstrap5;

/**
* Types for the button component.
* Types for the {@see \Yiisoft\Yii\Bootstrap5\Button} component.
*/
enum ButtonType: string
{
/**
* Link button, renders as `<a>` element.
*/
case LINK = 'link';
/**
* Reset button, renders as `<button type="reset">`.
*/
case RESET = 'reset';
/**
* Reset input, renders as `<input type="reset">`.
*/
case RESET_INPUT = 'reset-input';
/**
* Submit button, renders as `<button type="submit">`.
*/
case SUBMIT = 'submit';
/**
* Submit input, renders as `<input type="submit">`.
*/
case SUBMIT_INPUT = 'submit-input';
}
53 changes: 52 additions & 1 deletion src/ButtonVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,79 @@
namespace Yiisoft\Yii\Bootstrap5;

/**
* Variants for the button component.
* Variants for the {@see \Yiisoft\Yii\Bootstrap5\Button} component.
*
* @see https://getbootstrap.com/docs/5.3/components/buttons/#variants
* @see https://getbootstrap.com/docs/5.3/components/buttons/#outline-buttons
*/
enum ButtonVariant: string
{
/**
* Primary button variant.
*/
case PRIMARY = 'btn-primary';
/**
* Secondary button variant.
*/
case SECONDARY = 'btn-secondary';
/**
* Success button variant.
*/
case SUCCESS = 'btn-success';
/**
* Danger button variant.
*/
case DANGER = 'btn-danger';
/**
* Warning button variant.
*/
case WARNING = 'btn-warning';
/**
* Info button variant.
*/
case INFO = 'btn-info';
/**
* Light button variant.
*/
case LIGHT = 'btn-light';
/**
* Link button variant.
*/
case LINK = 'btn-link';
/**
* Dark button variant.
*/
case DARK = 'btn-dark';
/**
* Outline primary button variant.
*/
case OUTLINE_PRIMARY = 'btn-outline-primary';
/**
* Outline secondary button variant.
*/
case OUTLINE_SECONDARY = 'btn-outline-secondary';
/**
* Outline success button variant.
*/
case OUTLINE_SUCCESS = 'btn-outline-success';
/**
* Outline danger button variant.
*/
case OUTLINE_DANGER = 'btn-outline-danger';
/**
* Outline warning button variant.
*/
case OUTLINE_WARNING = 'btn-outline-warning';
/**
* Outline info button variant.
*/
case OUTLINE_INFO = 'btn-outline-info';
/**
* Outline light button variant.
*/
case OUTLINE_LIGHT = 'btn-outline-light';
/**
* Outline dark button variant.
*/
case OUTLINE_DARK = 'btn-outline-dark';
}

0 comments on commit e9f8a58

Please sign in to comment.