ソースを参照

MultiCheckboxWidget use selectedClass template

Erwane Breton 6 年 前
コミット
063c6611bc

+ 2 - 1
src/View/Widget/MultiCheckboxWidget.php

@@ -210,7 +210,8 @@ class MultiCheckboxWidget implements WidgetInterface
             ];
 
             if ($checkbox['checked']) {
-                $labelAttrs = $this->_templates->addClass($labelAttrs, 'selected');
+                $selectedClass = $this->_templates->format('selectedClass', []);
+                $labelAttrs = $this->_templates->addClass($labelAttrs, $selectedClass);
             }
 
             $label = $this->_label->render($labelAttrs, $context);

+ 43 - 0
tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php

@@ -714,4 +714,47 @@ class MultiCheckboxWidgetTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
     }
+
+    /**
+     * testRenderSelectedClass method
+     *
+     * Test that the custom selected class is passed to label
+     * Issue: https://github.com/cakephp/cakephp/issues/11249
+     *
+     * @return void
+     */
+    public function testRenderSelectedClass()
+    {
+        $this->templates->add(['selectedClass' => 'active']);
+
+        $label = new LabelWidget($this->templates);
+        $input = new MultiCheckboxWidget($this->templates, $label);
+        $data = [
+            'name' => 'field',
+            'options' => ['value1', 'value2'],
+            'id' => 'alternative-id',
+            'idPrefix' => 'willBeIgnored',
+        ];
+        $result = $input->render($data, $this->context);
+
+        $data = [
+            'name' => 'field',
+            'options' => [1 => 'value1', 2 => 'value2'],
+            'val' => 1,
+            'label' => ['title' => 'my label'],
+        ];
+        $result = $input->render($data, $this->context);
+
+        $expected = [
+            [
+                'div' => ['class' => 'checkbox'],
+                'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '1', 'checked' => 'checked', 'id' => 'field-1'],
+                'label' => ['title' =>'my label', 'for' => 'field-1', 'class' => 'active'],
+            ],
+            'value1',
+            '/label',
+        ];
+
+        $this->assertHtml($expected, $result);
+    }
 }