1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Html\Tag\Base\Tag; |
11
|
|
|
|
12
|
|
|
abstract class AbstractCloseButtonWidget extends AbstractToggleWidget |
13
|
|
|
{ |
14
|
|
|
protected ?array $closeButtonOptions = []; |
15
|
|
|
protected string|Stringable $closeButtonLabel = ''; |
16
|
|
|
protected bool $encodeCloseButton = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The HTML attributes for the widget close button tag. The following special options are recognized. |
20
|
|
|
* |
21
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
22
|
|
|
*/ |
23
|
5 |
|
public function withCloseButtonOptions(?array $options): static |
24
|
|
|
{ |
25
|
5 |
|
$new = clone $this; |
26
|
5 |
|
$new->closeButtonOptions = $options; |
27
|
|
|
|
28
|
5 |
|
return $new; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Disable close button. |
33
|
|
|
*/ |
34
|
1 |
|
public function withoutCloseButton(): static |
35
|
|
|
{ |
36
|
1 |
|
return $this->withCloseButtonOptions(null); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
public function withCloseButtonLabel(string|Stringable $label): static |
40
|
|
|
{ |
41
|
3 |
|
$new = clone $this; |
42
|
3 |
|
$new->closeButtonLabel = $label; |
43
|
|
|
|
44
|
3 |
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function withEncodeCloseButton(bool $encode): static |
48
|
|
|
{ |
49
|
1 |
|
$new = clone $this; |
50
|
1 |
|
$new->encodeCloseButton = $encode; |
51
|
|
|
|
52
|
1 |
|
return $new; |
53
|
|
|
} |
54
|
|
|
|
55
|
45 |
|
public function renderCloseButton(): ?Tag |
56
|
|
|
{ |
57
|
45 |
|
$options = $this->closeButtonOptions; |
58
|
|
|
|
59
|
45 |
|
if ($options === null) { |
60
|
1 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
44 |
|
$tagName = ArrayHelper::remove($options, 'tag', 'button'); |
64
|
|
|
|
65
|
44 |
|
Html::addCssClass($options, ['widget' => 'btn-close']); |
66
|
|
|
|
67
|
44 |
|
$label = (string) $this->closeButtonLabel; |
68
|
44 |
|
$options['data-bs-dismiss'] = $this->toggleComponent(); |
69
|
|
|
|
70
|
44 |
|
if (empty($label) && !isset($options['aria-label']) && !isset($options['aria']['label'])) { |
71
|
42 |
|
$options['aria-label'] = 'Close'; |
72
|
|
|
} |
73
|
|
|
|
74
|
44 |
|
if ($tagName !== 'button') { |
75
|
|
|
$options['role'] = 'button'; |
76
|
44 |
|
} elseif (!isset($options['type'])) { |
77
|
44 |
|
$options['type'] = 'button'; |
78
|
|
|
} |
79
|
|
|
|
80
|
44 |
|
return Html::tag($tagName, $label, $options) |
81
|
44 |
|
->encode($this->encodeCloseButton); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|