Type Listbox with $$Props

This commit is contained in:
Ryan Gossiaux
2022-02-07 17:04:13 -08:00
parent 168f7f1e3d
commit 231775e60b
7 changed files with 98 additions and 19 deletions

View File

@@ -50,6 +50,14 @@
return context; return context;
} }
type TListboxProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp> & {
disabled?: boolean;
horizontal?: boolean;
value?: StateDefinition["value"];
};
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -66,24 +74,32 @@
import { get_current_component } from "svelte/internal"; import { get_current_component } from "svelte/internal";
import type { SupportedAs } from "$lib/internal/elements"; import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions"; import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render from "$lib/utils/Render.svelte"; import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component(), [
"change", /***** Props *****/
]); type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxProps<typeof slotProps, TAsProp>;
export let as: SupportedAs = "div"; export let as: SupportedAs = "div";
export let use: HTMLActionArray = []; export let use: HTMLActionArray = [];
export let disabled = false; export let disabled = false;
export let horizontal = false; export let horizontal = false;
export let value: StateDefinition["value"]; export let value: StateDefinition["value"];
$: orientation = (
horizontal ? "horizontal" : "vertical" /***** Events *****/
) as StateDefinition["orientation"]; const forwardEvents = forwardEventsBuilder(get_current_component(), [
"change",
]);
const dispatch = createEventDispatcher<{ const dispatch = createEventDispatcher<{
change: any; change: any;
}>(); }>();
/***** Component *****/
$: orientation = (
horizontal ? "horizontal" : "vertical"
) as StateDefinition["orientation"];
let listboxState: StateDefinition["listboxState"] = ListboxStates.Closed; let listboxState: StateDefinition["listboxState"] = ListboxStates.Closed;
let labelRef: StateDefinition["labelRef"] = writable(null); let labelRef: StateDefinition["labelRef"] = writable(null);
let buttonRef: StateDefinition["buttonRef"] = writable(null); let buttonRef: StateDefinition["buttonRef"] = writable(null);

View File

@@ -1,3 +1,10 @@
<script lang="ts" context="module">
type TListboxButtonProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp> & {};
</script>
<script lang="ts"> <script lang="ts">
import { tick } from "svelte"; import { tick } from "svelte";
import { ListboxStates, useListboxContext } from "./Listbox.svelte"; import { ListboxStates, useListboxContext } from "./Listbox.svelte";
@@ -8,13 +15,20 @@
import type { SupportedAs } from "$lib/internal/elements"; import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions"; import type { HTMLActionArray } from "$lib/hooks/use-actions";
import { get_current_component } from "svelte/internal"; import { get_current_component } from "svelte/internal";
import Render from "$lib/utils/Render.svelte"; import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
import { resolveButtonType } from "$lib/utils/resolve-button-type"; import { resolveButtonType } from "$lib/utils/resolve-button-type";
const forwardEvents = forwardEventsBuilder(get_current_component()); /***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxButtonProps<typeof slotProps, TAsProp>;
export let as: SupportedAs = "button"; export let as: SupportedAs = "button";
export let use: HTMLActionArray = []; export let use: HTMLActionArray = [];
/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());
/***** Component *****/
let api = useListboxContext("ListboxButton"); let api = useListboxContext("ListboxButton");
let id = `headlessui-listbox-button-${useId()}`; let id = `headlessui-listbox-button-${useId()}`;
let buttonRef = $api.buttonRef; let buttonRef = $api.buttonRef;

View File

@@ -1,16 +1,30 @@
<script lang="ts" context="module">
type TListboxLabelProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp> & {};
</script>
<script lang="ts"> <script lang="ts">
import { ListboxStates, useListboxContext } from "./Listbox.svelte"; import { ListboxStates, useListboxContext } from "./Listbox.svelte";
import { useId } from "$lib/hooks/use-id"; import { useId } from "$lib/hooks/use-id";
import Render from "$lib/utils/Render.svelte"; import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder"; import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
import type { SupportedAs } from "$lib/internal/elements"; import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions"; import type { HTMLActionArray } from "$lib/hooks/use-actions";
import { get_current_component } from "svelte/internal"; import { get_current_component } from "svelte/internal";
const forwardEvents = forwardEventsBuilder(get_current_component()); /***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxLabelProps<typeof slotProps, TAsProp>;
export let as: SupportedAs = "label"; export let as: SupportedAs = "label";
export let use: HTMLActionArray = []; export let use: HTMLActionArray = [];
/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());
/***** Component *****/
let id = `headlessui-listbox-label-${useId()}`; let id = `headlessui-listbox-label-${useId()}`;
let api = useListboxContext("ListboxLabel"); let api = useListboxContext("ListboxLabel");

