mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-75153 output: Fix the group option and label support in select_menu
The following commit fixes the current logic that is used to return the selected option and take into account structures with group options. Also, it adds a default value to the $labelattributes class property to to fix the issues that occur then the label is not explicitely set by set_label().
This commit is contained in:
parent
b077af7e89
commit
3cf4dd206f
@ -39,7 +39,7 @@ class select_menu implements \renderable, \templatable {
|
||||
protected $label;
|
||||
|
||||
/** @var array Button label's attributes */
|
||||
protected $labelattributes;
|
||||
protected $labelattributes = [];
|
||||
|
||||
/** @var string Name of the combobox element */
|
||||
protected $name;
|
||||
@ -118,6 +118,33 @@ class select_menu implements \renderable, \templatable {
|
||||
return $flattened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the selected option.
|
||||
*
|
||||
* @return string|null The name of the selected option or null.
|
||||
*/
|
||||
private function get_selected_option(): ?string {
|
||||
foreach ($this->options as $value => $option) {
|
||||
if (is_array($option)) { // This is a group.
|
||||
foreach ($option as $groupname => $optoptions) {
|
||||
// Loop through the options within the group to check whether any of them matches the 'selected' value.
|
||||
foreach ($optoptions as $optvalue => $optoption) {
|
||||
// If the value of the option matches the 'selected' value, return the name of the option.
|
||||
if ($this->selected == $optvalue) {
|
||||
return $optoption;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else { // This is a standard option item.
|
||||
// If the value of the option matches the 'selected' value, return the name of the option.
|
||||
if ($this->selected == $value) {
|
||||
return $option;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
@ -128,8 +155,8 @@ class select_menu implements \renderable, \templatable {
|
||||
$data = new \stdClass();
|
||||
$data->baseid = \html_writer::random_id('select-menu');
|
||||
$data->label = $this->label;
|
||||
$data->options = $this->flatten_options($this->options);
|
||||
$data->selectedoption = array_column($data->options, 'name', 'value')[$this->selected];
|
||||
$data->options = $this->flatten_options();
|
||||
$data->selectedoption = $this->get_selected_option();
|
||||
$data->name = $this->name;
|
||||
$data->value = $this->selected;
|
||||
|
||||
|
@ -92,7 +92,7 @@
|
||||
class="btn dropdown-toggle"
|
||||
role="combobox"
|
||||
data-toggle="dropdown"
|
||||
{{#label}}aria-labelledby="{{baseid}}-label{{/label}}"
|
||||
{{#label}}aria-labelledby="{{baseid}}-label"{{/label}}
|
||||
aria-haspopup="listbox"
|
||||
aria-expanded="false"
|
||||
aria-controls="{{baseid}}-listbox"
|
||||
@ -100,26 +100,26 @@
|
||||
>
|
||||
{{selectedoption}}
|
||||
</div>
|
||||
<ul class="dropdown-menu" role="listbox" id="{{baseid}}-listbox" {{#label}}aria-labelledby="{{baseid}}-label{{/label}}">
|
||||
<ul class="dropdown-menu" role="listbox" id="{{baseid}}-listbox" {{#label}}aria-labelledby="{{baseid}}-label"{{/label}}>
|
||||
{{#options}}
|
||||
{{#isgroup}}
|
||||
<li role="none">
|
||||
<ul role="group" aria-labelledby="{{id}}">
|
||||
<li role="presentation" id="{{id}}">{{name}}</li>
|
||||
{{#options}}
|
||||
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
|
||||
{{name}}
|
||||
</li>
|
||||
{{/options}}
|
||||
</ul>
|
||||
</li>
|
||||
{{/isgroup}}
|
||||
{{^isgroup}}
|
||||
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
|
||||
{{name}}
|
||||
</li>
|
||||
{{/isgroup}}
|
||||
{{/options}}
|
||||
{{#isgroup}}
|
||||
<li role="none">
|
||||
<ul role="group" aria-labelledby="{{id}}">
|
||||
<li role="presentation" id="{{id}}">{{name}}</li>
|
||||
{{#options}}
|
||||
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
|
||||
{{name}}
|
||||
</li>
|
||||
{{/options}}
|
||||
</ul>
|
||||
</li>
|
||||
{{/isgroup}}
|
||||
{{^isgroup}}
|
||||
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
|
||||
{{name}}
|
||||
</li>
|
||||
{{/isgroup}}
|
||||
{{/options}}
|
||||
</ul>
|
||||
<input type="hidden" name="{{name}}" value="{{value}}" />
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user