Browse Source

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.
mark_story 12 years ago
parent
commit
ca9fddf9fd
2 changed files with 30 additions and 15 deletions
  1. 13 7
      src/View/Input/Radio.php
  2. 17 8
      tests/TestCase/View/Input/RadioTest.php

+ 13 - 7
src/View/Input/Radio.php

@@ -33,6 +33,11 @@ class Radio {
 	protected $_templates;
 	protected $_templates;
 
 
 /**
 /**
+ * @var Cake\View\Input\Label
+ */
+	protected $_label;
+
+/**
  * Constructor
  * Constructor
  *
  *
  * This class uses a few templates:
  * This class uses a few templates:
@@ -47,8 +52,9 @@ class Radio {
  *
  *
  * @param Cake\View\StringTemplate $templates
  * @param Cake\View\StringTemplate $templates
  */
  */
-	public function __construct($templates) {
+	public function __construct($templates, $label) {
 		$this->_templates = $templates;
 		$this->_templates = $templates;
+		$this->_label = $label;
 	}
 	}
 
 
 /**
 /**
@@ -180,13 +186,13 @@ class Radio {
 			return false;
 			return false;
 		}
 		}
 		$labelAttrs = is_array($label) ? $label : [];
 		$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,
 			'input' => $input,
-			'attrs' => $this->_templates->formatAttributes($labelAttrs),
-		]);
+		];
+		return $this->_label->render($labelAttrs);
 	}
 	}
 
 
 }
 }

+ 17 - 8
tests/TestCase/View/Input/RadioTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\View\Input;
 
 
 use Cake\Collection\Collection;
 use Cake\Collection\Collection;
 use Cake\TestSuite\TestCase;
 use Cake\TestSuite\TestCase;
+use Cake\View\Input\Label;
 use Cake\View\Input\Radio;
 use Cake\View\Input\Radio;
 use Cake\View\StringTemplate;
 use Cake\View\StringTemplate;
 
 
@@ -45,7 +46,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderSimple() {
 	public function testRenderSimple() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Crayons[color]',
 			'name' => 'Crayons[color]',
 			'options' => ['r' => 'Red', 'b' => 'Black']
 			'options' => ['r' => 'Red', 'b' => 'Black']
@@ -87,7 +89,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderComplex() {
 	public function testRenderComplex() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Crayons[color]',
 			'name' => 'Crayons[color]',
 			'options' => [
 			'options' => [
@@ -126,7 +129,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderEmptyOption() {
 	public function testRenderEmptyOption() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Crayons[color]',
 			'name' => 'Crayons[color]',
 			'options' => ['r' => 'Red'],
 			'options' => ['r' => 'Red'],
@@ -190,7 +194,8 @@ class RadioTest extends TestCase {
 			'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
 			'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
 			'radioContainer' => '{{label}}',
 			'radioContainer' => '{{label}}',
 		]);
 		]);
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Crayons[color]',
 			'name' => 'Crayons[color]',
 			'options' => ['r' => 'Red'],
 			'options' => ['r' => 'Red'],
@@ -216,7 +221,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderSelected() {
 	public function testRenderSelected() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Versions[ver]',
 			'name' => 'Versions[ver]',
 			'val' => '1',
 			'val' => '1',
@@ -266,7 +272,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderDisabled() {
 	public function testRenderDisabled() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Versions[ver]',
 			'name' => 'Versions[ver]',
 			'options' => [
 			'options' => [
@@ -333,7 +340,8 @@ class RadioTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testRenderLabelOptions() {
 	public function testRenderLabelOptions() {
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Versions[ver]',
 			'name' => 'Versions[ver]',
 			'options' => [
 			'options' => [
@@ -405,7 +413,8 @@ class RadioTest extends TestCase {
 		$this->templates->add([
 		$this->templates->add([
 			'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
 			'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
 		]);
 		]);
-		$radio = new Radio($this->templates);
+		$label = new Label($this->templates);
+		$radio = new Radio($this->templates, $label);
 		$data = [
 		$data = [
 			'name' => 'Versions[ver]',
 			'name' => 'Versions[ver]',
 			'options' => [
 			'options' => [