Skip to content

Commit

Permalink
Add optgroup rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 8, 2014
1 parent b0e4d7c commit ff25495
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 18 deletions.
43 changes: 28 additions & 15 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -56,7 +56,7 @@ public function render($data) {
if (empty($data['name'])) {
throw new \RuntimeException('Cannot make inputs with empty name attributes.');
}
$options = $this->_renderOptions($data);
$options = $this->_renderContent($data);
$name = $data['name'];
unset($data['name'], $data['options'], $data['empty'], $data['value']);
if (isset($data['disabled']) && is_array($data['disabled'])) {
Expand All @@ -71,7 +71,7 @@ public function render($data) {
]);
}

protected function _renderOptions($data) {
protected function _renderContent($data) {
$out = [];
if (!isset($data['options'])) {
$data['options'] = [];
Expand All @@ -92,22 +92,33 @@ protected function _renderOptions($data) {
if (isset($data['disabled']) && is_array($data['disabled'])) {
$disabled = $data['disabled'];
}
return $this->_renderOptions($options, $disabled, $selected);
}

protected function _renderOptions($options, $disabled, $selected) {
foreach ($options as $key => $val) {
$template = 'option';
$isSelected = $this->_isSelected($key, $selected);
$isDisabled = $this->_isDisabled($key, $disabled);
if ($isSelected) {
$template .= 'Selected';
}
if ($isDisabled) {
$template .= 'Disabled';
if (is_array($val)) {
$groupOptions = $this->_renderOptions($val, $disabled, $selected);
$out[] = $this->_templates->format('optgroup', [
'label' => $key,
'content' => implode('', $groupOptions)
]);
} else {
$template = 'option';
$isSelected = $this->_isSelected($key, $selected);
$isDisabled = $this->_isDisabled($key, $disabled);
if ($isSelected) {
$template .= 'Selected';
}
if ($isDisabled) {
$template .= 'Disabled';
}

$out[] = $this->_templates->format($template, [
'name' => $key,
'value' => $val
]);
}

$out[] = $this->_templates->format($template, [
'name' => $key,
'value' => $val
]);
}
return $out;
}
Expand Down Expand Up @@ -157,6 +168,8 @@ protected function _parseAttributes($options, $exclude = null) {
* Formats an individual attribute, and returns the string value of the composed attribute.
* Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
*
* TODO MOVE TO StringTemplate class?
*
* @param string $key The name of the attribute to create
* @param string $value The value of the attribute to create.
* @param boolean $escape Define if the value must be escaped
Expand Down
75 changes: 72 additions & 3 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -189,16 +189,85 @@ public function testRenderMultipleSelected() {
* @return void
*/
public function testRenderOptionGroups() {
$this->markTestIncomplete('Not done');
$select = new SelectBox($this->templates);
$data = [
'name' => 'Birds[name]',
'options' => [
'Mammal' => [
'beaver' => 'Beaver',
'elk' => 'Elk',
],
'Bird' => [
'budgie' => 'Budgie',
'eagle' => 'Eagle',
]
]
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
],
['optgroup' => ['label' => 'Mammal']],
['option' => ['value' => 'beaver']],
'Beaver',
'/option',
['option' => ['value' => 'elk']],
'Elk',
'/option',
'/optgroup',
['optgroup' => ['label' => 'Bird']],
['option' => ['value' => 'budgie']],
'Budgie',
'/option',
['option' => ['value' => 'eagle']],
'Eagle',
'/option',
'/optgroup',
'/select'
];
$this->assertTags($result, $expected);
}

/**
* test rendering option groups and selected values
*
* @return void
*/
public function testRenderOptionGroupsSelected() {
$this->markTestIncomplete('Not done');
public function testRenderOptionGroupsSelectedAndDisabled() {
$select = new SelectBox($this->templates);
$data = [
'name' => 'Birds[name]',
'value' => ['1', '2', 'burp'],
'disabled' => ['1x', '2x', 'nope'],
'options' => [
'ones' => [
1 => 'one',
'1x' => 'one x',
],
'twos' => [
'2' => 'two',
'2x' => 'two x',
]
]
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
],

['optgroup' => ['label' => 'ones']],
['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
'/optgroup',
['optgroup' => ['label' => 'twos']],
['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
'/optgroup',
'/select'
];
$this->assertTags($result, $expected);
}

/**
Expand Down

0 comments on commit ff25495

Please sign in to comment.