Skip to content

Commit 78cf6da

Browse files
Merge pull request #823 from thejackshelton/select-focus
fix: disabled select
2 parents d375f0b + f5b256e commit 78cf6da

File tree

8 files changed

+20
-17
lines changed

8 files changed

+20
-17
lines changed

.changeset/purple-games-ring.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik-ui/headless': patch
3+
---
4+
5+
fix: select can now be reactively disabled

apps/website/src/routes/docs/headless/select/snippets/select.css

-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
width: 100%;
1111
height: 100%;
1212
border: 2px dotted hsla(var(--primary) / 1);
13-
border-radius: calc(var (--border-radius) / 2);
1413
min-height: 44px;
1514
max-width: var(--select-width);
1615
padding-block: 0.5rem;
@@ -39,7 +38,6 @@
3938
background-color: hsl(var(--background));
4039
padding: 0.5rem;
4140
border: 2px dotted hsla(var(--foreground) / 0.6);
42-
border-radius: calc(var(--border-radius) / 2);
4341
max-width: var(--select-width);
4442
color: hsl(var(--foreground));
4543
}
@@ -93,7 +91,6 @@
9391
[data-highlighted] {
9492
background-color: hsla(var(--primary) / 0.08);
9593
outline: 2px dotted hsla(var(--primary) / 1);
96-
border-radius: calc(var(--border-radius) / 2);
9794
}
9895

9996
[data-disabled] {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const HHiddenNativeSelect = component$(
4444
multiple={context.multiple}
4545
tabIndex={-1}
4646
autocomplete={autoComplete}
47-
disabled={context.disabled}
47+
disabled={context.isDisabledSig.value ? true : undefined}
4848
required={context.required}
4949
name={context.name}
5050
// height is determined by its children

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type SelectContext = {
2222
highlightedIndexSig: Signal<number | null>;
2323
currDisplayValueSig: Signal<string | string[] | undefined>;
2424
isListboxOpenSig: Signal<boolean>;
25+
isDisabledSig: Signal<boolean>;
2526
localId: string;
2627

2728
// user configurable
@@ -38,11 +39,6 @@ export type SelectContext = {
3839
* Specifies that the user must select a value before submitting the form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#required).
3940
*/
4041
required?: boolean;
41-
42-
/**
43-
* If `true`, prevents the user from interacting with the select.
44-
*/
45-
disabled?: boolean;
4642
};
4743

4844
export const groupContextId = createContextId<GroupContext>('Select-Group');

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export const HSelectDescription = component$((props: SelectDescriptionProps) =>
88
const descriptionId = `${context.localId}-description`;
99

1010
return (
11-
<div id={descriptionId} data-disabled={context.disabled ? '' : undefined} {...props}>
11+
<div
12+
id={descriptionId}
13+
data-disabled={context.isDisabledSig.value ? '' : undefined}
14+
{...props}
15+
>
1216
<Slot />
1317
</div>
1418
);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const HSelectLabel = component$((props: PropsOf<'div'>) => {
66
const labelId = `${context.localId}-label`;
77

88
const handleClick$ = $(() => {
9-
if (context.disabled) return;
9+
if (context.isDisabledSig.value) return;
1010

1111
context.triggerRef.value?.focus();
1212
});
@@ -17,7 +17,7 @@ export const HSelectLabel = component$((props: PropsOf<'div'>) => {
1717

1818
return (
1919
<div
20-
data-disabled={context.disabled ? '' : undefined}
20+
data-disabled={context.isDisabledSig.value ? '' : undefined}
2121
ref={context.labelRef}
2222
id={labelId}
2323
onClick$={[handleClick$, props.onClick$]}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
162162

163163
const initialLoadSig = useSignal<boolean>(true);
164164
const highlightedItemRef = useSignal<HTMLLIElement>();
165+
const isDisabledSig = useSignal<boolean>(disabled ?? false);
165166

166167
const context: SelectContext = {
167168
itemsMapSig,
@@ -181,7 +182,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
181182
multiple,
182183
name,
183184
required,
184-
disabled,
185+
isDisabledSig,
185186
};
186187

187188
useContextProvider(SelectContextId, context);
@@ -281,7 +282,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
281282
});
282283

283284
useTask$(({ track }) => {
284-
context.disabled = track(() => disabled);
285+
isDisabledSig.value = track(() => disabled ?? false);
285286
});
286287

287288
return (
@@ -290,7 +291,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
290291
ref={rootRef}
291292
data-open={context.isListboxOpenSig.value ? '' : undefined}
292293
data-closed={!context.isListboxOpenSig.value ? '' : undefined}
293-
data-disabled={context.disabled ? '' : undefined}
294+
data-disabled={isDisabledSig.value ? '' : undefined}
294295
aria-controls={listboxId}
295296
aria-expanded={context.isListboxOpenSig.value}
296297
aria-haspopup="listbox"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ export const HSelectTrigger = component$<SelectTriggerProps>((props) => {
118118
onKeyDown$={[handleKeyDownSync$, handleKeyDown$, props.onKeyDown$]}
119119
data-open={context.isListboxOpenSig.value ? '' : undefined}
120120
data-closed={!context.isListboxOpenSig.value ? '' : undefined}
121-
data-disabled={context.disabled ? '' : undefined}
121+
data-disabled={context.isDisabledSig.value ? '' : undefined}
122122
aria-expanded={context.isListboxOpenSig.value}
123123
aria-labelledby={labelId}
124124
aria-describedby={descriptionId}
125-
disabled={context.disabled}
125+
disabled={context.isDisabledSig.value ? true : undefined}
126126
preventdefault:blur
127127
>
128128
<Slot />

0 commit comments

Comments
 (0)