@pdanpdan/headless-combobox-solid 0.0.0-stage → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +54 -2
- package/dist/HeadlessCombobox.d.ts +15 -0
- package/dist/HeadlessCombobox.d.ts.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +59 -0
- package/dist/index.mjs.map +1 -0
- package/dist/useHeadlessCombobox.d.ts +27 -0
- package/dist/useHeadlessCombobox.d.ts.map +1 -0
- package/package.json +78 -3
- package/src/HeadlessCombobox.tsx +24 -0
- package/src/index.ts +15 -0
- package/src/useHeadlessCombobox.ts +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pdanpdan/headless-combobox-solid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Accessible, renderless combobox for Solid. Backed by [`@pdanpdan/headless-combobox-core`](../headless-combobox-core).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @pdanpdan/headless-combobox-solid
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`solid-js@>=2` and `@solidjs/web@>=2` are peer dependencies.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { HeadlessCombobox, toSolidProps } from '@pdanpdan/headless-combobox-solid';
|
|
17
|
+
import { createSignal, For } from 'solid-js';
|
|
18
|
+
|
|
19
|
+
const users = [
|
|
20
|
+
{ id: 1, name: 'Wade Cooper' },
|
|
21
|
+
{ id: 2, name: 'Arlene Mccoy' },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export function Example() {
|
|
25
|
+
const [ value, setValue ] = createSignal<typeof users[ number ] | null>(null);
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<HeadlessCombobox
|
|
29
|
+
options={users}
|
|
30
|
+
optionLabel={(user) => user.name}
|
|
31
|
+
value={value()}
|
|
32
|
+
onChange={(next) => setValue(next as typeof users[ number ] | null)}
|
|
33
|
+
>
|
|
34
|
+
{(scope) => (
|
|
35
|
+
<div ref={scope().setContainerRef}>
|
|
36
|
+
<button ref={scope().setTriggerRef} type="button" {...toSolidProps(scope().triggerProps)}>
|
|
37
|
+
{value()?.name ?? 'Select…'}
|
|
38
|
+
</button>
|
|
39
|
+
<ul ref={scope().setDropdownRef} {...toSolidProps(scope().listboxProps)}>
|
|
40
|
+
<For each={scope().filteredOptions}>
|
|
41
|
+
{(user, index) => (
|
|
42
|
+
<li {...toSolidProps(scope().getOptionProps(user, index()))}>
|
|
43
|
+
{user.name}
|
|
44
|
+
</li>
|
|
45
|
+
)}
|
|
46
|
+
</For>
|
|
47
|
+
</ul>
|
|
48
|
+
</div>
|
|
49
|
+
)}
|
|
50
|
+
</HeadlessCombobox>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The render prop receives an **accessor**. Keep calling `scope()` inside JSX so every binding stays reactive, because Solid components run once. `useHeadlessCombobox(() => options)` returns the same accessor without the component wrapper.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { UseHeadlessComboboxOptions } from './useHeadlessCombobox';
|
|
2
|
+
import { HeadlessComboboxSnapshot } from '../../headless-combobox-core/src/index.ts';
|
|
3
|
+
import { JSX } from '@solidjs/web';
|
|
4
|
+
import { Accessor } from 'solid-js';
|
|
5
|
+
/** Props for the render-prop component: hook options plus a scope renderer. */
|
|
6
|
+
export interface HeadlessComboboxProps<O, V = O, Q = string> extends UseHeadlessComboboxOptions<O, V, Q> {
|
|
7
|
+
/** The scope accessor keeps every binding reactive (Solid components run once). */
|
|
8
|
+
children: (scope: Accessor<HeadlessComboboxSnapshot<O, V, Q>>) => JSX.Element;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Render-prop wrapper around {@link useHeadlessCombobox}. Prefer the hook when
|
|
12
|
+
* the markup does not need to be nested under a component.
|
|
13
|
+
*/
|
|
14
|
+
export declare function HeadlessCombobox<O, V = O, Q = string>(props: HeadlessComboboxProps<O, V, Q>): JSX.Element;
|
|
15
|
+
//# sourceMappingURL=HeadlessCombobox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HeadlessCombobox.d.ts","sourceRoot":"","sources":["../src/HeadlessCombobox.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AACxE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AACjF,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIzC,+EAA+E;AAC/E,MAAM,WAAW,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CACzD,SAAQ,0BAA0B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC3C,mFAAmF;IACnF,QAAQ,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;CAC/E;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EACnD,KAAK,EAAE,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GACpC,GAAG,CAAC,OAAO,CAGb"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@pdanpdan/headless-combobox-core"),t=require("solid-js");var n=[`options`,`multiple`,`minLength`,`maxLength`,`required`,`disabled`,`readonly`,`closeOnSelect`,`closeOnClickOutside`,`clickOutsideFilter`,`selectOnTab`,`optionValue`,`optionLabel`,`optionFilter`,`id`,`alignSelected`,`inputOnOpen`,`errorMessages`];function r(n){let[r,i]=(0,t.createSignal)((0,t.untrack)(()=>n().defaultValue??null)),a=()=>`value`in n(),s=()=>a()?n().value??null:r(),c=(0,e.createCombobox)({props:(0,t.untrack)(()=>o(n(),s())),onChange:e=>{a()||i(()=>e),n().onChange?.(e)},runtime:{nextTick:()=>Promise.resolve()}}),[l,u]=(0,t.createSignal)(c.getSnapshot(),{equals:!1});return(0,t.createEffect)(()=>o(n(),s()),e=>c.setProps(e)),(0,t.onCleanup)(c.subscribe(()=>u(c.getSnapshot()))),(0,t.onSettled)(()=>(c.mount(),()=>c.destroy())),l}var i={onKeydown:`onKeyDown`,onMousedown:`onMouseDown`,onMousemove:`onMouseMove`};function a(e){let t={};for(let[n,r]of Object.entries(e)){let e=i[n]??n;t[e]=e.startsWith(`aria-`)&&typeof r==`boolean`?String(r):r}return t}function o(e,t){let r={};for(let t of n)r[t]=e[t];return r.modelValue=t,r}function s(e){let t=r(()=>e);return e.children(t)}exports.HeadlessCombobox=s,exports.toSolidProps=a,exports.useHeadlessCombobox=r;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["createSignal","untrack","createCombobox","_pdanpdan_headless_combobox_core","solid_js","CORE_PROP_KEYS","useHeadlessCombobox","uncontrolled","setUncontrolled","options","controlled","modelValue","store","toCoreProps","value","snapshot","setSnapshot","next","SOLID_EVENT_NAMES","toSolidProps","adapted","key","props","name","resolved","HeadlessCombobox","scope"],"sources":["../src/useHeadlessCombobox.ts","../src/HeadlessCombobox.tsx"],"sourcesContent":["import type {\n HeadlessComboboxProps as CoreComboboxProps,\n HeadlessComboboxSnapshot,\n} from '@pdanpdan/headless-combobox-core';\nimport type { Accessor, Setter } from 'solid-js';\n\nimport { createCombobox } from '@pdanpdan/headless-combobox-core';\nimport { createEffect, createSignal, onCleanup, onSettled, untrack } from 'solid-js';\n\n/** Options accepted by the Solid hook and component. */\nexport type UseHeadlessComboboxOptions<O, V = O, Q = string>\n = Omit<CoreComboboxProps<O, V, Q>, 'modelValue'> & {\n /** Controlled value (`value` present) or omit for uncontrolled. */\n value?: V | V[] | null;\n /** Initial value for uncontrolled usage. */\n defaultValue?: V | V[] | null;\n /** Called for every selection change. */\n onChange?: (value: V | V[] | null) => void;\n };\n\nconst CORE_PROP_KEYS = [\n 'options',\n 'multiple',\n 'minLength',\n 'maxLength',\n 'required',\n 'disabled',\n 'readonly',\n 'closeOnSelect',\n 'closeOnClickOutside',\n 'clickOutsideFilter',\n 'selectOnTab',\n 'optionValue',\n 'optionLabel',\n 'optionFilter',\n 'id',\n 'alignSelected',\n 'inputOnOpen',\n 'errorMessages',\n] as const;\n\n/**\n * Solid adapter for the framework-independent combobox core.\n *\n * Returns an accessor for the snapshot, so `<button {...toSolidProps(scope().triggerProps)} />`\n * stays reactive. The options accessor is tracked by a `createEffect`.\n */\nexport function useHeadlessCombobox<O, V = O, Q = string>(\n options: Accessor<UseHeadlessComboboxOptions<O, V, Q>>,\n): Accessor<HeadlessComboboxSnapshot<O, V, Q>> {\n // Solid 2's createSignal overloads cannot express a generic `V | V[] | null`\n // initial value; the runtime value is the same.\n const [ uncontrolled, setUncontrolled ] = createSignal<unknown>(\n untrack(() => options().defaultValue ?? null),\n ) as unknown as [Accessor<V | V[] | null>, Setter<V | V[] | null>];\n const controlled = () => 'value' in options();\n const modelValue = () => (controlled()\n ? (options().value ?? null)\n : uncontrolled());\n\n const store = createCombobox<O, V, Q>({\n props: untrack(() => toCoreProps(options(), modelValue())),\n onChange: (value) => {\n if (!controlled()) {\n setUncontrolled(() => value);\n }\n options().onChange?.(value);\n },\n runtime: {\n nextTick: () => Promise.resolve(),\n },\n });\n\n const [ snapshot, setSnapshot ] = createSignal(store.getSnapshot(), { equals: false });\n\n createEffect(\n () => toCoreProps(options(), modelValue()),\n (next) => store.setProps(next),\n );\n\n onCleanup(store.subscribe(() => setSnapshot(store.getSnapshot())));\n onSettled(() => {\n store.mount();\n return () => store.destroy();\n });\n\n return snapshot;\n}\n\nconst SOLID_EVENT_NAMES: Record<string, string> = {\n onKeydown: 'onKeyDown',\n onMousedown: 'onMouseDown',\n onMousemove: 'onMouseMove',\n};\n\n/**\n * Adapt a core ARIA prop bag to Solid's JSX event prop names:\n *\n * ```tsx\n * <button {...toSolidProps(scope().triggerProps)} />\n * ```\n */\nexport function toSolidProps<T extends object>(props: T): Record<string, unknown> {\n const adapted: Record<string, unknown> = {};\n for (const [ key, value ] of Object.entries(props)) {\n const name = SOLID_EVENT_NAMES[ key ] ?? key;\n // Solid 2 removes attributes set to false, including ARIA state.\n adapted[ name ] = name.startsWith('aria-') && typeof value === 'boolean'\n ? String(value)\n : value;\n }\n return adapted;\n}\n\nfunction toCoreProps<O, V, Q>(\n options: UseHeadlessComboboxOptions<O, V, Q>,\n modelValue: V | V[] | null,\n): CoreComboboxProps<O, V, Q> {\n const resolved: Record<string, unknown> = {};\n for (const key of CORE_PROP_KEYS) {\n resolved[ key ] = options[ key ];\n }\n resolved.modelValue = modelValue;\n return resolved as unknown as CoreComboboxProps<O, V, Q>;\n}\n","import type { UseHeadlessComboboxOptions } from './useHeadlessCombobox';\nimport type { HeadlessComboboxSnapshot } from '@pdanpdan/headless-combobox-core';\nimport type { JSX } from '@solidjs/web';\nimport type { Accessor } from 'solid-js';\n\nimport { useHeadlessCombobox } from './useHeadlessCombobox';\n\n/** Props for the render-prop component: hook options plus a scope renderer. */\nexport interface HeadlessComboboxProps<O, V = O, Q = string>\n extends UseHeadlessComboboxOptions<O, V, Q> {\n /** The scope accessor keeps every binding reactive (Solid components run once). */\n children: (scope: Accessor<HeadlessComboboxSnapshot<O, V, Q>>) => JSX.Element;\n}\n\n/**\n * Render-prop wrapper around {@link useHeadlessCombobox}. Prefer the hook when\n * the markup does not need to be nested under a component.\n */\nexport function HeadlessCombobox<O, V = O, Q = string>(\n props: HeadlessComboboxProps<O, V, Q>,\n): JSX.Element {\n const scope = useHeadlessCombobox<O, V, Q>(() => props);\n return props.children(scope);\n}\n"],"mappings":"A,2IAoBA,IAAMK,EAAiB,CACrB,UACA,WACA,YACA,YACA,WACA,WACA,WACA,gBACA,sBACA,qBACA,cACA,cACA,cACA,eACA,KACA,gBACA,cACA,eACF,EAQA,SAAgBC,EACd,EAC6C,CAG7C,GAAM,CAAEC,EAAcC,IAAA,EAAoBR,EAAAA,aAAAA,EAAAA,EACxCC,EAAAA,QAAAA,KAAcQ,EAAQ,CAAC,CAAC,cAAgB,IAAI,CAC9C,EACMC,MAAmB,UAAWD,EAAQ,EACtCE,MAAoBD,EAAW,EAChCD,EAAQ,CAAC,CAAC,OAAS,KACpBF,EAAa,EAEXK,GAAA,EAAQV,EAAAA,eAAAA,CAAwB,CACpC,OAAA,EAAOD,EAAAA,QAAAA,KAAcY,EAAYJ,EAAQ,EAAGE,EAAW,CAAC,CAAC,EACzD,SAAW,GAAU,CACdD,EAAW,GACdF,MAAsBM,CAAK,EAE7BL,EAAQ,CAAC,CAAC,WAAWK,CAAK,CAC5B,EACA,QAAS,CACP,aAAgB,QAAQ,QAAQ,CAClC,CACF,CAAC,EAEK,CAAEC,EAAUC,IAAA,EAAgBhB,EAAAA,aAAAA,CAAaY,EAAM,YAAY,EAAG,CAAE,OAAQ,EAAM,CAAC,EAarF,OAXA,EAAAR,EAAA,aAAA,KACQS,EAAYJ,EAAQ,EAAGE,EAAW,CAAC,EACxC,GAASC,EAAM,SAASK,CAAI,CAC/B,GAEA,EAAAb,EAAA,UAAA,CAAUQ,EAAM,cAAgBI,EAAYJ,EAAM,YAAY,CAAC,CAAC,CAAC,GACjE,EAAAR,EAAA,UAAA,MACEQ,EAAM,MAAM,MACCA,EAAM,QAAQ,EAC5B,EAEMG,CACT,CAEA,IAAMG,EAA4C,CAChD,UAAW,YACX,YAAa,cACb,YAAa,aACf,EASA,SAAgBC,EAA+B,EAAmC,CAChF,IAAMC,EAAmC,CAAC,EAC1C,IAAK,GAAM,CAAEC,EAAKP,KAAW,OAAO,QAAQQ,CAAK,EAAG,CAClD,IAAMC,EAAOL,EAAmBG,IAASA,EAEzC,EAASE,GAASA,EAAK,WAAW,OAAO,GAAK,OAAOT,GAAU,UAC3D,OAAOA,CAAK,EACZA,CACN,CACA,OAAOM,CACT,CAEA,SAASP,EACP,EACA,EAC4B,CAC5B,IAAMW,EAAoC,CAAC,EAC3C,IAAK,IAAMH,KAAOhB,EAChB,EAAUgB,GAAQZ,EAASY,GAG7B,MADA,GAAS,WAAaV,EACfa,CACT,CC1GA,SAAgBC,EACd,EACa,CACb,IAAMC,EAAQpB,MAAmCgB,CAAK,EACtD,OAAOA,EAAM,SAASI,CAAK,CAC7B,C"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { HeadlessComboboxComboboxInputProps, HeadlessComboboxErrorCode, HeadlessComboboxInputProps, HeadlessComboboxListboxProps, HeadlessComboboxOptionProps, HeadlessComboboxPopupStyle, HeadlessComboboxSnapshot, HeadlessComboboxTriggerProps, } from '../../headless-combobox-core/src/index.ts';
|
|
2
|
+
export type { UseHeadlessComboboxOptions } from './useHeadlessCombobox';
|
|
3
|
+
export type { HeadlessComboboxProps } from './HeadlessCombobox';
|
|
4
|
+
export { HeadlessCombobox } from './HeadlessCombobox';
|
|
5
|
+
export { toSolidProps, useHeadlessCombobox } from './useHeadlessCombobox';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,kCAAkC,EAClC,yBAAyB,EACzB,0BAA0B,EAC1B,4BAA4B,EAC5B,2BAA2B,EAC3B,0BAA0B,EAC1B,wBAAwB,EACxB,4BAA4B,GAC7B,MAAM,kCAAkC,CAAC;AAC1C,YAAY,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AACxE,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createCombobox as e } from "@pdanpdan/headless-combobox-core";
|
|
2
|
+
import { createEffect as t, createSignal as n, onCleanup as r, onSettled as i, untrack as a } from "solid-js";
|
|
3
|
+
// src/useHeadlessCombobox.ts
|
|
4
|
+
var o = [
|
|
5
|
+
"options",
|
|
6
|
+
"multiple",
|
|
7
|
+
"minLength",
|
|
8
|
+
"maxLength",
|
|
9
|
+
"required",
|
|
10
|
+
"disabled",
|
|
11
|
+
"readonly",
|
|
12
|
+
"closeOnSelect",
|
|
13
|
+
"closeOnClickOutside",
|
|
14
|
+
"clickOutsideFilter",
|
|
15
|
+
"selectOnTab",
|
|
16
|
+
"optionValue",
|
|
17
|
+
"optionLabel",
|
|
18
|
+
"optionFilter",
|
|
19
|
+
"id",
|
|
20
|
+
"alignSelected",
|
|
21
|
+
"inputOnOpen",
|
|
22
|
+
"errorMessages"
|
|
23
|
+
];
|
|
24
|
+
function s(o) {
|
|
25
|
+
let [s, c] = n(a(() => o().defaultValue ?? null)), l = () => "value" in o(), d = () => l() ? o().value ?? null : s(), f = e({
|
|
26
|
+
props: a(() => u(o(), d())),
|
|
27
|
+
onChange: (e) => {
|
|
28
|
+
l() || c(() => e), o().onChange?.(e);
|
|
29
|
+
},
|
|
30
|
+
runtime: { nextTick: () => Promise.resolve() }
|
|
31
|
+
}), [p, m] = n(f.getSnapshot(), { equals: !1 });
|
|
32
|
+
return t(() => u(o(), d()), (e) => f.setProps(e)), r(f.subscribe(() => m(f.getSnapshot()))), i(() => (f.mount(), () => f.destroy())), p;
|
|
33
|
+
}
|
|
34
|
+
var c = {
|
|
35
|
+
onKeydown: "onKeyDown",
|
|
36
|
+
onMousedown: "onMouseDown",
|
|
37
|
+
onMousemove: "onMouseMove"
|
|
38
|
+
};
|
|
39
|
+
function l(e) {
|
|
40
|
+
let t = {};
|
|
41
|
+
for (let [n, r] of Object.entries(e)) {
|
|
42
|
+
let e = c[n] ?? n;
|
|
43
|
+
t[e] = e.startsWith("aria-") && typeof r == "boolean" ? String(r) : r;
|
|
44
|
+
}
|
|
45
|
+
return t;
|
|
46
|
+
}
|
|
47
|
+
function u(e, t) {
|
|
48
|
+
let n = {};
|
|
49
|
+
for (let t of o) n[t] = e[t];
|
|
50
|
+
return n.modelValue = t, n;
|
|
51
|
+
}
|
|
52
|
+
// src/HeadlessCombobox.tsx
|
|
53
|
+
function d(e) {
|
|
54
|
+
let t = s(() => e);
|
|
55
|
+
return e.children(t);
|
|
56
|
+
}
|
|
57
|
+
export { d as HeadlessCombobox, l as toSolidProps, s as useHeadlessCombobox };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["CORE_PROP_KEYS","useHeadlessCombobox","uncontrolled","setUncontrolled","createSignal","untrack","options","controlled","modelValue","store","createCombobox","toCoreProps","value","snapshot","setSnapshot","createEffect","next","onCleanup","onSettled","SOLID_EVENT_NAMES","toSolidProps","adapted","key","props","name","resolved","HeadlessCombobox","scope"],"sources":["../src/useHeadlessCombobox.ts","../src/HeadlessCombobox.tsx"],"sourcesContent":["import type {\n HeadlessComboboxProps as CoreComboboxProps,\n HeadlessComboboxSnapshot,\n} from '@pdanpdan/headless-combobox-core';\nimport type { Accessor, Setter } from 'solid-js';\n\nimport { createCombobox } from '@pdanpdan/headless-combobox-core';\nimport { createEffect, createSignal, onCleanup, onSettled, untrack } from 'solid-js';\n\n/** Options accepted by the Solid hook and component. */\nexport type UseHeadlessComboboxOptions<O, V = O, Q = string>\n = Omit<CoreComboboxProps<O, V, Q>, 'modelValue'> & {\n /** Controlled value (`value` present) or omit for uncontrolled. */\n value?: V | V[] | null;\n /** Initial value for uncontrolled usage. */\n defaultValue?: V | V[] | null;\n /** Called for every selection change. */\n onChange?: (value: V | V[] | null) => void;\n };\n\nconst CORE_PROP_KEYS = [\n 'options',\n 'multiple',\n 'minLength',\n 'maxLength',\n 'required',\n 'disabled',\n 'readonly',\n 'closeOnSelect',\n 'closeOnClickOutside',\n 'clickOutsideFilter',\n 'selectOnTab',\n 'optionValue',\n 'optionLabel',\n 'optionFilter',\n 'id',\n 'alignSelected',\n 'inputOnOpen',\n 'errorMessages',\n] as const;\n\n/**\n * Solid adapter for the framework-independent combobox core.\n *\n * Returns an accessor for the snapshot, so `<button {...toSolidProps(scope().triggerProps)} />`\n * stays reactive. The options accessor is tracked by a `createEffect`.\n */\nexport function useHeadlessCombobox<O, V = O, Q = string>(\n options: Accessor<UseHeadlessComboboxOptions<O, V, Q>>,\n): Accessor<HeadlessComboboxSnapshot<O, V, Q>> {\n // Solid 2's createSignal overloads cannot express a generic `V | V[] | null`\n // initial value; the runtime value is the same.\n const [ uncontrolled, setUncontrolled ] = createSignal<unknown>(\n untrack(() => options().defaultValue ?? null),\n ) as unknown as [Accessor<V | V[] | null>, Setter<V | V[] | null>];\n const controlled = () => 'value' in options();\n const modelValue = () => (controlled()\n ? (options().value ?? null)\n : uncontrolled());\n\n const store = createCombobox<O, V, Q>({\n props: untrack(() => toCoreProps(options(), modelValue())),\n onChange: (value) => {\n if (!controlled()) {\n setUncontrolled(() => value);\n }\n options().onChange?.(value);\n },\n runtime: {\n nextTick: () => Promise.resolve(),\n },\n });\n\n const [ snapshot, setSnapshot ] = createSignal(store.getSnapshot(), { equals: false });\n\n createEffect(\n () => toCoreProps(options(), modelValue()),\n (next) => store.setProps(next),\n );\n\n onCleanup(store.subscribe(() => setSnapshot(store.getSnapshot())));\n onSettled(() => {\n store.mount();\n return () => store.destroy();\n });\n\n return snapshot;\n}\n\nconst SOLID_EVENT_NAMES: Record<string, string> = {\n onKeydown: 'onKeyDown',\n onMousedown: 'onMouseDown',\n onMousemove: 'onMouseMove',\n};\n\n/**\n * Adapt a core ARIA prop bag to Solid's JSX event prop names:\n *\n * ```tsx\n * <button {...toSolidProps(scope().triggerProps)} />\n * ```\n */\nexport function toSolidProps<T extends object>(props: T): Record<string, unknown> {\n const adapted: Record<string, unknown> = {};\n for (const [ key, value ] of Object.entries(props)) {\n const name = SOLID_EVENT_NAMES[ key ] ?? key;\n // Solid 2 removes attributes set to false, including ARIA state.\n adapted[ name ] = name.startsWith('aria-') && typeof value === 'boolean'\n ? String(value)\n : value;\n }\n return adapted;\n}\n\nfunction toCoreProps<O, V, Q>(\n options: UseHeadlessComboboxOptions<O, V, Q>,\n modelValue: V | V[] | null,\n): CoreComboboxProps<O, V, Q> {\n const resolved: Record<string, unknown> = {};\n for (const key of CORE_PROP_KEYS) {\n resolved[ key ] = options[ key ];\n }\n resolved.modelValue = modelValue;\n return resolved as unknown as CoreComboboxProps<O, V, Q>;\n}\n","import type { UseHeadlessComboboxOptions } from './useHeadlessCombobox';\nimport type { HeadlessComboboxSnapshot } from '@pdanpdan/headless-combobox-core';\nimport type { JSX } from '@solidjs/web';\nimport type { Accessor } from 'solid-js';\n\nimport { useHeadlessCombobox } from './useHeadlessCombobox';\n\n/** Props for the render-prop component: hook options plus a scope renderer. */\nexport interface HeadlessComboboxProps<O, V = O, Q = string>\n extends UseHeadlessComboboxOptions<O, V, Q> {\n /** The scope accessor keeps every binding reactive (Solid components run once). */\n children: (scope: Accessor<HeadlessComboboxSnapshot<O, V, Q>>) => JSX.Element;\n}\n\n/**\n * Render-prop wrapper around {@link useHeadlessCombobox}. Prefer the hook when\n * the markup does not need to be nested under a component.\n */\nexport function HeadlessCombobox<O, V = O, Q = string>(\n props: HeadlessComboboxProps<O, V, Q>,\n): JSX.Element {\n const scope = useHeadlessCombobox<O, V, Q>(() => props);\n return props.children(scope);\n}\n"],"mappings":";;;AAoBA,IAAMA,IAAiB;CACrB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAQA,SAAgBC,EACd,GAC6C;CAG7C,IAAM,CAAEC,GAAcC,KAAoBC,EACxCC,QAAcC,EAAQ,CAAC,CAAC,gBAAgB,IAAI,CAC9C,GACMC,UAAmB,WAAWD,EAAQ,GACtCE,UAAoBD,EAAW,IAChCD,EAAQ,CAAC,CAAC,SAAS,OACpBJ,EAAa,GAEXO,IAAQC,EAAwB;EACpC,OAAOL,QAAcM,EAAYL,EAAQ,GAAGE,EAAW,CAAC,CAAC;EACzD,WAAW,MAAU;GAInB,AAHKD,EAAW,KACdJ,QAAsBS,CAAK,GAE7BN,EAAQ,CAAC,CAAC,WAAWM,CAAK;EAC5B;EACA,SAAS,EACP,gBAAgB,QAAQ,QAAQ,EAClC;CACF,CAAC,GAEK,CAAEC,GAAUC,KAAgBV,EAAaK,EAAM,YAAY,GAAG,EAAE,QAAQ,GAAM,CAAC;CAarF,OAXAM,QACQJ,EAAYL,EAAQ,GAAGE,EAAW,CAAC,IACxC,MAASC,EAAM,SAASO,CAAI,CAC/B,GAEAC,EAAUR,EAAM,gBAAgBK,EAAYL,EAAM,YAAY,CAAC,CAAC,CAAC,GACjES,SACET,EAAM,MAAM,SACCA,EAAM,QAAQ,EAC5B,GAEMI;AACT;AAEA,IAAMM,IAA4C;CAChD,WAAW;CACX,aAAa;CACb,aAAa;AACf;AASA,SAAgBC,EAA+B,GAAmC;CAChF,IAAMC,IAAmC,CAAC;CAC1C,KAAK,IAAM,CAAEC,GAAKV,MAAW,OAAO,QAAQW,CAAK,GAAG;EAClD,IAAMC,IAAOL,EAAmBG,MAASA;EAEzC,EAASE,KAASA,EAAK,WAAW,OAAO,KAAK,OAAOZ,KAAU,YAC3D,OAAOA,CAAK,IACZA;CACN;CACA,OAAOS;AACT;AAEA,SAASV,EACP,GACA,GAC4B;CAC5B,IAAMc,IAAoC,CAAC;CAC3C,KAAK,IAAMH,KAAOtB,GAChB,EAAUsB,KAAQhB,EAASgB;CAG7B,OADA,EAAS,aAAad,GACfiB;AACT;;AC1GA,SAAgBC,EACd,GACa;CACb,IAAMC,IAAQ1B,QAAmCsB,CAAK;CACtD,OAAOA,EAAM,SAASI,CAAK;AAC7B"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { HeadlessComboboxProps as CoreComboboxProps, HeadlessComboboxSnapshot } from '../../headless-combobox-core/src/index.ts';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
/** Options accepted by the Solid hook and component. */
|
|
4
|
+
export type UseHeadlessComboboxOptions<O, V = O, Q = string> = Omit<CoreComboboxProps<O, V, Q>, 'modelValue'> & {
|
|
5
|
+
/** Controlled value (`value` present) or omit for uncontrolled. */
|
|
6
|
+
value?: V | V[] | null;
|
|
7
|
+
/** Initial value for uncontrolled usage. */
|
|
8
|
+
defaultValue?: V | V[] | null;
|
|
9
|
+
/** Called for every selection change. */
|
|
10
|
+
onChange?: (value: V | V[] | null) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Solid adapter for the framework-independent combobox core.
|
|
14
|
+
*
|
|
15
|
+
* Returns an accessor for the snapshot, so `<button {...toSolidProps(scope().triggerProps)} />`
|
|
16
|
+
* stays reactive. The options accessor is tracked by a `createEffect`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useHeadlessCombobox<O, V = O, Q = string>(options: Accessor<UseHeadlessComboboxOptions<O, V, Q>>): Accessor<HeadlessComboboxSnapshot<O, V, Q>>;
|
|
19
|
+
/**
|
|
20
|
+
* Adapt a core ARIA prop bag to Solid's JSX event prop names:
|
|
21
|
+
*
|
|
22
|
+
* ```tsx
|
|
23
|
+
* <button {...toSolidProps(scope().triggerProps)} />
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function toSolidProps<T extends object>(props: T): Record<string, unknown>;
|
|
27
|
+
//# sourceMappingURL=useHeadlessCombobox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHeadlessCombobox.d.ts","sourceRoot":"","sources":["../src/useHeadlessCombobox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,IAAI,iBAAiB,EAC1C,wBAAwB,EACzB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAE,QAAQ,EAAU,MAAM,UAAU,CAAC;AAKjD,wDAAwD;AACxD,MAAM,MAAM,0BAA0B,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,IACvD,IAAI,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG;IACjD,mEAAmE;IACnE,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC;IACvB,4CAA4C;IAC5C,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC;IAC9B,yCAAyC;IACzC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,KAAK,IAAI,CAAC;CAC5C,CAAC;AAuBJ;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EACtD,OAAO,EAAE,QAAQ,CAAC,0BAA0B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GACrD,QAAQ,CAAC,wBAAwB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAsC7C;AAQD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAUhF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,81 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pdanpdan/headless-combobox-solid",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "3.0.0",
|
|
5
|
+
"description": "Accessible renderless combobox for Solid, built on the framework-independent core",
|
|
6
|
+
"author": "Dan Popescu",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://pdanpdan.github.io/headless-components",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/pdanpdan/headless-components.git",
|
|
12
|
+
"directory": "packages/headless-combobox-solid"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/pdanpdan/headless-components/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"solid",
|
|
19
|
+
"solidjs",
|
|
20
|
+
"combobox",
|
|
21
|
+
"headless",
|
|
22
|
+
"renderless",
|
|
23
|
+
"select",
|
|
24
|
+
"autocomplete",
|
|
25
|
+
"accessibility",
|
|
26
|
+
"typescript"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./source": {
|
|
36
|
+
"types": "./src/index.ts",
|
|
37
|
+
"import": "./src/index.ts"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"main": "./dist/index.cjs",
|
|
41
|
+
"module": "./dist/index.mjs",
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"src"
|
|
46
|
+
],
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@solidjs/web": ">=2.0.0",
|
|
49
|
+
"solid-js": ">=2.0.0"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@pdanpdan/headless-combobox-core": "3.0.0"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@babel/core": "^7.29.0",
|
|
60
|
+
"@solidjs/babel-plugin": "^2.0.2",
|
|
61
|
+
"@solidjs/vite-plugin": "^3.0.1",
|
|
62
|
+
"@solidjs/web": "^2.0.3",
|
|
63
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
64
|
+
"jsdom": "^31.0.1",
|
|
65
|
+
"solid-js": "^2.0.3",
|
|
66
|
+
"typescript": "^6.0.3",
|
|
67
|
+
"vite": "^8.3.8",
|
|
68
|
+
"vite-plugin-dts": "^5.1.2",
|
|
69
|
+
"vitest": "^5.1.1"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "vite build",
|
|
73
|
+
"lint": "pnpm --dir ../.. lint",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest",
|
|
76
|
+
"test:coverage": "vitest run --coverage",
|
|
77
|
+
"typecheck": "tsc -p tsconfig.app.json --noEmit && tsc -p tsconfig.vitest.json --noEmit",
|
|
78
|
+
"test:all": "pnpm lint && pnpm typecheck && pnpm test",
|
|
79
|
+
"release:npm": "pnpm publish"
|
|
80
|
+
}
|
|
6
81
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { UseHeadlessComboboxOptions } from './useHeadlessCombobox';
|
|
2
|
+
import type { HeadlessComboboxSnapshot } from '@pdanpdan/headless-combobox-core';
|
|
3
|
+
import type { JSX } from '@solidjs/web';
|
|
4
|
+
import type { Accessor } from 'solid-js';
|
|
5
|
+
|
|
6
|
+
import { useHeadlessCombobox } from './useHeadlessCombobox';
|
|
7
|
+
|
|
8
|
+
/** Props for the render-prop component: hook options plus a scope renderer. */
|
|
9
|
+
export interface HeadlessComboboxProps<O, V = O, Q = string>
|
|
10
|
+
extends UseHeadlessComboboxOptions<O, V, Q> {
|
|
11
|
+
/** The scope accessor keeps every binding reactive (Solid components run once). */
|
|
12
|
+
children: (scope: Accessor<HeadlessComboboxSnapshot<O, V, Q>>) => JSX.Element;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Render-prop wrapper around {@link useHeadlessCombobox}. Prefer the hook when
|
|
17
|
+
* the markup does not need to be nested under a component.
|
|
18
|
+
*/
|
|
19
|
+
export function HeadlessCombobox<O, V = O, Q = string>(
|
|
20
|
+
props: HeadlessComboboxProps<O, V, Q>,
|
|
21
|
+
): JSX.Element {
|
|
22
|
+
const scope = useHeadlessCombobox<O, V, Q>(() => props);
|
|
23
|
+
return props.children(scope);
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
HeadlessComboboxComboboxInputProps,
|
|
3
|
+
HeadlessComboboxErrorCode,
|
|
4
|
+
HeadlessComboboxInputProps,
|
|
5
|
+
HeadlessComboboxListboxProps,
|
|
6
|
+
HeadlessComboboxOptionProps,
|
|
7
|
+
HeadlessComboboxPopupStyle,
|
|
8
|
+
HeadlessComboboxSnapshot,
|
|
9
|
+
HeadlessComboboxTriggerProps,
|
|
10
|
+
} from '@pdanpdan/headless-combobox-core';
|
|
11
|
+
export type { UseHeadlessComboboxOptions } from './useHeadlessCombobox';
|
|
12
|
+
export type { HeadlessComboboxProps } from './HeadlessCombobox';
|
|
13
|
+
|
|
14
|
+
export { HeadlessCombobox } from './HeadlessCombobox';
|
|
15
|
+
export { toSolidProps, useHeadlessCombobox } from './useHeadlessCombobox';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
HeadlessComboboxProps as CoreComboboxProps,
|
|
3
|
+
HeadlessComboboxSnapshot,
|
|
4
|
+
} from '@pdanpdan/headless-combobox-core';
|
|
5
|
+
import type { Accessor, Setter } from 'solid-js';
|
|
6
|
+
|
|
7
|
+
import { createCombobox } from '@pdanpdan/headless-combobox-core';
|
|
8
|
+
import { createEffect, createSignal, onCleanup, onSettled, untrack } from 'solid-js';
|
|
9
|
+
|
|
10
|
+
/** Options accepted by the Solid hook and component. */
|
|
11
|
+
export type UseHeadlessComboboxOptions<O, V = O, Q = string>
|
|
12
|
+
= Omit<CoreComboboxProps<O, V, Q>, 'modelValue'> & {
|
|
13
|
+
/** Controlled value (`value` present) or omit for uncontrolled. */
|
|
14
|
+
value?: V | V[] | null;
|
|
15
|
+
/** Initial value for uncontrolled usage. */
|
|
16
|
+
defaultValue?: V | V[] | null;
|
|
17
|
+
/** Called for every selection change. */
|
|
18
|
+
onChange?: (value: V | V[] | null) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const CORE_PROP_KEYS = [
|
|
22
|
+
'options',
|
|
23
|
+
'multiple',
|
|
24
|
+
'minLength',
|
|
25
|
+
'maxLength',
|
|
26
|
+
'required',
|
|
27
|
+
'disabled',
|
|
28
|
+
'readonly',
|
|
29
|
+
'closeOnSelect',
|
|
30
|
+
'closeOnClickOutside',
|
|
31
|
+
'clickOutsideFilter',
|
|
32
|
+
'selectOnTab',
|
|
33
|
+
'optionValue',
|
|
34
|
+
'optionLabel',
|
|
35
|
+
'optionFilter',
|
|
36
|
+
'id',
|
|
37
|
+
'alignSelected',
|
|
38
|
+
'inputOnOpen',
|
|
39
|
+
'errorMessages',
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Solid adapter for the framework-independent combobox core.
|
|
44
|
+
*
|
|
45
|
+
* Returns an accessor for the snapshot, so `<button {...toSolidProps(scope().triggerProps)} />`
|
|
46
|
+
* stays reactive. The options accessor is tracked by a `createEffect`.
|
|
47
|
+
*/
|
|
48
|
+
export function useHeadlessCombobox<O, V = O, Q = string>(
|
|
49
|
+
options: Accessor<UseHeadlessComboboxOptions<O, V, Q>>,
|
|
50
|
+
): Accessor<HeadlessComboboxSnapshot<O, V, Q>> {
|
|
51
|
+
// Solid 2's createSignal overloads cannot express a generic `V | V[] | null`
|
|
52
|
+
// initial value; the runtime value is the same.
|
|
53
|
+
const [ uncontrolled, setUncontrolled ] = createSignal<unknown>(
|
|
54
|
+
untrack(() => options().defaultValue ?? null),
|
|
55
|
+
) as unknown as [Accessor<V | V[] | null>, Setter<V | V[] | null>];
|
|
56
|
+
const controlled = () => 'value' in options();
|
|
57
|
+
const modelValue = () => (controlled()
|
|
58
|
+
? (options().value ?? null)
|
|
59
|
+
: uncontrolled());
|
|
60
|
+
|
|
61
|
+
const store = createCombobox<O, V, Q>({
|
|
62
|
+
props: untrack(() => toCoreProps(options(), modelValue())),
|
|
63
|
+
onChange: (value) => {
|
|
64
|
+
if (!controlled()) {
|
|
65
|
+
setUncontrolled(() => value);
|
|
66
|
+
}
|
|
67
|
+
options().onChange?.(value);
|
|
68
|
+
},
|
|
69
|
+
runtime: {
|
|
70
|
+
nextTick: () => Promise.resolve(),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const [ snapshot, setSnapshot ] = createSignal(store.getSnapshot(), { equals: false });
|
|
75
|
+
|
|
76
|
+
createEffect(
|
|
77
|
+
() => toCoreProps(options(), modelValue()),
|
|
78
|
+
(next) => store.setProps(next),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
onCleanup(store.subscribe(() => setSnapshot(store.getSnapshot())));
|
|
82
|
+
onSettled(() => {
|
|
83
|
+
store.mount();
|
|
84
|
+
return () => store.destroy();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return snapshot;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const SOLID_EVENT_NAMES: Record<string, string> = {
|
|
91
|
+
onKeydown: 'onKeyDown',
|
|
92
|
+
onMousedown: 'onMouseDown',
|
|
93
|
+
onMousemove: 'onMouseMove',
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Adapt a core ARIA prop bag to Solid's JSX event prop names:
|
|
98
|
+
*
|
|
99
|
+
* ```tsx
|
|
100
|
+
* <button {...toSolidProps(scope().triggerProps)} />
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function toSolidProps<T extends object>(props: T): Record<string, unknown> {
|
|
104
|
+
const adapted: Record<string, unknown> = {};
|
|
105
|
+
for (const [ key, value ] of Object.entries(props)) {
|
|
106
|
+
const name = SOLID_EVENT_NAMES[ key ] ?? key;
|
|
107
|
+
// Solid 2 removes attributes set to false, including ARIA state.
|
|
108
|
+
adapted[ name ] = name.startsWith('aria-') && typeof value === 'boolean'
|
|
109
|
+
? String(value)
|
|
110
|
+
: value;
|
|
111
|
+
}
|
|
112
|
+
return adapted;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function toCoreProps<O, V, Q>(
|
|
116
|
+
options: UseHeadlessComboboxOptions<O, V, Q>,
|
|
117
|
+
modelValue: V | V[] | null,
|
|
118
|
+
): CoreComboboxProps<O, V, Q> {
|
|
119
|
+
const resolved: Record<string, unknown> = {};
|
|
120
|
+
for (const key of CORE_PROP_KEYS) {
|
|
121
|
+
resolved[ key ] = options[ key ];
|
|
122
|
+
}
|
|
123
|
+
resolved.modelValue = modelValue;
|
|
124
|
+
return resolved as unknown as CoreComboboxProps<O, V, Q>;
|
|
125
|
+
}
|