Skip to content

Commit ca9fddf

Browse files
committed
Refactor radio widget to use the label widget.
The registry class will be able to co-ordinate dependencies between widgets. Reusing widgets will allow developers more control on how HTML is generated.
1 parent b3d943e commit ca9fddf

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/View/Input/Radio.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Radio {
3232
*/
3333
protected $_templates;
3434

35+
/**
36+
* @var Cake\View\Input\Label
37+
*/
38+
protected $_label;
39+
3540
/**
3641
* Constructor
3742
*
@@ -47,8 +52,9 @@ class Radio {
4752
*
4853
* @param Cake\View\StringTemplate $templates
4954
*/
50-
public function __construct($templates) {
55+
public function __construct($templates, $label) {
5156
$this->_templates = $templates;
57+
$this->_label = $label;
5258
}
5359

5460
/**
@@ -180,13 +186,13 @@ protected function _renderLabel($radio, $label, $input, $escape) {
180186
return false;
181187
}
182188
$labelAttrs = is_array($label) ? $label : [];
183-
$labelAttrs += ['for' => $radio['id'], 'escape' => $escape];
184-
185-
return $this->_templates->format('label', [
186-
'text' => $escape ? h($radio['text']) : $radio['text'],
189+
$labelAttrs += [
190+
'for' => $radio['id'],
191+
'escape' => $escape,
192+
'text' => $radio['text'],
187193
'input' => $input,
188-
'attrs' => $this->_templates->formatAttributes($labelAttrs),
189-
]);
194+
];
195+
return $this->_label->render($labelAttrs);
190196
}
191197

192198
}

tests/TestCase/View/Input/RadioTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Cake\Collection\Collection;
1818
use Cake\TestSuite\TestCase;
19+
use Cake\View\Input\Label;
1920
use Cake\View\Input\Radio;
2021
use Cake\View\StringTemplate;
2122

@@ -45,7 +46,8 @@ public function setUp() {
4546
* @return void
4647
*/
4748
public function testRenderSimple() {
48-
$radio = new Radio($this->templates);
49+
$label = new Label($this->templates);
50+
$radio = new Radio($this->templates, $label);
4951
$data = [
5052
'name' => 'Crayons[color]',
5153
'options' => ['r' => 'Red', 'b' => 'Black']
@@ -87,7 +89,8 @@ public function testRenderSimple() {
8789
* @return void
8890
*/
8991
public function testRenderComplex() {
90-
$radio = new Radio($this->templates);
92+
$label = new Label($this->templates);
93+
$radio = new Radio($this->templates, $label);
9194
$data = [
9295
'name' => 'Crayons[color]',
9396
'options' => [
@@ -126,7 +129,8 @@ public function testRenderComplex() {
126129
* @return void
127130
*/
128131
public function testRenderEmptyOption() {
129-
$radio = new Radio($this->templates);
132+
$label = new Label($this->templates);
133+
$radio = new Radio($this->templates, $label);
130134
$data = [
131135
'name' => 'Crayons[color]',
132136
'options' => ['r' => 'Red'],
@@ -190,7 +194,8 @@ public function testRenderInputInsideLabel() {
190194
'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
191195
'radioContainer' => '{{label}}',
192196
]);
193-
$radio = new Radio($this->templates);
197+
$label = new Label($this->templates);
198+
$radio = new Radio($this->templates, $label);
194199
$data = [
195200
'name' => 'Crayons[color]',
196201
'options' => ['r' => 'Red'],
@@ -216,7 +221,8 @@ public function testRenderInputInsideLabel() {
216221
* @return void
217222
*/
218223
public function testRenderSelected() {
219-
$radio = new Radio($this->templates);
224+
$label = new Label($this->templates);
225+
$radio = new Radio($this->templates, $label);
220226
$data = [
221227
'name' => 'Versions[ver]',
222228
'val' => '1',
@@ -266,7 +272,8 @@ public function testRenderSelected() {
266272
* @return void
267273
*/
268274
public function testRenderDisabled() {
269-
$radio = new Radio($this->templates);
275+
$label = new Label($this->templates);
276+
$radio = new Radio($this->templates, $label);
270277
$data = [
271278
'name' => 'Versions[ver]',
272279
'options' => [
@@ -333,7 +340,8 @@ public function testRenderDisabled() {
333340
* @return void
334341
*/
335342
public function testRenderLabelOptions() {
336-
$radio = new Radio($this->templates);
343+
$label = new Label($this->templates);
344+
$radio = new Radio($this->templates, $label);
337345
$data = [
338346
'name' => 'Versions[ver]',
339347
'options' => [
@@ -405,7 +413,8 @@ public function testRenderContainerTemplate() {
405413
$this->templates->add([
406414
'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
407415
]);
408-
$radio = new Radio($this->templates);
416+
$label = new Label($this->templates);
417+
$radio = new Radio($this->templates, $label);
409418
$data = [
410419
'name' => 'Versions[ver]',
411420
'options' => [

0 commit comments

Comments
 (0)