Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable autocomplete to accept dropdown options #60

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jade/page-contents/autocomplete_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ <h3 class="header">Options</h3>
<td></td>
<td>Sort function that defines the order of the list of autocomplete options.</td>
</tr>
<tr>
<td>dropdownOptions</td>
<td>Object</td>
<td>{}</td>
<td>Pass options object to select dropdown initialization.</td>
</tr>
</tbody>
</table>

Expand Down
30 changes: 23 additions & 7 deletions js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
data: {}, // Autocomplete data set
limit: Infinity, // Limit of results the autocomplete shows
onAutocomplete: null, // Callback for when autocompleted
dropdownOptions: {
// Default dropdown options
autoFocus: false,
closeOnClick: false,
coverTrigger: false
},
minLength: 1, // Min characters before autocomplete starts
sortFunction: function(a, b, inputString) {
// Sort function for sorting autocomplete results
Expand Down Expand Up @@ -152,14 +158,24 @@
this.$inputField.append(this.container);
this.el.setAttribute('data-target', this.container.id);

this.dropdown = M.Dropdown.init(this.el, {
autoFocus: false,
closeOnClick: false,
coverTrigger: false,
onItemClick: (itemEl) => {
this.selectOption($(itemEl));
// Initialize dropdown
let dropdownOptions = $.extend(
Autocomplete.defaults.dropdownOptions,
this.options.dropdownOptions
);
let userOnItemClick = dropdownOptions.onItemClick;

// Ensuring the selectOption call when user passes custom onItemClick function to dropdown
dropdownOptions.onItemClick = (el) => {
this.selectOption($(el));

// Handle user declared onItemClick if needed
if (userOnItemClick && typeof userOnItemClick === 'function') {
userOnItemClick.call(this.dropdown, this.el);
}
});
};

this.dropdown = M.Dropdown.init(this.el, dropdownOptions);

// Sketchy removal of dropdown click handler
this.el.removeEventListener('click', this.dropdown._handleClickBound);
Expand Down