Skip to content

Commit

Permalink
Revert "CheckboxList: added containerPrototype and itemLabelPrototype [
Browse files Browse the repository at this point in the history
…Closes #92]"

This reverts commit e0bc505.
  • Loading branch information
dg committed Oct 19, 2015
1 parent f89c58b commit 5e07a7b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 93 deletions.
83 changes: 23 additions & 60 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,41 @@
* Set of checkboxes.
*
* @property-read Html $separatorPrototype
* @property-read Html $containerPrototype
* @property-read Html $itemLabelPrototype
*/
class CheckboxList extends MultiChoiceControl
{
/** @var Html separator element template */
protected $separator;

/** @var Html container element template */
protected $container;

/** @var Html item label template */
protected $itemLabel;


/**
* @param string label
* @param array options from which to choose
*/
public function __construct($label = NULL, array $items = NULL)
{
parent::__construct($label, $items);
$this->control->type = 'checkbox';
$this->container = Html::el();
$this->separator = Html::el('br');
$this->itemLabel = Html::el();
}


/**
* Generates control's HTML element.
* @return Html
* @return string
*/
public function getControl()
{
$input = parent::getControl();
$items = $this->getItems();
reset($items);

return $this->container->setHtml(
Nette\Forms\Helpers::createInputList(
$this->translate($items),
array_merge($input->attrs, array(
'id' => NULL,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'required' => NULL,
'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
)),
$this->itemLabel->attrs,
$this->separator
)
$input = parent::getControl();
return Nette\Forms\Helpers::createInputList(
$this->translate($items),
array_merge($input->attrs, array(
'id' => NULL,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'required' => NULL,
'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
)),
$this->label->attrs,
$this->separator
);
}

Expand All @@ -82,6 +65,16 @@ public function getLabel($caption = NULL)
}


/**
* Returns separator HTML element template.
* @return Html
*/
public function getSeparatorPrototype()
{
return $this->separator;
}


/**
* @return Html
*/
Expand All @@ -108,34 +101,4 @@ public function getLabelPart($key = NULL)
: $this->getLabel();
}


/**
* Returns separator HTML element template.
* @return Html
*/
public function getSeparatorPrototype()
{
return $this->separator;
}


/**
* Returns container HTML element template.
* @return Html
*/
public function getContainerPrototype()
{
return $this->container;
}


/**
* Returns item label HTML element template.
* @return Html
*/
public function getItemLabelPrototype()
{
return $this->itemLabel;
}

}
43 changes: 10 additions & 33 deletions tests/Forms/Controls.CheckboxList.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test(function () {
Assert::type('Nette\Utils\Html', $input->getLabelPart(0));
Assert::same('<label for="frm-list-0">Second</label>', (string) $input->getLabelPart(0));

Assert::type('Nette\Utils\Html', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::type('string', $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());

Assert::type('Nette\Utils\Html', $input->getControlPart(0));
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
Expand All @@ -50,7 +50,7 @@ test(function () { // checked
0 => 'Second',
))->setValue(0);

Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', $input->getControl());
});


Expand All @@ -66,7 +66,7 @@ test(function () { // translator
Assert::same('<label>ANOTHER LABEL</label>', (string) $input->getLabel('Another label'));
Assert::same('<label for="frm-list-0">SECOND</label>', (string) $input->getLabelPart(0));

Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a">FIRST</label><br><label><input type="checkbox" name="list[]" value="0">SECOND</label>', $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" value="0">', (string) $input->getControlPart(0));
});

Expand All @@ -81,7 +81,7 @@ test(function () { // Html
Assert::same('<label><b>Label</b></label>', (string) $input->getLabel());
Assert::same('<label><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));

Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" value="a"><b>First</b></label>', $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" value="a">', (string) $input->getControlPart('a'));
});

Expand All @@ -93,7 +93,7 @@ test(function () { // validation rules
0 => 'Second',
))->setRequired('required');

Assert::same('<label><input type="checkbox" name="list[]" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-0" data-nette-rules=\'[{"op":":filled","msg":"required"}]\' value="0">', (string) $input->getControlPart(0));
});

Expand All @@ -106,7 +106,7 @@ test(function () { // container
0 => 'Second',
));

Assert::same('<label><input type="checkbox" name="container[list][]" value="a">First</label><br><label><input type="checkbox" name="container[list][]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="container[list][]" value="a">First</label><br><label><input type="checkbox" name="container[list][]" value="0">Second</label>', $input->getControl());
});


Expand All @@ -117,7 +117,7 @@ test(function () { // separator prototype
));
$input->getSeparatorPrototype()->setName('div');

Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', $input->getControl());
});


Expand All @@ -128,7 +128,7 @@ test(function () { // disabled all
0 => 'Second',
))->setDisabled(TRUE);

Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" disabled value="0">Second</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" disabled value="0">Second</label>', $input->getControl());
});


Expand All @@ -139,7 +139,7 @@ test(function () { // disabled one
0 => 'Second',
))->setDisabled(array('a'));

Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', (string) $input->getControl());
Assert::same('<label><input type="checkbox" name="list[]" disabled value="a">First</label><br><label><input type="checkbox" name="list[]" value="0">Second</label>', $input->getControl());
Assert::same('<input type="checkbox" name="list[]" id="frm-list-a" disabled value="a">', (string) $input->getControlPart('a'));
});

Expand All @@ -153,26 +153,3 @@ test(function () { // numeric key as string & getControlPart

Assert::same('<input type="checkbox" name="list[]" id="frm-list-1" checked value="1">', (string) $input->getControlPart('1'));
});


test(function () { // container prototype
$form = new Form;
$input = $form->addCheckboxList('list', NULL, array(
'a' => 'b',
));
$input->getSeparatorPrototype()->setName('hr');
$input->getContainerPrototype()->setName('div');

Assert::same('<div><label><input type="checkbox" name="list[]" value="a">b</label></div>', (string) $input->getControl());
});


test(function () { // item label prototype
$form = new Form;
$input = $form->addCheckboxList('list', NULL, array(
'a' => 'b',
));
$input->getItemLabelPrototype()->class('foo');

Assert::same('<label class="foo"><input type="checkbox" name="list[]" value="a">b</label>', (string) $input->getControl());
});

0 comments on commit 5e07a7b

Please sign in to comment.