0 follower

Final Class Yiisoft\Bootstrap5\ButtonGroup

InheritanceYiisoft\Bootstrap5\ButtonGroup » Yiisoft\Widget\Widget

ButtonGroup renders a button group bootstrap component.

For example,

<?= ButtonGroup::widget()
        ->addClass('btn-lg')
        ->ariaLabel('Basic example')
        ->buttons(
            Button::widget()->label('Left')->variant(ButtonVariant::PRIMARY),
            Button::widget()->label('Middle')->variant(ButtonVariant::PRIMARY),
            Button::widget()->label('Right')->variant(ButtonVariant::PRIMARY),
        )
?>

Pressing on the button should be handled via JavaScript. See the following for details:

Public Methods

Hide inherited methods

Method Description Defined By
addAttributes() Adds a sets of attributes. Yiisoft\Bootstrap5\ButtonGroup
addClass() Adds one or more CSS classes to the existing classes. Yiisoft\Bootstrap5\ButtonGroup
addCssStyle() Adds a CSS style. Yiisoft\Bootstrap5\ButtonGroup
ariaLabel() Sets the ARIA label. Yiisoft\Bootstrap5\ButtonGroup
attribute() Adds a sets attribute value. Yiisoft\Bootstrap5\ButtonGroup
attributes() Sets the HTML attributes. Yiisoft\Bootstrap5\ButtonGroup
buttons() List of buttons. Yiisoft\Bootstrap5\ButtonGroup
class() Replaces all existing CSS classes with the specified one(s). Yiisoft\Bootstrap5\ButtonGroup
id() Sets the ID. Yiisoft\Bootstrap5\ButtonGroup
render() Run the button group widget. Yiisoft\Bootstrap5\ButtonGroup
size() Sets the size. Yiisoft\Bootstrap5\ButtonGroup
vertical() Sets the button group to be vertical. Yiisoft\Bootstrap5\ButtonGroup

Constants

Hide inherited constants

Constant Value Description Defined By
NAME 'btn-group' Yiisoft\Bootstrap5\ButtonGroup

Method Details

Hide inherited methods

addAttributes() public method

Adds a sets of attributes.

public self addAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names. for example, ['id' => 'my-id'].

return self

A new instance with the specified attributes added.

Example usage: `php $buttonGroup->addAttributes(['data-id' => '123']); `

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = [...$this->attributes, ...$attributes];
    return $new;
}

            
addClass() public method

Adds one or more CSS classes to the existing classes.

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self addClass ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or more CSS class names to add. Pass null to skip adding a class.

return self

A new instance with the specified CSS classes added to existing ones.

                public function addClass(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    $new->cssClasses = [...$this->cssClasses, ...$class];
    return $new;
}

            
addCssStyle() public method

Adds a CSS style.

public self addCssStyle ( array|string $style, boolean $overwrite true )
$style array|string

The CSS style. If the value is an array, a space will separate the values. For example, ['color' => 'red', 'font-weight' => 'bold'] will be rendered as color: red; font-weight: bold;. If it is a string, it will be added as is, for example, color: red.

$overwrite boolean

Whether to overwrite existing styles with the same name. If false, the new value will be appended to the existing one.

return self

A new instance with the specified CSS style value added.

Example usage: `php $buttonGroup->addCssStyle('color: red');

// or $buttonGroup->addCssStyle(['color' => 'red', 'font-weight' => 'bold']); `

                public function addCssStyle(array|string $style, bool $overwrite = true): self
{
    $new = clone $this;
    Html::addCssStyle($new->attributes, $style, $overwrite);
    return $new;
}

            
ariaLabel() public method

Sets the ARIA label.

public self ariaLabel ( string $label )
$label string

The ARIA label.

return self

A new instance with the specified ARIA label.

                public function ariaLabel(string $label): self
{
    return $this->attribute('aria-label', $label);
}

            
attribute() public method

Adds a sets attribute value.

public self attribute ( string $name, mixed $value )
$name string

The attribute name.

$value mixed

The attribute value.

return self

A new instance with the specified attribute added.

Example usage: `php $buttonGroup->attribute('data-id', '123'); `

                public function attribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->attributes[$name] = $value;
    return $new;
}

            
attributes() public method

Sets the HTML attributes.

public self attributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes.

                public function attributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
buttons() public method

List of buttons.

public self buttons ( Yiisoft\Bootstrap5\Button|\Yiisoft\Html\Tag\Input\Checkbox|\Yiisoft\Html\Tag\Input\Radio $buttons )
$buttons Yiisoft\Bootstrap5\Button|\Yiisoft\Html\Tag\Input\Checkbox|\Yiisoft\Html\Tag\Input\Radio

The button.

return self

A new instance with the specified buttons.

Example usage: `php $buttonGroup->buttons(

Button::widget()->label('Left')->variant(ButtonVariant::PRIMARY),
Button::widget()->label('Middle')->variant(ButtonVariant::PRIMARY),
Button::widget()->label('Right')->variant(ButtonVariant::PRIMARY),

); `

                public function buttons(Button|Checkbox|Radio ...$buttons): self
{
    $new = clone $this;
    $new->buttons = $buttons;
    return $new;
}

            
class() public method

Replaces all existing CSS classes with the specified one(s).

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self class ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or more CSS class names to set. Pass null to skip setting a class.

return self

A new instance with the specified CSS classes set.

Example usage: `php $buttonGroup->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

                public function class(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    $new->cssClasses = $class;
    return $new;
}

            
id() public method

Sets the ID.

public self id ( boolean|string $id )
$id boolean|string

The ID of the component. If true, an ID will be generated automatically.

return self

A new instance with the specified ID.

Example usage: `php $buttonGroup->id('my-id'); `

                public function id(bool|string $id): self
{
    $new = clone $this;
    $new->id = $id;
    return $new;
}

            
render() public method

Run the button group widget.

public string render ( )
return string

The HTML representation of the element.

                public function render(): string
{
    $attributes = $this->attributes;
    $classes = $attributes['class'] ?? null;
    unset($attributes['class'], $attributes['id']);
    $button = implode("\n", $this->buttons);
    $buttons = $button === '' ? '' : "\n" . $button . "\n";
    if ($buttons === '') {
        return '';
    }
    return Div::tag()
        ->attributes($attributes)
        ->attribute('role', 'group')
        ->addClass(
            self::NAME,
            $classes,
            ...$this->cssClasses,
        )
        ->content($buttons)
        ->encode(false)
        ->id($this->getId())
        ->render();
}

            
size() public method

Sets the size.

public self size ( \Yiisoft\Bootstrap5\ButtonSize|null $size )
$size \Yiisoft\Bootstrap5\ButtonSize|null

The size. If null, the size will not be set.

return self

A new instance with the specified size.

Example usage: `php $buttonGroup->size(ButtonSize::LARGE); `

                public function size(ButtonSize|null $size): self
{
    return $this->addClass($size?->value);
}

            
vertical() public method

Sets the button group to be vertical.

public self vertical ( )
return self

A new instance of the current class with the button group as vertical.

Example usage: `php $buttonGroup->vertical(); `

                public function vertical(): self
{
    return $this->addClass('btn-group-vertical');
}