';
/**
* @var string
*/
protected $template = '%s';
/**
* @var array
*/
protected $button = [
'text' => null,
'class' => 'btn btn-sm btn-white waves-effect',
'style' => null,
];
/**
* @var string
*/
protected $buttonId;
/**
* @var \Closure
*/
protected $builder;
/**
* @var bool
*/
protected $divider;
/**
* @var bool
*/
protected $click = false;
/**
* @var array
*/
protected $firstOptions = [];
public function __construct(array $options = [])
{
$this->options($options);
}
/**
* Set the options of dropdown menus.
*
* @param array $options
* @param string|null $title
*
* @return $this
*/
public function options($options = [], string $title = null)
{
if (! $options) {
return $this;
}
if ($options instanceof Arrayable) {
$options = $options->toArray();
}
$options = (array) $options;
if (! $this->options) {
$this->firstOptions = &$options;
}
$this->options[] = [$title, &$options];
return $this;
}
/**
* Set the button text.
*
* @param string|null $text
*
* @return $this
*/
public function button(?string $text)
{
$this->button['text'] = $text;
return $this;
}
/**
* Without text of button.
*
* @return $this
*/
public function withoutTextButton()
{
return $this->button('');
}
/**
* Set the button class.
*
* @param string $class
*
* @return $this
*/
public function buttonClass(string $class)
{
$this->button['class'] = $class;
return $this;
}
/**
* Set the button style.
*
* @param string $class
*
* @return $this
*/
public function buttonStyle(string $style)
{
$this->button['style'] = $style;
return $this;
}
/**
* Show divider.
*
* @param string $class
*
* @return $this
*/
public function divider()
{
$this->divider = true;
return $this;
}
/**
* Applies the callback to the elements of the options.
*
* @param string $class
*
* @return $this
*/
public function map(\Closure $builder)
{
$this->builder = $builder;
return $this;
}
/**
* Add click event listener.
*
* @param string|null $defaultLabel
*
* @return $this
*/
public function click(?string $defaultLabel = null)
{
$this->click = true;
$this->buttonId = 'dropd_'.Str::random(8);
if ($defaultLabel !== null) {
$this->button($defaultLabel);
}
return $this;
}
/**
* Set the template of dropdown menu.
*
* @param string|\Closure|Renderable $template
*
* @return $this
*/
public function template($template)
{
$this->template = $this->toString($template);
return $this;
}
/**
* @return string
*/
protected function renderButton()
{
if (is_null($this->button['text']) && ! $this->click) {
return;
}
$text = $this->button['text'];
$class = $this->button['class'];
$style = $this->button['style'];
if ($this->click && ! $text) {
if (Arr::isAssoc($this->firstOptions)) {
$text = array_keys($this->firstOptions)[0];
} else {
$text = $this->firstOptions[0] ?? '';
}
if (is_array($text)) {
$text = $text['label'] ?? current($text);
}
}
return str_replace(
['{id}', '{class}', '{style}', '{text}'],
[
$this->buttonId,
$class,
$style ? "style='$style'" : '',
$text ? " $text " : '',
],
<<<'HTML'