Skip to content

Commit

Permalink
Refactor radio widget to use the label widget.
Browse files Browse the repository at this point in the history
The registry class will be able to co-ordinate dependencies between
widgets. Reusing widgets will allow developers more control on how HTML
is generated.
  • Loading branch information
markstory committed Jan 16, 2014
1 parent b3d943e commit ca9fddf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
20 changes: 13 additions & 7 deletions src/View/Input/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Radio {
*/
protected $_templates;

/**
* @var Cake\View\Input\Label
*/
protected $_label;

/**
* Constructor
*
Expand All @@ -47,8 +52,9 @@ class Radio {
*
* @param Cake\View\StringTemplate $templates
*/
public function __construct($templates) {
public function __construct($templates, $label) {
$this->_templates = $templates;
$this->_label = $label;
}

/**
Expand Down Expand Up @@ -180,13 +186,13 @@ protected function _renderLabel($radio, $label, $input, $escape) {
return false;
}
$labelAttrs = is_array($label) ? $label : [];
$labelAttrs += ['for' => $radio['id'], 'escape' => $escape];

return $this->_templates->format('label', [
'text' => $escape ? h($radio['text']) : $radio['text'],
$labelAttrs += [
'for' => $radio['id'],
'escape' => $escape,
'text' => $radio['text'],
'input' => $input,
'attrs' => $this->_templates->formatAttributes($labelAttrs),
]);
];
return $this->_label->render($labelAttrs);
}

}
25 changes: 17 additions & 8 deletions tests/TestCase/View/Input/RadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Collection\Collection;
use Cake\TestSuite\TestCase;
use Cake\View\Input\Label;
use Cake\View\Input\Radio;
use Cake\View\StringTemplate;

Expand Down Expand Up @@ -45,7 +46,8 @@ public function setUp() {
* @return void
*/
public function testRenderSimple() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Crayons[color]',
'options' => ['r' => 'Red', 'b' => 'Black']
Expand Down Expand Up @@ -87,7 +89,8 @@ public function testRenderSimple() {
* @return void
*/
public function testRenderComplex() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Crayons[color]',
'options' => [
Expand Down Expand Up @@ -126,7 +129,8 @@ public function testRenderComplex() {
* @return void
*/
public function testRenderEmptyOption() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Crayons[color]',
'options' => ['r' => 'Red'],
Expand Down Expand Up @@ -190,7 +194,8 @@ public function testRenderInputInsideLabel() {
'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
'radioContainer' => '{{label}}',
]);
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Crayons[color]',
'options' => ['r' => 'Red'],
Expand All @@ -216,7 +221,8 @@ public function testRenderInputInsideLabel() {
* @return void
*/
public function testRenderSelected() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Versions[ver]',
'val' => '1',
Expand Down Expand Up @@ -266,7 +272,8 @@ public function testRenderSelected() {
* @return void
*/
public function testRenderDisabled() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Versions[ver]',
'options' => [
Expand Down Expand Up @@ -333,7 +340,8 @@ public function testRenderDisabled() {
* @return void
*/
public function testRenderLabelOptions() {
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Versions[ver]',
'options' => [
Expand Down Expand Up @@ -405,7 +413,8 @@ public function testRenderContainerTemplate() {
$this->templates->add([
'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
]);
$radio = new Radio($this->templates);
$label = new Label($this->templates);
$radio = new Radio($this->templates, $label);
$data = [
'name' => 'Versions[ver]',
'options' => [
Expand Down

0 comments on commit ca9fddf

Please sign in to comment.