Skip to content
Permalink
Browse files

Use 'val' instead of 'selected'.

Selected does not map well onto input types like text or hidden. Using
val lets us not shadow the value attribute, and also mirrors jQuery so
the concept shouldn't be foreign to developers.
  • Loading branch information...
markstory committed Jan 13, 2014
1 parent a2a4c8c commit 88a3ccad3a952188570e8c61902a5ecc1a279e3e
@@ -42,7 +42,8 @@ public function __construct($templates) {
*
* - `name` - The name of the input.
* - `value` - The value attribute. Defaults to '1'.
* - `checked` - Whether or not the checkbox should be checked.
* - `val` - The current value. If it matches `value` the checkbox will be checked.
* You can also use the 'checked' attribute to make the checkbox checked.
* - `disabled` - Whether or not the checkbox should be disabled.
*
* Any other attributes passed in will be treated as HTML attributes.
@@ -54,14 +55,14 @@ public function render($data) {
$data += [
'name' => '',
'value' => 1,
'selected' => null,
'val' => null,
'checked' => false,
'disabled' => false,
];
if ($this->_isChecked($data)) {
$data['checked'] = true;
}
unset($data['selected']);
unset($data['val']);
$attrs = $this->_templates->formatAttributes(
$data,
@@ -85,7 +86,7 @@ protected function _isChecked($data) {
if (!empty($data['checked'])) {
return true;
}
if ((string)$data['selected'] === (string)$data['value']) {
if ((string)$data['val'] === (string)$data['value']) {
return true;
}
return false;
@@ -61,7 +61,7 @@ public function __construct($templates) {
* - `options` - An array of options. See below for more information.
* - `disabled` - Either true or an array of inputs to disable.
* When true, the select element will be disabled.
* - `selected` - A string of the option to mark as selected.
* - `val` - A string of the option to mark as selected.
* - `label` - Either false to disable label generation, or
* an array of attributes for all labels.
*
@@ -73,7 +73,7 @@ public function render($data) {
'name' => '',
'options' => [],
'disabled' => null,
'selected' => null,
'val' => null,
'escape' => true,
'label' => true,
'empty' => false,
@@ -137,7 +137,7 @@ protected function _renderInput($val, $text, $data) {
$radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']);
}
if (isset($data['selected']) && strval($data['selected']) === strval($radio['value'])) {
if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) {
$radio['checked'] = true;
}
@@ -51,7 +51,7 @@ public function __construct($templates) {
* - `options` - An array of options.
* - `disabled` - Either true or an array of options to disable.
* When true, the select element will be disabled.
* - `selected` - Either a string or an array of options to mark as selected.
* - `val` - Either a string or an array of options to mark as selected.
* - `empty` - Set to true to add an empty option at the top of the
* option elements. Set to a string to define the display value of the
* empty option.
@@ -121,15 +121,15 @@ public function render($data) {
'escape' => true,
'options' => [],
'disabled' => null,
'selected' => null,
'val' => null,
];
if (empty($data['name'])) {
throw new \RuntimeException('Cannot make inputs with empty name attributes.');
}
$options = $this->_renderContent($data);
$name = $data['name'];
unset($data['name'], $data['options'], $data['empty'], $data['selected'], $data['escape']);
unset($data['name'], $data['options'], $data['empty'], $data['val'], $data['escape']);
if (isset($data['disabled']) && is_array($data['disabled'])) {
unset($data['disabled']);
}
@@ -168,7 +168,7 @@ protected function _renderContent($data) {
return [];
}
$selected = isset($data['selected']) ? $data['selected'] : null;
$selected = isset($data['val']) ? $data['val'] : null;
$disabled = null;
if (isset($data['disabled']) && is_array($data['disabled'])) {
$disabled = $data['disabled'];
@@ -116,19 +116,19 @@ public function testRenderChecked() {
$data = [
'name' => 'Comment[spam]',
'value' => 1,
'selected' => 1,
'val' => 1,
];
$result = $checkbox->render($data);
$this->assertTags($result, $expected);
$data['selected'] = '1';
$data['val'] = '1';
$result = $checkbox->render($data);
$this->assertTags($result, $expected);
$data = [
'name' => 'Comment[spam]',
'value' => 1,
'selected' => '1x',
'val' => '1x',
];
$result = $checkbox->render($data);
$expected = [
@@ -215,7 +215,7 @@ public function testRenderSelected() {
$radio = new Radio($this->templates);
$data = [
'name' => 'Versions[ver]',
'selected' => '1',
'val' => '1',
'options' => [
1 => 'one',
'1x' => 'one x',
@@ -141,7 +141,7 @@ public function testRenderSelected() {
$data = [
'id' => 'BirdName',
'name' => 'Birds[name]',
'selected' => '1',
'val' => '1',
'options' => [
1 => 'one',
'1x' => 'one x',
@@ -160,7 +160,7 @@ public function testRenderSelected() {
];
$this->assertTags($result, $expected);
$data['selected'] = 2;
$data['val'] = 2;
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
@@ -211,7 +211,7 @@ public function testRenderMultipleSelected() {
'multiple' => true,
'id' => 'BirdName',
'name' => 'Birds[name]',
'selected' => ['1', '2', 'burp'],
'val' => ['1', '2', 'burp'],
'options' => [
1 => 'one',
'1x' => 'one x',
@@ -373,7 +373,7 @@ public function testRenderOptionGroupsSelectedAndDisabled() {
$select = new SelectBox($this->templates);
$data = [
'name' => 'Birds[name]',
'selected' => ['1', '2', 'burp'],
'val' => ['1', '2', 'burp'],
'disabled' => ['1x', '2x', 'nope'],
'options' => [
'ones' => [
@@ -416,7 +416,7 @@ public function testRenderDisabled() {
'disabled' => true,
'name' => 'Birds[name]',
'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
'selected' => 'a',
'val' => 'a',
];
$result = $select->render($data);
$expected = [
@@ -440,7 +440,7 @@ public function testRenderDisabledMultiple() {
$select = new SelectBox($this->templates);
$data = [
'disabled' => ['a', 'c'],
'selected' => 'a',
'val' => 'a',
'name' => 'Birds[name]',
'options' => [
'a' => 'Albatross',
@@ -502,7 +502,7 @@ public function testRenderEmptyOption() {
$this->assertTags($result, $expected);
$data['empty'] = 'empty';
$data['selected'] = '';
$data['val'] = '';
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
@@ -513,7 +513,7 @@ public function testRenderEmptyOption() {
];
$this->assertTags($result, $expected);
$data['selected'] = false;
$data['val'] = false;
$result = $select->render($data);
$this->assertTags($result, $expected);
}

0 comments on commit 88a3cca

Please sign in to comment.
You can’t perform that action at this time.