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

Add tabindex to list item and they can be controlled by up/down arrow. #613

Merged
merged 1 commit into from
Nov 23, 2023
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
50 changes: 42 additions & 8 deletions src/MultipleSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class MultipleSelect {
let tabIndex = ''

if (this.tabIndex !== null) {
this.$el.attr('tabindex', -1)
tabIndex = this.tabIndex && `tabindex="${this.tabIndex}"`
}
this.$el.attr('tabindex', -1)

this.$choice = $(`
<button type="button" class="ms-choice"${tabIndex}>
Expand Down Expand Up @@ -386,9 +386,9 @@ class MultipleSelect {

if (this.options.selectAll && !this.options.single) {
rows.push(`
<li class="ms-select-all">
<li class="ms-select-all" tabindex="0">
<label>
<input type="checkbox" ${this.selectAllName}${this.allSelected ? ' checked="checked"' : ''} />
<input type="checkbox" ${this.selectAllName}${this.allSelected ? ' checked="checked"' : ''} tabindex="-1" />
<span>${this.options.formatSelectAll()}</span>
</label>
</li>
Expand Down Expand Up @@ -436,6 +436,7 @@ class MultipleSelect {
data-key="${row._key}"
${row.selected ? ' checked="checked"' : ''}
${row.disabled ? ' disabled="disabled"' : ''}
tabindex="-1"
>`

if (
Expand All @@ -446,7 +447,7 @@ class MultipleSelect {
}

html.push(`
<li class="group ${classes}" ${style}>
<li class="group ${classes}" ${style} tabindex="${classes.includes('hide-radio') || row.disabled ? -1 : 0}">
<label class="optgroup${this.options.single || row.disabled ? ' disabled' : ''}">
${group}${row.label}
</label>
Expand Down Expand Up @@ -474,14 +475,15 @@ class MultipleSelect {
}

return [`
<li class="${multiple} ${classes}" ${title} ${style}>
<li class="${multiple} ${classes}" ${title} ${style} tabindex="${row.disabled ? -1 : 0}">
<label class="${row.disabled ? 'disabled' : ''}">
<input type="${type}"
value="${row.value}"
data-key="${row._key}"
${this.selectItemName}
${row.selected ? ' checked="checked"' : ''}
${row.disabled ? ' disabled="disabled"' : ''}
tabindex="-1"
>
<span>${row.text}</span>
</label>
Expand Down Expand Up @@ -619,6 +621,38 @@ class MultipleSelect {

close()
})

this.$ul.find('li').off('keydown').on('keydown', e => {
const $this = $(e.currentTarget)
let $divider
let $li

switch (e.key) {
case 'ArrowUp':
e.preventDefault()
$divider = $this.prev('li.option-divider')
$li = $divider.length ? $divider : $this

$li.prev().trigger('focus')
break
case 'ArrowDown':
e.preventDefault()
$divider = $this.next('li.option-divider')
$li = $divider.length ? $divider : $this

$li.next().trigger('focus')
break
case 'Enter':
e.preventDefault()
$this.find('input').trigger('click')
if (this.options.single) {
this.$choice.trigger('focus')
}
break
default:
// ignore
}
})
}

initView () {
Expand Down Expand Up @@ -996,7 +1030,7 @@ class MultipleSelect {
if (row.type === 'optgroup') {
if (this.options.filterGroup) {
const visible = this.options.customFilter({
label: removeDiacritics(row.label.toLowerCase()),
label: removeDiacritics(row.label.toString().toLowerCase()),
search: removeDiacritics(search),
originalLabel: row.label,
originalSearch,
Expand All @@ -1010,7 +1044,7 @@ class MultipleSelect {
} else {
for (const child of row.children) {
child.visible = this.options.customFilter({
text: removeDiacritics(child.text.toLowerCase()),
text: removeDiacritics(child.text.toString().toLowerCase()),
search: removeDiacritics(search),
originalText: child.text,
originalSearch,
Expand All @@ -1022,7 +1056,7 @@ class MultipleSelect {
}
} else {
row.visible = this.options.customFilter({
text: removeDiacritics(row.text.toLowerCase()),
text: removeDiacritics(row.text.toString().toLowerCase()),
search: removeDiacritics(search),
originalText: row.text,
originalSearch,
Expand Down
6 changes: 3 additions & 3 deletions vue-examples/src/examples/CustomFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export default {
caseSensitive: false,
options: {
filter: true,
customFilter: (label, text, originalLabel, originalText) => {
customFilter: ({ search, text, originalSearch, originalText }) => {
if (this.caseSensitive) {
return originalLabel.indexOf(originalText) === 0
return originalText.indexOf(originalSearch) === 0
}
return label.indexOf(text) === 0
return text.indexOf(search) === 0
}
}
}
Expand Down