-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): add a headless and daisy select
- Loading branch information
Showing
6 changed files
with
153 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { component$, PropFunction } from '@builder.io/qwik'; | ||
import { | ||
Select as HeadlessSelect, | ||
SelectItemProps, | ||
SelectOption as HeadlessSelectOption, | ||
} from '@qwik-ui/headless'; | ||
|
||
interface SelectProps { | ||
options: SelectItemProps[]; | ||
onChange$?: PropFunction<(evt: any) => void>; | ||
placeholder?: string; | ||
} | ||
|
||
export const Select = component$( | ||
({ onChange$, placeholder, options }: SelectProps) => { | ||
return ( | ||
<HeadlessSelect class="select w-full max-w-xs" onChange={onChange$}> | ||
{placeholder && <HeadlessSelectOption disabled label={placeholder} />} | ||
{options.map((option) => ( | ||
<HeadlessSelectOption | ||
value={option.value} | ||
label={option.label} | ||
disabled={option.disabled} | ||
/> | ||
))} | ||
</HeadlessSelect> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
component$, | ||
createContext, | ||
Signal, | ||
Slot, | ||
$, | ||
QwikChangeEvent, | ||
useContext, | ||
useContextProvider, | ||
useSignal, | ||
PropFunction, | ||
} from '@builder.io/qwik'; | ||
|
||
interface SelectItem { | ||
value?: string; | ||
label: string; | ||
} | ||
|
||
interface SelectContext { | ||
selected: Signal<string>; | ||
} | ||
|
||
export const selectContext = createContext<SelectContext>('select'); | ||
|
||
export interface SelectProps { | ||
onChange?: PropFunction<(evt: QwikChangeEvent) => void>; | ||
class?: string; | ||
} | ||
|
||
export const Select = component$(({ onChange, ...props }: SelectProps) => { | ||
const selected = useSignal(''); | ||
|
||
const contextService: SelectContext = { | ||
selected, | ||
}; | ||
|
||
useContextProvider(selectContext, contextService); | ||
|
||
return ( | ||
<select | ||
role="listbox" | ||
onChange$={$((evt: QwikChangeEvent) => { | ||
contextService.selected = evt.target.value; | ||
if (onChange) { | ||
onChange(evt); | ||
} | ||
})} | ||
{...props} | ||
> | ||
<Slot /> | ||
</select> | ||
); | ||
}); | ||
|
||
export interface SelectItemProps extends SelectItem { | ||
disabled?: boolean; | ||
class?: string; | ||
} | ||
|
||
// single select option | ||
export const SelectOption = component$( | ||
({ value, label, disabled, ...props }: SelectItemProps) => { | ||
const contextService = useContext(selectContext); | ||
|
||
return ( | ||
<option | ||
role="option" | ||
value={value} | ||
selected={value === contextService.selected.value} | ||
disabled={disabled} | ||
{...props} | ||
> | ||
{label} | ||
</option> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters