summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Widget/Dropdown.php
blob: b6eb20d1b8ca4adaaed236cd052c310f6370d55d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace ipl\Web\Widget;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Web\Url;

/**
 * Toggleable overlay dropdown element for displaying a list of links
 */
class Dropdown extends BaseHtmlElement
{
    /** @var array */
    protected $links = [];

    protected $defaultAttributes = ['class' => 'dropdown'];

    protected $tag = 'div';

    /**
     * Create a dropdown element
     *
     * @param mixed            $content
     * @param Attributes|array $attributes
     */
    public function __construct($content, $attributes = null)
    {
        $toggle = new ActionLink($content, '#', null, [
            'aria-expanded' => false,
            'aria-haspopup' => true,
            'class'         => 'dropdown-toggle',
            'role'          => 'button'
        ]);

        $this
            ->setContent($toggle)
            ->getAttributes()
                ->add($attributes);
    }

    /**
     * Add a link to the dropdown
     *
     * @param mixed      $content
     * @param Url|string $url
     * @param string     $icon
     *
     * @return $this
     */
    public function addLink($content, $url, $icon = null)
    {
        $this->links[] = new ActionLink($content, $url, $icon, ['class' => 'dropdown-item']);

        return $this;
    }

    protected function assemble()
    {
        $this->add(Html::tag('div', ['class' => 'dropdown-menu'], $this->links));
    }
}