You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.2 KiB
85 lines
2.2 KiB
<script lang="ts">
|
|
/**
|
|
* # Combobox Input
|
|
*
|
|
* Campo de texto para buscar y filtrar las opciones del combobox.
|
|
*
|
|
* ## Uso
|
|
* ```svelte
|
|
* <Combobox bind:inputValue={search}>
|
|
* <ComboboxInput
|
|
* placeholder="Buscar..."
|
|
* defaultValue=""
|
|
* />
|
|
* <ComboboxContent>...</ComboboxContent>
|
|
* </Combobox>
|
|
* ```
|
|
*
|
|
* ## Propiedades
|
|
* - `defaultValue`: string - Valor inicial del input
|
|
* - `clearOnDeselect`: boolean - Limpia el input al deseleccionar
|
|
* - `id`: string - ID del elemento
|
|
*
|
|
* ## Selectores CSS
|
|
* ```
|
|
* [data-combobox-input] → Input de texto del combobox
|
|
* [data-state="open"] → Estado del combobox abierto
|
|
* [data-state="closed"] → Estado del combobox cerrado
|
|
* [data-disabled] → Input deshabilitado
|
|
* ```
|
|
*
|
|
* ## Estilos
|
|
* Renderiza un elemento `<input>` sincronizado con el `inputValue` del Combobox.
|
|
*/
|
|
import {
|
|
mergeProps,
|
|
readableActive,
|
|
writableActive,
|
|
createId
|
|
} from '$terra/utils';
|
|
|
|
import FloatingLayerAnchor from '../../layers/floating/components/floating-layer-anchor.svelte';
|
|
import { SelectInputState } from '../../select/select.svelte';
|
|
import type { ComboboxInputProps } from '../types';
|
|
|
|
const uid = $props.id();
|
|
|
|
let {
|
|
id = createId(uid),
|
|
ref = $bindable(null),
|
|
child,
|
|
defaultValue,
|
|
clearOnDeselect = false,
|
|
...restProps
|
|
}: ComboboxInputProps = $props();
|
|
|
|
const inputState = SelectInputState.create({
|
|
id: readableActive(() => id),
|
|
ref: writableActive(
|
|
() => ref,
|
|
(v) => (ref = v)
|
|
),
|
|
clearOnDeselect: readableActive(() => clearOnDeselect),
|
|
});
|
|
|
|
let hasAppliedDefaultValue = false;
|
|
$effect(() => {
|
|
if (hasAppliedDefaultValue) return;
|
|
if (defaultValue === undefined || defaultValue === '') return;
|
|
inputState.root.opts.inputValue.current = defaultValue;
|
|
hasAppliedDefaultValue = true;
|
|
});
|
|
|
|
const mergedProps = $derived(
|
|
mergeProps(restProps, inputState.props, { value: inputState.root.opts.inputValue.current })
|
|
);
|
|
</script>
|
|
|
|
<FloatingLayerAnchor {id} ref={inputState.opts.ref}>
|
|
{#if child}
|
|
{@render child({ props: mergedProps })}
|
|
{:else}
|
|
<input {...mergedProps} />
|
|
{/if}
|
|
</FloatingLayerAnchor>
|