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 self active ( boolean $enabled ) | ||
| $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 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 content.
| public self content ( string|\Stringable $content ) | ||
| $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 self disabled ( boolean $enabled ) | ||
| $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 self divider ( array $attributes = [], array $itemAttributes = [] ) | ||
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function divider(array $attributes = [], array $itemAttributes = []): self
{
return new self(
DropdownItemType::DIVIDER,
'',
'',
false,
false,
$attributes,
$itemAttributes,
'h6',
);
}
| public array getAttributes ( ) | ||
| return | array |
The HTML attributes for the |
|---|---|---|
public function getAttributes(): array
{
return $this->attributes;
}
| public string|\Stringable getContent ( ) | ||
| return | string|\Stringable |
The content. |
|---|---|---|
public function getContent(): string|Stringable
{
return $this->content;
}
| public string getHeaderTag ( ) | ||
| return | string |
The header tag. |
|---|---|---|
public function getHeaderTag(): string
{
return $this->headerTag;
}
| public array getItemAttributes ( ) | ||
| return | array |
The HTML attributes for the item. |
|---|---|---|
public function getItemAttributes(): array
{
return $this->itemAttributes;
}
| public \Yiisoft\Bootstrap5\DropdownItemType getType ( ) | ||
| return | \Yiisoft\Bootstrap5\DropdownItemType |
The type. |
|---|---|---|
public function getType(): DropdownItemType
{
return $this->type;
}
| public string getUrl ( ) | ||
| return | string |
The URL. |
|---|---|---|
public function getUrl(): string
{
return $this->url;
}
Creates a header dropdown item.
| public static self header ( string|\Stringable $content = '', string $headerTag = 'h6', array $attributes = [], array $itemAttributes = [] ) | ||
| $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 |
| 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 = [],
): self {
if ($headerTag === '') {
throw new InvalidArgumentException('The header tag cannot be empty.');
}
return new self(
DropdownItemType::HEADER,
$content,
'',
false,
false,
$attributes,
$itemAttributes,
$headerTag,
);
}
Sets the header tag.
| public self headerTag ( string $tag ) | ||
| $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 boolean isActive ( ) | ||
| return | boolean |
Whether the item is active. |
|---|---|---|
public function isActive(): bool
{
return $this->active;
}
| public boolean isDisabled ( ) | ||
| return | boolean |
Whether the item is disabled. |
|---|---|---|
public function isDisabled(): bool
{
return $this->disabled;
}
Sets the HTML attributes for the item.
| public self itemAttributes ( array $attributes ) | ||
| $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 self link ( string|\Stringable $content = '', string $url = '#', boolean $active = false, boolean $disabled = false, array $attributes = [], array $itemAttributes = [] ) | ||
| $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 |
| 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 = [],
): 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',
);
}
Creates a list with custom content dropdown item.
| public static self listContent ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
The list content. |
| $attributes | array |
The HTML attributes for the |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function listContent(string|Stringable $content = '', array $attributes = []): self
{
return new self(
DropdownItemType::CUSTOM_CONTENT,
$content,
'',
false,
false,
$attributes,
[],
'h6',
);
}
Creates a text dropdown item.
| public static self text ( string|\Stringable $content = '', array $attributes = [], array $itemAttributes = [] ) | ||
| $content | string|\Stringable |
The text content. |
| $attributes | array |
The HTML attributes for the |
| $itemAttributes | array |
The HTML attributes for the |
| return | self |
A new instance with the specified configuration. Example:
|
|---|---|---|
public static function text(
string|Stringable $content = '',
array $attributes = [],
array $itemAttributes = [],
): self {
return new self(
DropdownItemType::TEXT,
$content,
'',
false,
false,
$attributes,
$itemAttributes,
'h6',
);
}
Sets the type.
| public self type ( \Yiisoft\Bootstrap5\DropdownItemType $type ) | ||
| $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;
}
Signup or Login in order to comment.