Commit 8739f218 authored by Shamim Rezaie's avatar Shamim Rezaie
Browse files

MDL-69116 qtype_multichoice: accessibility improvement

- Screen-readers should not see 'clear my choice' when it is not visible
- 'clear my choice' option should only become visible when a choice is
selected
parent b0af8469
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -55,7 +55,10 @@ define(['jquery', 'core/custom_interaction_events'], function($, CustomEvents) {
* @param {Object} clearChoiceContainer The clear choice option container.
*/
var hideClearChoiceOption = function(clearChoiceContainer) {
// We are using .sr-only and aria-hidden together so while the element is hidden
// from both the monitor and the screen-reader, it is still tabbable.
clearChoiceContainer.addClass('sr-only');
clearChoiceContainer.attr('aria-hidden', true);
clearChoiceContainer.find(SELECTORS.LINK).attr('tabindex', -1);
};
......@@ -66,6 +69,7 @@ define(['jquery', 'core/custom_interaction_events'], function($, CustomEvents) {
*/
var showClearChoiceOption = function(clearChoiceContainer) {
clearChoiceContainer.removeClass('sr-only');
clearChoiceContainer.removeAttr('aria-hidden');
clearChoiceContainer.find(SELECTORS.LINK).attr('tabindex', 0);
clearChoiceContainer.find(SELECTORS.RADIO).prop('disabled', true);
};
......@@ -89,7 +93,7 @@ define(['jquery', 'core/custom_interaction_events'], function($, CustomEvents) {
data.originalEvent.preventDefault();
});
root.on(CustomEvents.events.activate, SELECTORS.CHOICE_ELEMENT, function() {
root.on('change', SELECTORS.CHOICE_ELEMENT, function() {
// If the event has been triggered by any other choice, show the clear choice option.
showClearChoiceOption(clearChoiceContainer);
});
......
......@@ -293,14 +293,21 @@ class qtype_multichoice_single_renderer extends qtype_multichoice_renderer_base
'name' => $qa->get_qt_field_name('answer'),
'id' => $clearchoiceid,
'value' => -1,
'class' => 'sr-only'
'class' => 'sr-only',
'aria-hidden' => 'true'
];
$clearchoicewrapperattrs = [
'id' => $clearchoicefieldname,
'class' => 'qtype_multichoice_clearchoice',
];
$cssclass = 'qtype_multichoice_clearchoice';
// When no choice selected during rendering, then hide the clear choice option.
// We are using .sr-only and aria-hidden together so while the element is hidden
// from both the monitor and the screen-reader, it is still tabbable.
$linktabindex = 0;
if (!$hascheckedchoice && $response == -1) {
$cssclass .= ' sr-only';
$clearchoicewrapperattrs['class'] .= ' sr-only';
$clearchoicewrapperattrs['aria-hidden'] = 'true';
$clearchoiceradioattrs['checked'] = 'checked';
$linktabindex = -1;
}
......@@ -311,7 +318,7 @@ class qtype_multichoice_single_renderer extends qtype_multichoice_renderer_base
'class' => 'btn btn-link ml-4 pl-1 mt-2']);
// Now wrap the radio and label inside a div.
$result = html_writer::tag('div', $clearchoiceradio, ['id' => $clearchoicefieldname, 'class' => $cssclass]);
$result = html_writer::tag('div', $clearchoiceradio, $clearchoicewrapperattrs);
// Load required clearchoice AMD module.
$this->page->requires->js_call_amd('qtype_multichoice/clearchoice', 'init',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment