Skip to content

Commit c7c2d0d

Browse files
committed
CheckboxList: added item label prototype
1 parent 0bff7ed commit c7c2d0d

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Forms/Controls/CheckboxList.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
* @property-read Html $separatorPrototype
1818
* @property-read Html $containerPrototype
19+
* @property-read Html $itemLabelPrototype
1920
*/
2021
class CheckboxList extends MultiChoiceControl
2122
{
@@ -25,6 +26,9 @@ class CheckboxList extends MultiChoiceControl
2526
/** @var Html container element template */
2627
protected $container;
2728

29+
/** @var Html item label template */
30+
protected $itemLabel;
31+
2832

2933
/**
3034
* @param string label
@@ -36,6 +40,7 @@ public function __construct($label = NULL, array $items = NULL)
3640
$this->control->type = 'checkbox';
3741
$this->container = Html::el();
3842
$this->separator = Html::el('br');
43+
// $this->itemLabel = Html::el('label'); back compatiblity
3944
$this->setOption('type', 'checkbox');
4045
}
4146

@@ -60,7 +65,7 @@ public function getControl()
6065
'required' => NULL,
6166
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
6267
]),
63-
$this->label->attrs,
68+
$this->itemLabel ? $this->itemLabel->attrs : $this->label->attrs,
6469
$this->separator
6570
)
6671
);
@@ -99,8 +104,9 @@ public function getControlPart($key = NULL)
99104
*/
100105
public function getLabelPart($key = NULL)
101106
{
107+
$itemLabel = $this->itemLabel ? clone $this->itemLabel : clone $this->label;
102108
return func_num_args()
103-
? parent::getLabel($this->items[$key])->for($this->getHtmlId() . '-' . $key)
109+
? $itemLabel->setText($this->translate($this->items[$key]))->for($this->getHtmlId() . '-' . $key)
104110
: $this->getLabel();
105111
}
106112

@@ -124,4 +130,14 @@ public function getContainerPrototype()
124130
return $this->container;
125131
}
126132

133+
134+
/**
135+
* Returns item label HTML element template.
136+
* @return Html
137+
*/
138+
public function getItemLabelPrototype()
139+
{
140+
return $this->itemLabel ?: $this->itemLabel = Html::el('label');
141+
}
142+
127143
}

tests/Forms/Controls.CheckboxList.render.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,29 @@ test(function () { // rendering options
177177
$input->getControl();
178178
Assert::true($input->getOption('rendered'));
179179
});
180+
181+
182+
test(function () { // item label prototype
183+
$form = new Form;
184+
$input = $form->addCheckboxList('list', NULL, [
185+
'a' => 'b',
186+
]);
187+
$input->getItemLabelPrototype()->class('foo');
188+
189+
Assert::same('<label></label>', (string) $input->getLabel());
190+
Assert::same('<label class="foo" for="frm-list-a">b</label>', (string) $input->getLabelPart('a'));
191+
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
192+
});
193+
194+
195+
test(function () { // item label prototype (back compatiblity)
196+
$form = new Form;
197+
$input = $form->addCheckboxList('list', NULL, [
198+
'a' => 'b',
199+
]);
200+
$input->getLabelPrototype()->class('foo');
201+
202+
Assert::same('<label class="foo"></label>', (string) $input->getLabel());
203+
Assert::same('<label class="foo" for="frm-list-a">b</label>', (string) $input->getLabelPart('a'));
204+
Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
205+
});

0 commit comments

Comments
 (0)