Skip to content

Commit 0ec57f1

Browse files
refactor(select): replaced trigger options with data we get from ssr
1 parent b0ca702 commit 0ec57f1

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

packages/kit-headless/src/components/select/select-inline.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const Select: FunctionComponent<SelectProps> = (props) => {
8484
}
8585

8686
return (
87-
<SelectImpl {...rest} _valuePropIndex={valuePropIndex}>
87+
<SelectImpl {...rest} _valuePropIndex={valuePropIndex} _options={opts}>
8888
{props.children}
8989
</SelectImpl>
9090
);

packages/kit-headless/src/components/select/select-trigger.tsx

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { component$, type PropsOf, useContext, sync$, $, Slot } from '@builder.io/qwik';
22
import SelectContextId from './select-context';
33
import { getNextEnabledOptionIndex, getPrevEnabledOptionIndex } from './utils';
4-
5-
export type OptionsType = {
6-
element: HTMLLIElement;
7-
isDisabled: boolean;
8-
}[];
94
export type OpenKeys = 'ArrowUp' | 'Enter' | 'Space' | 'ArrowDown';
105

116
type SelectTriggerProps = PropsOf<'button'>;
12-
export type DisabledArr = Array<{ disabled: boolean }>;
137
export const SelectTrigger = component$<SelectTriggerProps>((props) => {
148
const context = useContext(SelectContextId);
159
const openKeys = ['ArrowUp', 'ArrowDown'];
@@ -30,17 +24,7 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
3024
const handleKeyDown$ = $((e: KeyboardEvent) => {
3125
const shouldOpen = !context.isListboxOpenSig.value && openKeys.includes(e.key);
3226
const shouldClose = context.isListboxOpenSig.value && closedKeys.includes(e.key);
33-
34-
// TODO: refactor with data grabbed from inline component
35-
const options: OptionsType = context.optionRefsArray.value.map((option) => {
36-
if (option.value === undefined) {
37-
throw new Error('Qwik UI: internal select option is undefined');
38-
}
39-
40-
const isDisabled = option.value.ariaDisabled === 'true';
41-
42-
return { element: option.value, isDisabled };
43-
});
27+
if (!context.options) return;
4428

4529
if (shouldOpen) {
4630
context.isListboxOpenSig.value = true;
@@ -51,32 +35,35 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
5135
}
5236

5337
if (e.key === 'Home') {
54-
context.highlightedIndexSig.value = getNextEnabledOptionIndex(-1, options);
38+
context.highlightedIndexSig.value = getNextEnabledOptionIndex(-1, context.options);
5539
}
5640

5741
if (e.key === 'End') {
58-
const lastEnabledOptionIndex = getPrevEnabledOptionIndex(options.length, options);
42+
const lastEnabledOptionIndex = getPrevEnabledOptionIndex(
43+
context.options.length,
44+
context.options,
45+
);
5946
context.highlightedIndexSig.value = lastEnabledOptionIndex;
6047
}
6148

6249
/** When initially opening the listbox, we want to grab the first enabled option index */
6350
if (context.highlightedIndexSig.value === null) {
64-
context.highlightedIndexSig.value = getNextEnabledOptionIndex(-1, options);
51+
context.highlightedIndexSig.value = getNextEnabledOptionIndex(-1, context.options);
6552
return;
6653
}
6754

6855
if (context.isListboxOpenSig.value && !shouldOpen) {
6956
if (e.key === 'ArrowDown') {
7057
context.highlightedIndexSig.value = getNextEnabledOptionIndex(
7158
context.highlightedIndexSig.value,
72-
options,
59+
context.options,
7360
);
7461
}
7562

7663
if (e.key === 'ArrowUp') {
7764
context.highlightedIndexSig.value = getPrevEnabledOptionIndex(
7865
context.highlightedIndexSig.value,
79-
options,
66+
context.options,
8067
);
8168
}
8269

packages/kit-headless/src/components/select/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OptionsType } from './select-trigger';
1+
import { type Opt } from './select-inline';
22

3-
export const getNextEnabledOptionIndex = (index: number, options: OptionsType) => {
3+
export const getNextEnabledOptionIndex = (index: number, options: Opt[]) => {
44
let offset = 1;
55
let currentIndex = index;
66
const opts = options;
@@ -21,7 +21,7 @@ export const getNextEnabledOptionIndex = (index: number, options: OptionsType) =
2121
return (currentIndex + offset) % len;
2222
};
2323

24-
export const getPrevEnabledOptionIndex = (index: number, options: OptionsType) => {
24+
export const getPrevEnabledOptionIndex = (index: number, options: Opt[]) => {
2525
let offset = 1;
2626
let currentIndex = index;
2727
const opts = options;

0 commit comments

Comments
 (0)