Skip to content

Commit

Permalink
fix: rich select keyboard navigation not working (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries authored Oct 13, 2021
1 parent 8727948 commit 77f6429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
30 changes: 18 additions & 12 deletions resources/assets/js/rich-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ const RichSelect = (
$dispatch(dispatchEvent, $event.target.value);
}
},
init() {
this.$nextTick(() => {
if (grouped) {
this.optionsCount = Object.keys(this.options)
.map((groupName) => Object.keys(this.options[groupName]))
.flat().length;
} else {
this.optionsCount = Object.keys(this.options).length;
}
});
getOptionsCount() {
if (this.optionsCount !== null) {
return this.optionsCount;
}

if (grouped) {
this.optionsCount = Object.keys(this.options)
.map((groupName) => Object.keys(this.options[groupName]))
.flat().length;
} else {
this.optionsCount = Object.keys(this.options).length;
}

return this.optionsCount;
},
optionsCount: null,
open: false,
Expand Down Expand Up @@ -93,13 +97,15 @@ const RichSelect = (
this.$refs.button.focus();
},
onArrowUp() {
const optionsCount = this.getOptionsCount();
this.selected =
this.selected - 1 < 0 ? this.optionsCount - 1 : this.selected - 1;
this.selected - 1 < 0 ? optionsCount - 1 : this.selected - 1;
this.scrollToSelectedOption();
},
onArrowDown() {
const optionsCount = this.getOptionsCount();
this.selected =
this.selected + 1 > this.optionsCount - 1 ? 1 : this.selected + 1;
this.selected + 1 > optionsCount - 1 ? 0 : this.selected + 1;
this.scrollToSelectedOption();
},
scrollToSelectedOption() {
Expand Down
1 change: 0 additions & 1 deletion resources/views/inputs/rich-select.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class="input-label @if ($name ?? false) @error($name) input-label--error @enderr
<div
class="relative input-rich-select {{ $wrapperClass }}"
x-data="RichSelect({{ $xData }}, {{ json_encode($options) }}, '{{ $initialValue }}', '{{ $initialText }}', {{ $grouped ? 'true' : 'false'}}@if($dispatchEvent), '{{ $dispatchEvent }}' @endif)"
x-init="init()"
>
<input x-ref="input" {{ $attributes }} type="hidden" @input="onInput($dispatch, $event)" @isset($initialValue) value="{{ $initialValue }}" @endisset />

Expand Down

0 comments on commit 77f6429

Please sign in to comment.