Final Class Yiisoft\Bootstrap5\Accordion
| Inheritance | Yiisoft\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
| 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
| 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
Adds a sets of attributes.
| public self addAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. for example, |
| return | self |
A new instance with the specified attributes added. Example usage:
|
|---|---|---|
public function addAttributes(array $attributes): self
{
$new = clone $this;
$new->attributes = [...$this->attributes, ...$attributes];
return $new;
}
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 |
| 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;
}
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, |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// 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;
}
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:
|
|---|---|---|
public function addTogglerAttribute(string $name, mixed $value): self
{
$new = clone $this;
$new->togglerAttributes[$name] = $value;
return $new;
}
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 |
| 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;
}
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, |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// 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;
}
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;
}
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:
|
|---|---|---|
public function attribute(string $name, mixed $value): self
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
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;
}
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;
}
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 |
| return | self |
A new instance with the specified CSS classes set. Example usage:
|
|---|---|---|
public function class(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->cssClasses = $class;
return $new;
}
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;
}
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);
}
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;
}
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:
|
|---|---|---|
public function headerTag(string $tag): self
{
$new = clone $this;
$new->headerTag = $tag;
return $new;
}
Sets the ID.
| public self id ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the component. If |
| return | self |
A new instance with the specified ID. |
|---|---|---|
| throws | InvalidArgumentException |
If the ID is an empty string or |
public function id(bool|string $id): self
{
$new = clone $this;
$new->id = $id;
return $new;
}
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:
);
|
|---|---|---|
public function items(AccordionItem ...$items): self
{
$new = clone $this;
$new->items = $items;
return $new;
}
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();
}
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;
}
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:
|
|---|---|---|
| throws | InvalidArgumentException |
if the tag is an empty string. |
public function togglerTag(string $tag): self
{
$new = clone $this;
$new->togglerTag = $tag;
return $new;
}
Signup or Login in order to comment.