Skip to content

Commit 8994c3c

Browse files
fix(select): grab correct selected option based on value prop
1 parent 2b2eeab commit 8994c3c

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

apps/website/src/routes/docs/headless/select/select.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,15 @@ test.describe('Props', () => {
508508
await expect(await getValue()).toEqual('Jessie');
509509
});
510510

511-
// test(`GIVEN an uncontrolled select with a value prop on the root component
512-
// WHEN the value data matches the fourth option
513-
// THEN the fourth option should have aria-selected set to true`, async ({
514-
// page,
515-
// }) => {
516-
// const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
517-
518-
// await expect(await getValue()).toEqual('Jessie');
519-
// const options = await getOptions();
520-
// await expect(await options[3]).toBeSelected();
521-
// });
511+
test(`GIVEN an uncontrolled select with a value prop on the root component
512+
WHEN the value data matches the fourth option
513+
THEN the fourth option should have data-highlighted set to true`, async ({
514+
page,
515+
}) => {
516+
const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
517+
518+
await expect(await getValue()).toEqual('Jessie');
519+
const options = await getOptions();
520+
await expect(await options[3]).toHaveAttribute('data-highlighted');
521+
});
522522
});

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SelectOption } from './select-option';
1010
export const Select: FunctionComponent<SelectProps> = (props) => {
1111
const { children: myChildren, ...rest } = props;
1212
let valuePropIndex = 0;
13-
13+
const isDisabledArr = [];
1414
const childrenToProcess = (
1515
Array.isArray(myChildren) ? [...myChildren] : [myChildren]
1616
) as Array<JSXNode>;
@@ -49,11 +49,17 @@ export const Select: FunctionComponent<SelectProps> = (props) => {
4949
if (child.props.children === props.value) {
5050
valuePropIndex = currentIndex;
5151
}
52-
52+
isDisabledArr.push(child.props.disabled);
5353
currentIndex++;
5454
}
5555
}
5656
}
57+
58+
// TODO: will have problems if all options are disabled
59+
if (isDisabledArr[valuePropIndex] === true) {
60+
valuePropIndex = isDisabledArr.findIndex((isDisabled) => isDisabled === false);
61+
}
62+
5763
return (
5864
<SelectImpl {...rest} valuePropIndex={valuePropIndex}>
5965
{props.children}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
3131
const shouldOpen = !context.isListboxOpenSig.value && openKeys.includes(e.key);
3232
const shouldClose = context.isListboxOpenSig.value && closedKeys.includes(e.key);
3333

34+
// TODO: refactor with data grabbed from inline component
3435
const options: OptionsType = context.optionRefsArray.value.map((option) => {
3536
if (option.value === undefined) {
3637
throw new Error('Qwik UI: internal select option is undefined');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type SelectProps = PropsOf<'div'> & {
1414
value?: string;
1515

1616
// when a value is passed, we check if it's an actual option value, and get its index at pre-render time.
17-
valuePropIndex: number;
17+
valuePropIndex?: number;
1818
};
1919

2020
/* root component in select-inline.tsx */
@@ -31,7 +31,7 @@ export const SelectImpl = component$<SelectProps>((props) => {
3131
const selectedIndexSig = useSignal<number | null>(null);
3232
const selectedOptionRef = useSignal<HTMLLIElement | null>(null);
3333
const isListboxOpenSig = useSignal<boolean>(false);
34-
const highlightedIndexSig = useSignal<number | null>(props.valuePropIndex);
34+
const highlightedIndexSig = useSignal<number | null>(props.valuePropIndex ?? null);
3535

3636
useTask$(function deriveSelectedRef({ track }) {
3737
track(() => selectedIndexSig.value);

0 commit comments

Comments
 (0)