0 follower

Final Class Yiisoft\Bootstrap5\Accordion

InheritanceYiisoft\Bootstrap5\Accordion » Yiisoft\Widget\Widget

Accordion renders an accordion bootstrap JavaScript component.

For example:

<?= Accordion::widget()
        ->items(
           AccordionItem::to('Accordion Item #1', 'This is the first item\'s accordion body.'),
           AccordionItem::to('Accordion Item #2', 'This is the second item\'s accordion body.'),
           AccordionItem::to('Accordion Item #3', 'This is the third item\'s accordion body.'),
       )
?>

Public Methods

Hide inherited methods

Method Description Defined By
addAttributes() Adds a sets of attributes. Yiisoft\Bootstrap5\Accordion
addClass() Adds one or more CSS classes to the existing classes. Yiisoft\Bootstrap5\Accordion
addCssStyle() Adds a CSS style. Yiisoft\Bootstrap5\Accordion
addTogglerAttribute() Adds toggler attribute value. Yiisoft\Bootstrap5\Accordion
addTogglerClass() Adds one or more CSS classes to the existing toggler classes. Yiisoft\Bootstrap5\Accordion
addTogglerCssStyle() Adds a toggler CSS style. Yiisoft\Bootstrap5\Accordion
alwaysOpen() Sets whether it should always allow multiple items to be open simultaneously. Yiisoft\Bootstrap5\Accordion
attribute() Adds a sets attribute value. Yiisoft\Bootstrap5\Accordion
attributes() Sets the HTML attributes. Yiisoft\Bootstrap5\Accordion
bodyAttributes() Sets the HTML attributes for the body section. Yiisoft\Bootstrap5\Accordion
class() Replaces all existing CSS classes with the specified one(s). Yiisoft\Bootstrap5\Accordion
collapseAttributes() Sets the HTML attributes for the collapse section. Yiisoft\Bootstrap5\Accordion
flush() Sets whether should use the flush style. Yiisoft\Bootstrap5\Accordion
headerAttributes() Sets the HTML attributes for the header section. Yiisoft\Bootstrap5\Accordion
headerTag() Sets the HTML tag to be used for the header section. Yiisoft\Bootstrap5\Accordion
id() Sets the ID. Yiisoft\Bootstrap5\Accordion
items() Sets the items. Yiisoft\Bootstrap5\Accordion
render() Run the widget. Yiisoft\Bootstrap5\Accordion
togglerAttributes() Sets the HTML attributes for the toggler. Yiisoft\Bootstrap5\Accordion
togglerTag() Sets the HTML tag to be used for the toggler. Yiisoft\Bootstrap5\Accordion

Constants

Hide inherited constants

Constant Value Description Defined By
CLASS_BODY 'accordion-body' Yiisoft\Bootstrap5\Accordion
CLASS_COLLAPSE 'accordion-collapse collapse' Yiisoft\Bootstrap5\Accordion
CLASS_HEADER 'accordion-header' Yiisoft\Bootstrap5\Accordion
CLASS_ITEM 'accordion-item' Yiisoft\Bootstrap5\Accordion
CLASS_TOGGLE 'accordion-button' Yiisoft\Bootstrap5\Accordion
CLASS_TOGGLE_ACTIVE 'collapsed' Yiisoft\Bootstrap5\Accordion
NAME 'accordion' Yiisoft\Bootstrap5\Accordion

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 $accordion->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 $accordion->addCssStyle('color: red');

// or $accordion->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;
}

            
addTogglerAttribute() public method

Adds toggler attribute value.

public self addTogglerAttribute ( 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 $accordion->addTogglerAttribute('data-id', '123'); `

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

            
addTogglerClass() public method

Adds one or more CSS classes to the existing toggler classes.

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

public self addTogglerClass ( \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 addTogglerClass(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    foreach ($class as $item) {
        Html::addCssClass($new->togglerAttributes, $item);
    }
    return $new;
}

            
addTogglerCssStyle() public method

Adds a toggler CSS style.

public self addTogglerCssStyle ( 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 $accordion->addTogglerCssStyle('color: red');

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

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

            
alwaysOpen() public method

Sets whether it should always allow multiple items to be open simultaneously.

See also https://getbootstrap.com/docs/5.3/components/accordion/#always-open Example usage: `php $accordion->alwaysOpen(); `.

public self alwaysOpen ( boolean $enabled true )
$enabled boolean

Whether it should always be open.

return self

A new instance with the updated alwaysOpen property.

                public function alwaysOpen(bool $enabled = true): self
{
    $new = clone $this;
    $new->alwaysOpen = $enabled;
    return $new;
}

            
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 $accordion->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;
}

            
bodyAttributes() public method

Sets the HTML attributes for the body section.

public self bodyAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the body section.

                public function bodyAttributes(array $attributes): self
{
    $new = clone $this;
    $new->bodyAttributes = $attributes;
    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 $accordion->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

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

            
collapseAttributes() public method

Sets the HTML attributes for the collapse section.

public self collapseAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the collapse section.

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

            
flush() public method

Sets whether should use the flush style.

See also https://getbootstrap.com/docs/5.3/components/accordion/#flush Example usage: `php $accordion->flush(); `.

public self flush ( boolean $enabled true )
$enabled boolean

Whether to apply the flush style.

return self

A new instance with the updated CSS class for the flush style.

                public function flush(bool $enabled = true): self
{
    return $this->addClass($enabled ? 'accordion-flush' : null);
}

            
headerAttributes() public method

Sets the HTML attributes for the header section.

public self headerAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the header section.

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

            
headerTag() public method

Sets the HTML tag to be used for the header section.

public self headerTag ( string $tag )
$tag string

The HTML tag name for the header section.

return self

A new instance with the specified header tag.

Example usage: `php $accordion->headerTag('h3'); `

                public function headerTag(string $tag): self
{
    $new = clone $this;
    $new->headerTag = $tag;
    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.

throws InvalidArgumentException

If the ID is an empty string or false.

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

            
items() public method

Sets the items.

public self items ( Yiisoft\Bootstrap5\AccordionItem $items )
$items Yiisoft\Bootstrap5\AccordionItem

The items.

return self

A new instance with the specified items.

Example usage: `php $accordion->items(

AccordionItem::to('Accordion Item #1', 'This is the first item\'s accordion body.'),
AccordionItem::to('Accordion Item #2', 'This is the second item\'s accordion body.'),
AccordionItem::to('Accordion Item #3', 'This is the third item\'s accordion body.'),

); `

                public function items(AccordionItem ...$items): self
{
    $new = clone $this;
    $new->items = $items;
    return $new;
}

            
render() public method

Run the 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']);
    if ($this->items === []) {
        return '';
    }
    $id = $this->getId();
    return Div::tag()
        ->addAttributes($attributes)
        ->addClass(self::NAME, $classes, ...$this->cssClasses)
        ->addContent("\n", $this->renderItems($id), "\n")
        ->id($id)
        ->encode(false)
        ->render();
}

            
togglerAttributes() public method

Sets the HTML attributes for the toggler.

public self togglerAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the toggler.

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

            
togglerTag() public method

Sets the HTML tag to be used for the toggler.

public self togglerTag ( string $tag )
$tag string

The HTML tag name for the toggler.

return self

A new instance with the specified toggler tag.

Example usage: `php $accordion->toggleTag('button'); `

throws InvalidArgumentException

if the tag is an empty string.

                public function togglerTag(string $tag): self
{
    $new = clone $this;
    $new->togglerTag = $tag;
    return $new;
}