Final Class Yiisoft\Bootstrap5\DropdownItem
| Inheritance | Yiisoft\Bootstrap5\DropdownItem |
|---|
DropdownItem represents a single item within a Bootstrap Dropdown menu.
Supports multiple item types:
- Buttons: Button-style menu items.
- Dividers: Horizontal separators.
- Headers: Section headers.
- Links: Standard clickable menu items.
- Text: Plain text items.
Example usage:
`php
// Create a link item
DropdownItem::link('Profile', '/profile');
// Create an active item DropdownItem::link('Dashboard', '/dashboard', true);
// Create a divider DropdownItem::divider();
// Create a header
DropdownItem::header('Section Title');
`
Public Methods
Method Details
Sets the active state.
| public active( boolean $enabled ): self | ||
| $enabled | boolean |
Whether is active or not. |
| return | self |
A new instance with the specified active state. |
|---|---|---|
public function active(bool $enabled): self
{
if ($enabled && $this->disabled) {
throw new InvalidArgumentException('The dropdown item cannot be active and disabled at the same time.');
}
$new = clone $this;
$new->active = $enabled;
return $new;
}
Sets the HTML attributes.
| public attributes( array $attributes ): self | ||
| $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 content.
| public content( string|\Stringable $content ): self | ||
| $content | string|\Stringable |
The content. |
| return | self |
A new instance with the specified content. |
|---|---|---|
public function content(string|Stringable $content): self
{
$new = clone $this;
$new->content = $content;
return $new;
}
Sets the disabled state.
| public disabled( boolean $enabled ): self | ||
| $enabled | boolean |
Whether is disabled or not. |
| return | self |
A new instance with the specified disabled state. |
|---|---|---|
public function disabled(bool $enabled): self
{
if ($enabled && $this->active) {
throw new InvalidArgumentException('The dropdown item cannot be active and disabled at the same time.');
}
$new = clone $this;
$new->disabled = $enabled;
return $new;
}
Creates a divider dropdown item.
| public static divider( array $attributes = [], array $itemAttributes = [], boolean $visible = true ): self | ||
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| $visible | boolean |
Whether the item is visible. |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function divider(array $attributes = [], array $itemAttributes = [], bool $visible = true): self
{
return new self(
DropdownItemType::DIVIDER,
'',
'',
false,
false,
$attributes,
$itemAttributes,
'h6',
$visible,
);
}
| public getAttributes( ): array | ||
| return | array |
The HTML attributes for the |
|---|---|---|
public function getAttributes(): array
{
return $this->attributes;
}
| public getContent( ): string|\Stringable | ||
| return | string|\Stringable |
The content. |
|---|---|---|
public function getContent(): string|Stringable
{
return $this->content;
}
| public getHeaderTag( ): string | ||
| return | string |
The header tag. |
|---|---|---|
public function getHeaderTag(): string
{
return $this->headerTag;
}
| public getItemAttributes( ): array | ||
| return | array |
The HTML attributes for the item. |
|---|---|---|
public function getItemAttributes(): array
{
return $this->itemAttributes;
}
| public getType( ): \Yiisoft\Bootstrap5\DropdownItemType | ||
| return | \Yiisoft\Bootstrap5\DropdownItemType |
The type. |
|---|---|---|
public function getType(): DropdownItemType
{
return $this->type;
}
| public getUrl( ): string | ||
| return | string |
The URL. |
|---|---|---|
public function getUrl(): string
{
return $this->url;
}
Creates a header dropdown item.
| public static header( string|\Stringable $content = '', string $headerTag = 'h6', array $attributes = [], array $itemAttributes = [], boolean $visible = true ): self | ||
| $content | string|\Stringable |
The header text. |
| $headerTag | string |
The HTML tag to use (defaults to |
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| $visible | boolean |
Whether the item is visible. |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
| throws | InvalidArgumentException |
When header tag is empty. |
public static function header(
string|Stringable $content = '',
string $headerTag = 'h6',
array $attributes = [],
array $itemAttributes = [],
bool $visible = true,
): self {
if ($headerTag === '') {
throw new InvalidArgumentException('The header tag cannot be empty.');
}
return new self(
DropdownItemType::HEADER,
$content,
'',
false,
false,
$attributes,
$itemAttributes,
$headerTag,
$visible,
);
}
Sets the header tag.
| public headerTag( string $tag ): self | ||
| $tag | string |
The header tag. |
| return | self |
A new instance with the specified header tag. |
|---|---|---|
public function headerTag(string $tag): self
{
if ($tag === '') {
throw new InvalidArgumentException('The header tag cannot be empty.');
}
$new = clone $this;
$new->headerTag = $tag;
return $new;
}
| public isActive( ): boolean | ||
| return | boolean |
Whether the item is active. |
|---|---|---|
public function isActive(): bool
{
return $this->active;
}
| public isDisabled( ): boolean | ||
| return | boolean |
Whether the item is disabled. |
|---|---|---|
public function isDisabled(): bool
{
return $this->disabled;
}
| public isVisible( ): boolean | ||
| return | boolean |
Whether the item is visible. |
|---|---|---|
public function isVisible(): bool
{
return $this->visible;
}
Sets the HTML attributes for the item.
| public itemAttributes( array $attributes ): self | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the item. |
|---|---|---|
public function itemAttributes(array $attributes): self
{
$new = clone $this;
$new->itemAttributes = $attributes;
return $new;
}
Creates a link-type dropdown item.
| public static link( string|\Stringable $content = '', string $url = '#', boolean $active = false, boolean $disabled = false, array $attributes = [], array $itemAttributes = [], boolean $visible = true ): self | ||
| $content | string|\Stringable |
The link text. |
| $url | string |
The URL (defaults to '#'). |
| $active | boolean |
Whether the link is active. |
| $disabled | boolean |
Whether the link is disabled. |
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| $visible | boolean |
Whether the item is visible. |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
| throws | InvalidArgumentException |
When item is set as both active and disabled. |
public static function link(
string|Stringable $content = '',
string $url = '#',
bool $active = false,
bool $disabled = false,
array $attributes = [],
array $itemAttributes = [],
bool $visible = true,
): self {
if ($active && $disabled) {
throw new InvalidArgumentException('The dropdown item cannot be active and disabled at the same time.');
}
return new self(
DropdownItemType::LINK,
$content,
$url,
$active,
$disabled,
$attributes,
$itemAttributes,
'h6',
$visible,
);
}
Creates a list with custom content dropdown item.
| public static listContent( string|\Stringable $content = '', array $attributes = [], boolean $visible = true ): self | ||
| $content | string|\Stringable |
The list content. |
| $attributes | array |
The HTML attributes for the |
| $visible | boolean |
Whether the item is visible. |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function listContent(string|Stringable $content = '', array $attributes = [], bool $visible = true): self
{
return new self(
DropdownItemType::CUSTOM_CONTENT,
$content,
'',
false,
false,
$attributes,
[],
'h6',
$visible,
);
}
Creates a text dropdown item.
| public static text( string|\Stringable $content = '', array $attributes = [], array $itemAttributes = [], boolean $visible = true ): self | ||
| $content | string|\Stringable |
The text content. |
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| $visible | boolean |
Whether the item is visible. |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function text(
string|Stringable $content = '',
array $attributes = [],
array $itemAttributes = [],
bool $visible = true,
): self {
return new self(
DropdownItemType::TEXT,
$content,
'',
false,
false,
$attributes,
$itemAttributes,
'h6',
$visible,
);
}
Sets the type.
| public type( \Yiisoft\Bootstrap5\DropdownItemType $type ): self | ||
| $type | \Yiisoft\Bootstrap5\DropdownItemType |
The type. |
| return | self |
A new instance with the specified type. |
|---|---|---|
public function type(DropdownItemType $type): self
{
$new = clone $this;
$new->type = $type;
return $new;
}
Sets the URL.
| public url( string $url ): self | ||
| $url | string |
The URL. |
| return | self |
A new instance with the specified URL. |
|---|---|---|
public function url(string $url): self
{
$new = clone $this;
$new->url = $url;
return $new;
}
Sets the visibility of the dropdown item.
| public visible( boolean $enabled ): self | ||
| $enabled | boolean |
Whether the dropdown item is visible. |
| return | self |
A new instance with the specified visibility. |
|---|---|---|
public function visible(bool $enabled): self
{
$new = clone $this;
$new->visible = $enabled;
return $new;
}
Signup or Login in order to comment.