Skip to content

Commit

Permalink
Merge branch 'MDL-63125' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve committed Oct 23, 2019
2 parents 9f997f9 + d25223a commit 3a8b78b
Showing 8 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/form-autocomplete.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/amd/build/form-autocomplete.min.js.map

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions lib/amd/src/form-autocomplete.js
Original file line number Diff line number Diff line change
@@ -107,11 +107,12 @@ function($, log, str, templates, notification, LoadingIcon) {
} else {
label = $(ele).html();
}
items.push({label: label, value: $(ele).attr('value')});
if (label !== '') {
items.push({label: label, value: $(ele).attr('value')});
}
}
});
var context = $.extend({items: items}, options, state);

// Render the template.
return templates.render('core/form_autocomplete_selection', context)
.then(function(html, js) {
@@ -161,19 +162,16 @@ function($, log, str, templates, notification, LoadingIcon) {
var deselectItem = function(options, state, item, originalSelect) {
var selectedItemValue = $(item).attr('data-value');

// We can only deselect items if this is a multi-select field.
if (options.multiple) {
// Look for a match, and toggle the selected property if there is a match.
originalSelect.children('option').each(function(index, ele) {
if ($(ele).attr('value') == selectedItemValue) {
$(ele).prop('selected', false);
// We remove newly created custom tags from the suggestions list when they are deselected.
if ($(ele).attr('data-iscustom')) {
$(ele).remove();
}
// Look for a match, and toggle the selected property if there is a match.
originalSelect.children('option').each(function(index, ele) {
if ($(ele).attr('value') == selectedItemValue) {
$(ele).prop('selected', false);
// We remove newly created custom tags from the suggestions list when they are deselected.
if ($(ele).attr('data-iscustom')) {
$(ele).remove();
}
});
}
}
});
// Rerender the selection list.
return updateSelectionList(options, state, originalSelect)
.then(function() {
@@ -571,6 +569,9 @@ function($, log, str, templates, notification, LoadingIcon) {
var existingValues = [];

// Now destroy all options that are not currently selected.
if (!options.multiple) {
originalSelect.children('option').remove();
}
originalSelect.children('option').each(function(optionIndex, option) {
option = $(option);
if (!option.prop('selected')) {
28 changes: 28 additions & 0 deletions lib/form/tests/behat/autocomplete.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@core @javascript @core_form
Feature: Autocomplete functionality in forms
For forms including autocomplete elements
As a user
I need to use the autocomplete form element

Background:
Given the following "users" exist:
| username | firstname | lastname |
| user1 | Jane | Jones |
| user2 | Sam | Smith |
And I log in as "admin"

Scenario: Use autocomplete element which accepts a single value
When I navigate to "Users > Privacy and policies > Data requests" in site administration
And I follow "New request"
And I open the autocomplete suggestions list
And I click on "Jane Jones" item in the autocomplete list
Then "Jane Jones" "autocomplete_selection" should exist
# Change selection
And I open the autocomplete suggestions list
And I click on "Sam Smith" item in the autocomplete list
And "Sam Smith" "autocomplete_selection" should exist
And "Jane Jones" "autocomplete_selection" should not exist
# Remove selection
And I click on "Sam Smith" "autocomplete_selection"
And "Sam Smith" "autocomplete_selection" should not exist
And I should see "No selection" in the ".form-autocomplete-selection" "css_element"
2 changes: 1 addition & 1 deletion lib/templates/form_autocomplete_selection.mustache
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
<span class="accesshide">{{#str}}selecteditems, form{{/str}}</span>
{{#items}}
<span role="listitem" data-value="{{value}}" aria-selected="true" class="badge badge-info mb-3 mr-1" style="font-size: 100%">
{{#multiple}}<span aria-hidden="true">× </span>{{/multiple}}{{{label}}}
<span aria-hidden="true">× </span>{{{label}}}
</span>
{{/items}}
{{^items}}
2 changes: 1 addition & 1 deletion theme/boost/scss/moodle/forms.scss
Original file line number Diff line number Diff line change
@@ -285,7 +285,7 @@ fieldset.coursesearchbox label {
min-height: 2 * $input-padding-y-sm + 2 * $font-size-base;
}

.form-autocomplete-multiple [role=listitem] {
.form-autocomplete-selection [role=listitem] {
cursor: pointer;
white-space: inherit;
word-break: break-word;
2 changes: 1 addition & 1 deletion theme/boost/style/moodle.css
Original file line number Diff line number Diff line change
@@ -15857,7 +15857,7 @@ fieldset.coursesearchbox label {
margin: 0.25rem;
min-height: 2.375rem; }

.form-autocomplete-multiple [role=listitem] {
.form-autocomplete-selection [role=listitem] {
cursor: pointer;
white-space: inherit;
word-break: break-word;
2 changes: 1 addition & 1 deletion theme/classic/style/moodle.css
Original file line number Diff line number Diff line change
@@ -16128,7 +16128,7 @@ fieldset.coursesearchbox label {
margin: 0.25rem;
min-height: 2.375rem; }

.form-autocomplete-multiple [role=listitem] {
.form-autocomplete-selection [role=listitem] {
cursor: pointer;
white-space: inherit;
word-break: break-word;

0 comments on commit 3a8b78b

Please sign in to comment.