1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Base\PartsField; |
8
|
|
|
use Yiisoft\Html\Tag\Button as ButtonTag; |
9
|
|
|
use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* `ButtonGroup` renders a button group widget. |
13
|
|
|
*/ |
14
|
|
|
final class ButtonGroup extends PartsField |
15
|
|
|
{ |
16
|
|
|
private ButtonGroupWidget $widget; |
17
|
|
|
|
18
|
1 |
|
public function __construct() |
19
|
|
|
{ |
20
|
1 |
|
$this->widget = ButtonGroupWidget::create()->withoutContainer(); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
public function buttons(ButtonTag ...$buttons): self |
24
|
|
|
{ |
25
|
1 |
|
$new = clone $this; |
26
|
1 |
|
$new->widget = $this->widget->buttons(...$buttons); |
27
|
1 |
|
return $new; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data Array of buttons. Each button is an array with label as first element and additional |
32
|
|
|
* name-value pairs as attrbiutes of button. |
33
|
|
|
* |
34
|
|
|
* Example: |
35
|
|
|
* ```php |
36
|
|
|
* [ |
37
|
|
|
* ['Reset', 'type' => 'reset', 'class' => 'default'], |
38
|
|
|
* ['Send', 'type' => 'submit', 'class' => 'primary'], |
39
|
|
|
* ] |
40
|
|
|
* ``` |
41
|
|
|
* @param bool $encode Whether button content should be HTML-encoded. |
42
|
|
|
*/ |
43
|
|
|
public function buttonsData(array $data, bool $encode = true): self |
44
|
|
|
{ |
45
|
|
|
$new = clone $this; |
46
|
|
|
$new->widget = $this->widget->buttonsData($data, $encode); |
47
|
|
|
return $new; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function buttonAttributes(array $attributes): self |
51
|
|
|
{ |
52
|
|
|
$new = clone $this; |
53
|
|
|
$new->widget = $this->widget->buttonAttributes($attributes); |
54
|
|
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function replaceButtonAttributes(array $attributes): self |
58
|
|
|
{ |
59
|
|
|
$new = clone $this; |
60
|
|
|
$new->widget = $this->widget->replaceButtonAttributes($attributes); |
61
|
|
|
return $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
66
|
|
|
*/ |
67
|
|
|
public function disabled(bool $disabled = true): self |
68
|
|
|
{ |
69
|
|
|
$new = clone $this; |
70
|
|
|
$new->widget = $this->widget->disabled($disabled); |
71
|
|
|
return $new; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function form(?string $formId): self |
75
|
|
|
{ |
76
|
|
|
$new = clone $this; |
77
|
|
|
$new->widget = $this->widget->form($formId); |
78
|
|
|
return $new; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function separator(string $separator): self |
82
|
|
|
{ |
83
|
|
|
$new = clone $this; |
84
|
|
|
$new->widget = $this->widget->separator($separator); |
85
|
|
|
return $new; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
protected function generateInput(): string |
89
|
|
|
{ |
90
|
1 |
|
$html = $this->widget->render(); |
91
|
|
|
|
92
|
1 |
|
return $html !== '' |
93
|
1 |
|
? "\n" . $html . "\n" |
94
|
1 |
|
: ''; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|