Skip to content

Commit 77f6429

Browse files
authored
fix: rich select keyboard navigation not working (#23)
1 parent 8727948 commit 77f6429

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

resources/assets/js/rich-select.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ const RichSelect = (
1212
$dispatch(dispatchEvent, $event.target.value);
1313
}
1414
},
15-
init() {
16-
this.$nextTick(() => {
17-
if (grouped) {
18-
this.optionsCount = Object.keys(this.options)
19-
.map((groupName) => Object.keys(this.options[groupName]))
20-
.flat().length;
21-
} else {
22-
this.optionsCount = Object.keys(this.options).length;
23-
}
24-
});
15+
getOptionsCount() {
16+
if (this.optionsCount !== null) {
17+
return this.optionsCount;
18+
}
19+
20+
if (grouped) {
21+
this.optionsCount = Object.keys(this.options)
22+
.map((groupName) => Object.keys(this.options[groupName]))
23+
.flat().length;
24+
} else {
25+
this.optionsCount = Object.keys(this.options).length;
26+
}
27+
28+
return this.optionsCount;
2529
},
2630
optionsCount: null,
2731
open: false,
@@ -93,13 +97,15 @@ const RichSelect = (
9397
this.$refs.button.focus();
9498
},
9599
onArrowUp() {
100+
const optionsCount = this.getOptionsCount();
96101
this.selected =
97-
this.selected - 1 < 0 ? this.optionsCount - 1 : this.selected - 1;
102+
this.selected - 1 < 0 ? optionsCount - 1 : this.selected - 1;
98103
this.scrollToSelectedOption();
99104
},
100105
onArrowDown() {
106+
const optionsCount = this.getOptionsCount();
101107
this.selected =
102-
this.selected + 1 > this.optionsCount - 1 ? 1 : this.selected + 1;
108+
this.selected + 1 > optionsCount - 1 ? 0 : this.selected + 1;
103109
this.scrollToSelectedOption();
104110
},
105111
scrollToSelectedOption() {

resources/views/inputs/rich-select.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class="input-label @if ($name ?? false) @error($name) input-label--error @enderr
3434
<div
3535
class="relative input-rich-select {{ $wrapperClass }}"
3636
x-data="RichSelect({{ $xData }}, {{ json_encode($options) }}, '{{ $initialValue }}', '{{ $initialText }}', {{ $grouped ? 'true' : 'false'}}@if($dispatchEvent), '{{ $dispatchEvent }}' @endif)"
37-
x-init="init()"
3837
>
3938
<input x-ref="input" {{ $attributes }} type="hidden" @input="onInput($dispatch, $event)" @isset($initialValue) value="{{ $initialValue }}" @endisset />
4039

0 commit comments

Comments
 (0)