View File

@@ -1,20 +1,37 @@
<script lang="ts" context="module">
type TListboxOptionProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp> & {
value: unknown;
disabled?: boolean;
};
</script>
<script lang="ts"> <script lang="ts">
import { onDestroy, onMount, tick } from "svelte"; import { onDestroy, onMount, tick } from "svelte";
import { ListboxStates, useListboxContext } from "./Listbox.svelte"; import { ListboxStates, useListboxContext } from "./Listbox.svelte";
import { useId } from "$lib/hooks/use-id"; import { useId } from "$lib/hooks/use-id";
import { Focus } from "$lib/utils/calculate-active-index"; import { Focus } from "$lib/utils/calculate-active-index";
import Render from "$lib/utils/Render.svelte"; import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder"; import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
import type { SupportedAs } from "$lib/internal/elements"; import type { SupportedAs } from "$lib/internal/elements";
import { get_current_component } from "svelte/internal"; import { get_current_component } from "svelte/internal";
import type { HTMLActionArray } from "$lib/hooks/use-actions"; import type { HTMLActionArray } from "$lib/hooks/use-actions";
const forwardEvents = forwardEventsBuilder(get_current_component()); /***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxOptionProps<typeof slotProps, TAsProp>;
export let as: SupportedAs = "li"; export let as: SupportedAs = "li";
export let use: HTMLActionArray = []; export let use: HTMLActionArray = [];
export let value: unknown; export let value: unknown;
export let disabled = false; export let disabled = false;
/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());
/***** Component *****/
let api = useListboxContext("ListboxOption"); let api = useListboxContext("ListboxOption");
let id = `headlessui-listbox-option-${useId()}`; let id = `headlessui-listbox-option-${useId()}`;

View File

@@ -1,3 +1,13 @@
<script lang="ts" context="module">
type TListboxOptionsProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp> & {
unmount?: boolean;
static?: boolean;
};
</script>
<script lang="ts"> <script lang="ts">
import { tick } from "svelte"; import { tick } from "svelte";
import { ListboxStates, useListboxContext } from "./Listbox.svelte"; import { ListboxStates, useListboxContext } from "./Listbox.svelte";
@@ -6,16 +16,26 @@
import { Keys } from "$lib/utils/keyboard"; import { Keys } from "$lib/utils/keyboard";
import { Focus } from "$lib/utils/calculate-active-index"; import { Focus } from "$lib/utils/calculate-active-index";
import { State, useOpenClosed } from "$lib/internal/open-closed"; import { State, useOpenClosed } from "$lib/internal/open-closed";
import Render, { Features } from "$lib/utils/Render.svelte"; import Render, {
Features,
type TPassThroughProps,
} from "$lib/utils/Render.svelte";
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder"; import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
import type { SupportedAs } from "$lib/internal/elements"; import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions"; import type { HTMLActionArray } from "$lib/hooks/use-actions";
import { get_current_component } from "svelte/internal"; import { get_current_component } from "svelte/internal";
const forwardEvents = forwardEventsBuilder(get_current_component()); /***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxOptionsProps<typeof slotProps, TAsProp>;
export let as: SupportedAs = "ul"; export let as: SupportedAs = "ul";
export let use: HTMLActionArray = []; export let use: HTMLActionArray = [];
/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());
/***** Component *****/
let api = useListboxContext("ListboxOptions"); let api = useListboxContext("ListboxOptions");
let id = `headlessui-listbox-options-${useId()}`; let id = `headlessui-listbox-options-${useId()}`;
let optionsRef = $api.optionsRef; let optionsRef = $api.optionsRef;

View File

@@ -75,7 +75,6 @@
> >
{#each people as name (name)} {#each people as name (name)}
<ListboxOption <ListboxOption
key={name}
value={name} value={name}
class={({ active }) => { class={({ active }) => {
return classNames( return classNames(

View File

@@ -74,7 +74,6 @@
> >
{#each people as name (name)} {#each people as name (name)}
<ListboxOption <ListboxOption
key={name}
value={name} value={name}
class={({ active }) => { class={({ active }) => {
return classNames( return classNames(