0 follower

Final Class Yiisoft\Bootstrap5\DropdownItem

InheritanceYiisoft\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'); `

Method Details

Hide inherited methods

active() public method

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

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

            
button() public static method

Creates a button-type dropdown item.

public static self button ( string|\Stringable $content '', array $attributes = [], array $itemAttributes = [] )
$content string|\Stringable

The button content.

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <button> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::button('Submit', active: true); `

throws InvalidArgumentException

When item is set as both active and disabled.

                public static function button(
    string|Stringable $content = '',
    array $attributes = [],
    array $itemAttributes = [],
): self {
    return new self(
        DropdownItemType::BUTTON,
        $content,
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
    );
}

            
content() public method

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

            
disabled() public method

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

            
divider() public static method

Creates a divider dropdown item.

public static self divider ( array $attributes = [], array $itemAttributes = [] )
$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <hr> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::divider(); `

                public static function divider(array $attributes = [], array $itemAttributes = []): self
{
    return new self(
        DropdownItemType::DIVIDER,
        '',
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
    );
}

            
getAttributes() public method

public array getAttributes ( )
return array

The HTML attributes for the <li> tag.

                public function getAttributes(): array
{
    return $this->attributes;
}

            
getContent() public method

public string|\Stringable getContent ( )
return string|\Stringable

The content.

                public function getContent(): string|Stringable
{
    return $this->content;
}

            
getHeaderTag() public method

public string getHeaderTag ( )
return string

The header tag.

                public function getHeaderTag(): string
{
    return $this->headerTag;
}

            
getItemAttributes() public method

public array getItemAttributes ( )
return array

The HTML attributes for the item.

                public function getItemAttributes(): array
{
    return $this->itemAttributes;
}

            
getType() public method

public \Yiisoft\Bootstrap5\DropdownItemType getType ( )
return \Yiisoft\Bootstrap5\DropdownItemType

The type.

                public function getType(): DropdownItemType
{
    return $this->type;
}

            
getUrl() public method

public string getUrl ( )
return string

The URL.

                public function getUrl(): string
{
    return $this->url;
}

            
header() public static method

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 h6).

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <h6> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::header('Section Title'); `

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,
    );
}

            
headerTag() public method

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

            
isActive() public method

public boolean isActive ( )
return boolean

Whether the item is active.

                public function isActive(): bool
{
    return $this->active;
}

            
isDisabled() public method

public boolean isDisabled ( )
return boolean

Whether the item is disabled.

                public function isDisabled(): bool
{
    return $this->disabled;
}

            
itemAttributes() public method

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

            
link() public static method

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 <li> tag.

$itemAttributes array

The HTML attributes for the <a> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::link('Profile', '/profile'); `

throws InvalidArgumentException

When item is set as both active and disabled.

listContent() public static method

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 <li> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::listContent('Simple list item'); `

                public static function listContent(string|Stringable $content = '', array $attributes = []): self
{
    return new self(
        DropdownItemType::CUSTOM_CONTENT,
        $content,
        '',
        false,
        false,
        $attributes,
        [],
        'h6',
    );
}

            
text() public static method

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 <li> tag.

$itemAttributes array

The HTML attributes for the <span> tag.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::text('Simple text item'); `

                public static function text(
    string|Stringable $content = '',
    array $attributes = [],
    array $itemAttributes = [],
): self {
    return new self(
        DropdownItemType::TEXT,
        $content,
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
    );
}

            
type() public method

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

            
url() public method

Sets the URL.

public self url ( string $url )
$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;
}