Skip to content

Commit a025698

Browse files
refactor(select): clean up props
1 parent f144689 commit a025698

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -515,33 +515,65 @@ test.describe('Props', () => {
515515
await expect(await getValue()).toEqual('Select an option');
516516
});
517517

518-
test(`GIVEN an uncontrolled select with a value prop on the root component
518+
test.describe('uncontrolled', () => {
519+
test(`GIVEN an uncontrolled select with a value prop on the root component
519520
WHEN the value data matches the fourth option
520521
THEN the selected value should be the data passed to the value prop`, async ({
521-
page,
522-
}) => {
523-
const { getValue } = await setup(page, 'select-uncontrolled-test');
522+
page,
523+
}) => {
524+
const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
524525

525-
await expect(await getValue()).toEqual('Jessie');
526-
});
526+
const options = await getOptions();
527527

528-
test(`GIVEN an uncontrolled select with a value prop on the root component
529-
WHEN the value data matches the fourth option
528+
expect(await getValue()).toEqual(await options[3].textContent());
529+
});
530+
531+
test(`GIVEN an uncontrolled select with a value prop on the root component
532+
WHEN the value prop data matches the fourth option
530533
THEN the fourth option should have data-highlighted set to true`, async ({
531-
page,
532-
}) => {
533-
const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
534+
page,
535+
}) => {
536+
const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
534537

535-
expect(await getValue()).toEqual('Jessie');
536-
const options = await getOptions();
537-
await expect(options[3]).toHaveAttribute('data-highlighted');
538+
const options = await getOptions();
539+
expect(await getValue()).toEqual(await options[3].textContent());
540+
await expect(options[3]).toHaveAttribute('data-highlighted');
541+
});
542+
543+
test(`GIVEN an uncontrolled select with a value prop on the root component
544+
WHEN the value prop data matches the fourth option
545+
THEN the fourth option should have aria-selected set to true`, async ({
546+
page,
547+
}) => {
548+
const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
549+
550+
const options = await getOptions();
551+
expect(await getValue()).toEqual(await options[3].textContent());
552+
await expect(options[3]).toHaveAttribute('aria-selected', 'true');
553+
});
554+
555+
test(`GIVEN an uncontrolled select with a value prop on the root component
556+
WHEN the value data does NOT match any option
557+
THEN fallback to the placeholder`, async ({ page }) => {
558+
const { getValue } = await setup(page, 'select-wrong-value-test');
559+
560+
/**
561+
we also currently give a console warning in the terminal if an option does not match. Ideally, I would like to have a union type of options, and it gives an error if there is no matching option
562+
*/
563+
expect(await getValue()).toEqual('wrong value placeholder');
564+
});
538565
});
539566

540-
// test(`GIVEN an uncontrolled select with a value prop on the root component
541-
// WHEN the value data does NOT match any option
542-
// THEN throw an error`, async ({ page }) => {
543-
// const { getValue } = await setup(page, 'select-wrong-value-test');
567+
// test.describe('controlled', () => {
568+
// test(`GIVEN a controlled select with a bind:value prop on the root component
569+
// WHEN the signal data matches the second option
570+
// THEN the selected value should be the data passed to the value prop`, async ({
571+
// page,
572+
// }) => {
573+
// const { getValue, getOptions } = await setup(page, 'select-uncontrolled-test');
544574

545-
// expect(await getValue()).toEqual('Jessi');
575+
// const options = await getOptions();
576+
// expect(await getValue()).toEqual(await options[1].textContent());
577+
// });
546578
// });
547579
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ export type SelectContext = {
1818
highlightedIndexSig: Signal<number | null>;
1919
isListboxOpenSig: Signal<boolean>;
2020
selectedIndexSig: Signal<number | null>;
21-
value: string | undefined;
2221
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const Select: FunctionComponent<SelectProps> = (props) => {
5353
const isString = typeof child.props.children === 'string';
5454
if (!isString) {
5555
throw new Error(
56-
`Qwik UI: Select option value passed was not a string. It was an ${typeof child
56+
`Qwik UI: Select option value passed was not a string. It was a ${typeof child
5757
.props.children}.`,
5858
);
5959
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ export const SelectImpl = component$<SelectProps>((props) => {
2626
const triggerRef = useSignal<HTMLButtonElement>();
2727
const popoverRef = useSignal<HTMLElement>();
2828
const listboxRef = useSignal<HTMLUListElement>();
29-
const value = props.value;
3029
const options = props._options;
3130

3231
// core state
3332
const selectedIndexSig = useSignal<number | null>(props._valuePropIndex ?? null);
34-
const isListboxOpenSig = useSignal<boolean>(false);
3533
const highlightedIndexSig = useSignal<number | null>(props._valuePropIndex ?? null);
34+
const isListboxOpenSig = useSignal<boolean>(false);
3635

3736
const context: SelectContext = {
3837
triggerRef,
@@ -42,7 +41,6 @@ export const SelectImpl = component$<SelectProps>((props) => {
4241
highlightedIndexSig,
4342
isListboxOpenSig,
4443
selectedIndexSig,
45-
value,
4644
};
4745

4846
useContextProvider(SelectContextId, context);

0 commit comments

Comments
 (0)