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:
Mihail Geshoski 2022-09-12 10:47:37 +08:00
parent b077af7e89
commit 3cf4dd206f
2 changed files with 50 additions and 23 deletions

View File

@ -39,7 +39,7 @@ class select_menu implements \renderable, \templatable {
protected $label; protected $label;
/** @var array Button label's attributes */ /** @var array Button label's attributes */
protected $labelattributes; protected $labelattributes = [];
/** @var string Name of the combobox element */ /** @var string Name of the combobox element */
protected $name; protected $name;
@ -118,6 +118,33 @@ class select_menu implements \renderable, \templatable {
return $flattened; 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. * Export for template.
* *
@ -128,8 +155,8 @@ class select_menu implements \renderable, \templatable {
$data = new \stdClass(); $data = new \stdClass();
$data->baseid = \html_writer::random_id('select-menu'); $data->baseid = \html_writer::random_id('select-menu');
$data->label = $this->label; $data->label = $this->label;
$data->options = $this->flatten_options($this->options); $data->options = $this->flatten_options();
$data->selectedoption = array_column($data->options, 'name', 'value')[$this->selected]; $data->selectedoption = $this->get_selected_option();
$data->name = $this->name; $data->name = $this->name;
$data->value = $this->selected; $data->value = $this->selected;

View File

@ -92,7 +92,7 @@
class="btn dropdown-toggle" class="btn dropdown-toggle"
role="combobox" role="combobox"
data-toggle="dropdown" data-toggle="dropdown"
{{#label}}aria-labelledby="{{baseid}}-label{{/label}}" {{#label}}aria-labelledby="{{baseid}}-label"{{/label}}
aria-haspopup="listbox" aria-haspopup="listbox"
aria-expanded="false" aria-expanded="false"
aria-controls="{{baseid}}-listbox" aria-controls="{{baseid}}-listbox"
@ -100,26 +100,26 @@
> >
{{selectedoption}} {{selectedoption}}
</div> </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}} {{#options}}
{{#isgroup}} {{#isgroup}}
<li role="none"> <li role="none">
<ul role="group" aria-labelledby="{{id}}"> <ul role="group" aria-labelledby="{{id}}">
<li role="presentation" id="{{id}}">{{name}}</li> <li role="presentation" id="{{id}}">{{name}}</li>
{{#options}} {{#options}}
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}> <li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
{{name}} {{name}}
</li> </li>
{{/options}} {{/options}}
</ul> </ul>
</li> </li>
{{/isgroup}} {{/isgroup}}
{{^isgroup}} {{^isgroup}}
<li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}> <li class="dropdown-item" role="option" id="{{id}}" data-value="{{value}}" {{#selected}}aria-selected="true"{{/selected}}>
{{name}} {{name}}
</li> </li>
{{/isgroup}} {{/isgroup}}
{{/options}} {{/options}}
</ul> </ul>
<input type="hidden" name="{{name}}" value="{{value}}" /> <input type="hidden" name="{{name}}" value="{{value}}" />
</div> </div>