Skip to content

Commit

Permalink
Simplify auto-focus code
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Sep 29, 2021
1 parent 8df51a2 commit bb1ebcb
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions inc/html.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4333,15 +4333,6 @@ static function jsAdaptDropdown($id, $params = []) {
$templateresult = $params["templateResult"] ?? "templateResult";
$templateselection = $params["templateSelection"] ?? "templateSelection";

$search_autofocus_event = <<<JS
$(select2_el).on('select2:open', (e) => {
const select2_selector = `.select2-search__field[aria-controls='select2-\${e.target.id}-results']`;
$(select2_selector).each((k, v) => {
v.focus();
});
});
JS;

$js = "$(function() {
const select2_el = $('#$id').select2({
$placeholder
Expand Down Expand Up @@ -4435,7 +4426,7 @@ static function jsAdaptDropdown($id, $params = []) {
$('#$id').val(value).trigger('change');
})
$('label[for=$id]').on('click', function(){ $('#$id').select2('open'); });
$search_autofocus_event
$(`.select2-search__field[aria-controls='select2-\${e.target.id}-results']`).focus();
});";
return Html::scriptBlock($js);
}
Expand Down Expand Up @@ -4520,15 +4511,6 @@ static function jsAjaxDropdown($name, $field_id, $url, $params = []) {
$options[$tag] = $val;
}

$search_autofocus_event = <<<JS
$(select2_el).on('select2:open', (e) => {
const select2_selector = `.select2-search__field[aria-controls='select2-\${e.target.id}-results']`;
$(select2_selector).each((k, v) => {
v.focus();
});
});
JS;

$output = '';

$js = "
Expand Down Expand Up @@ -4619,7 +4601,7 @@ static function jsAjaxDropdown($name, $field_id, $url, $params = []) {
}

$js .= " $('label[for=$field_id]').on('click', function(){ $('#$field_id').select2('open'); });";
$js .= $search_autofocus_event;
$js .= " $(`.select2-search__field[aria-controls='select2-\${e.target.id}-results']`).focus();";

$output .= Html::scriptBlock('$(function() {' . $js . '});');

Expand Down

0 comments on commit bb1ebcb

Please sign in to comment.