@tsproxy/react 0.0.1 → 0.0.3
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/README.md +68 -0
- package/dist/index.d.ts +27 -4
- package/dist/index.js +147 -18
- package/package.json +11 -13
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @tsproxy/react
|
|
2
|
+
|
|
3
|
+
Headless React components for [tsproxy](https://github.com/akshitkrnagpal/tsproxy) with a BaseUI-style overrides pattern.
|
|
4
|
+
|
|
5
|
+
> **This project is under heavy development.**
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @tsproxy/react @tsproxy/js react-instantsearch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { SearchProvider, SearchBox, Hits, RefinementList, Pagination, Stats } from "@tsproxy/react";
|
|
17
|
+
import { Configure } from "react-instantsearch";
|
|
18
|
+
|
|
19
|
+
export default function SearchPage() {
|
|
20
|
+
return (
|
|
21
|
+
<SearchProvider serverUrl="http://localhost:3000" indexName="products">
|
|
22
|
+
<Configure hitsPerPage={12} />
|
|
23
|
+
<SearchBox placeholder="Search..." />
|
|
24
|
+
<Stats />
|
|
25
|
+
<RefinementList attribute="category" />
|
|
26
|
+
<Hits hitComponent={({ hit }) => <div>{hit.name}</div>} />
|
|
27
|
+
<Pagination />
|
|
28
|
+
</SearchProvider>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Overrides
|
|
34
|
+
|
|
35
|
+
Every component accepts an `overrides` prop to customize any sub-element:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
<SearchBox
|
|
39
|
+
overrides={{
|
|
40
|
+
Input: { props: { className: "rounded-full border px-4 py-2" } },
|
|
41
|
+
SubmitButton: { props: { hidden: true } },
|
|
42
|
+
ResetButton: { component: MyResetButton },
|
|
43
|
+
}}
|
|
44
|
+
/>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Components
|
|
48
|
+
|
|
49
|
+
| Component | Sub-elements |
|
|
50
|
+
|-----------|-------------|
|
|
51
|
+
| `SearchBox` | Root, Form, Input, SubmitButton, ResetButton |
|
|
52
|
+
| `Hits` | Root, List, Item |
|
|
53
|
+
| `RefinementList` | Root, List, Item, Label, Checkbox, LabelText, Count |
|
|
54
|
+
| `Pagination` | Root, List, Item, Link |
|
|
55
|
+
| `Stats` | Root, Text |
|
|
56
|
+
| `SortBy` | Root, Select, Option |
|
|
57
|
+
| `NoResults` | Root, Title, Message |
|
|
58
|
+
| `HitsSkeleton` | Root, List, Item |
|
|
59
|
+
| `LocaleSelector` | Root, Select, Option |
|
|
60
|
+
| `SearchProvider` | — (wraps InstantSearch) |
|
|
61
|
+
|
|
62
|
+
## Documentation
|
|
63
|
+
|
|
64
|
+
[tsproxy.akshit.io](https://tsproxy.akshit.io)
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ interface LocaleContextValue {
|
|
|
40
40
|
locale: string | undefined;
|
|
41
41
|
setLocale: (locale: string) => void;
|
|
42
42
|
}
|
|
43
|
-
declare const LocaleContext: react.Context<LocaleContextValue
|
|
43
|
+
declare const LocaleContext: react.Context<LocaleContextValue>;
|
|
44
44
|
declare function useLocale(): LocaleContextValue;
|
|
45
45
|
interface LocaleProviderProps {
|
|
46
46
|
initialLocale?: string;
|
|
@@ -108,7 +108,7 @@ interface PaginationProps {
|
|
|
108
108
|
padding?: number;
|
|
109
109
|
overrides?: Overrides<PaginationElements>;
|
|
110
110
|
}
|
|
111
|
-
declare function Pagination({ padding, overrides }: PaginationProps): react_jsx_runtime.JSX.Element
|
|
111
|
+
declare function Pagination({ padding, overrides }: PaginationProps): react_jsx_runtime.JSX.Element;
|
|
112
112
|
|
|
113
113
|
type StatsElements = {
|
|
114
114
|
Root: "div";
|
|
@@ -161,7 +161,7 @@ interface NoResultsProps {
|
|
|
161
161
|
children?: ReactNode;
|
|
162
162
|
overrides?: Overrides<NoResultsElements>;
|
|
163
163
|
}
|
|
164
|
-
declare function NoResults({ title, message, children, overrides, }: NoResultsProps): react_jsx_runtime.JSX.Element
|
|
164
|
+
declare function NoResults({ title, message, children, overrides, }: NoResultsProps): react_jsx_runtime.JSX.Element;
|
|
165
165
|
|
|
166
166
|
type HitsSkeletonElements = {
|
|
167
167
|
Root: "div";
|
|
@@ -174,4 +174,27 @@ interface HitsSkeletonProps {
|
|
|
174
174
|
}
|
|
175
175
|
declare function HitsSkeleton({ count, overrides }: HitsSkeletonProps): react_jsx_runtime.JSX.Element;
|
|
176
176
|
|
|
177
|
-
|
|
177
|
+
type AutocompleteElements = {
|
|
178
|
+
Root: "div";
|
|
179
|
+
Input: "input";
|
|
180
|
+
List: "ul";
|
|
181
|
+
Item: "li";
|
|
182
|
+
Highlight: "span";
|
|
183
|
+
};
|
|
184
|
+
interface Suggestion {
|
|
185
|
+
objectID: string;
|
|
186
|
+
query: string;
|
|
187
|
+
highlight: string;
|
|
188
|
+
}
|
|
189
|
+
interface AutocompleteProps {
|
|
190
|
+
serverUrl: string;
|
|
191
|
+
collection?: string;
|
|
192
|
+
limit?: number;
|
|
193
|
+
placeholder?: string;
|
|
194
|
+
debounce?: number;
|
|
195
|
+
onSelect?: (suggestion: Suggestion) => void;
|
|
196
|
+
overrides?: Overrides<AutocompleteElements>;
|
|
197
|
+
}
|
|
198
|
+
declare function Autocomplete({ serverUrl, collection, limit, placeholder, debounce, onSelect, overrides, }: AutocompleteProps): react_jsx_runtime.JSX.Element;
|
|
199
|
+
|
|
200
|
+
export { Autocomplete, type AutocompleteElements, type AutocompleteProps, Hits, type HitsElements, type HitsProps, HitsSkeleton, type HitsSkeletonElements, type HitsSkeletonProps, LocaleContext, type LocaleOption, LocaleProvider, LocaleSelector, type LocaleSelectorElements, type LocaleSelectorProps, NoResults, type NoResultsElements, type NoResultsProps, type Override, type Overrides, Pagination, type PaginationElements, type PaginationProps, RefinementList, type RefinementListElements, type RefinementListProps, SearchBox, type SearchBoxElements, type SearchBoxProps, SearchProvider, SortBy, type SortByElements, type SortByItem, type SortByProps, Stats, type StatsElements, type StatsProps, type Suggestion, getOverride, useLocale };
|
package/dist/index.js
CHANGED
|
@@ -40,12 +40,12 @@ import { useMemo as useMemo2 } from "react";
|
|
|
40
40
|
import { InstantSearch } from "react-instantsearch";
|
|
41
41
|
import { createSearchClient } from "@tsproxy/js";
|
|
42
42
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
43
|
-
function
|
|
43
|
+
function SearchProvider({
|
|
44
44
|
serverUrl,
|
|
45
45
|
indexName,
|
|
46
|
+
locale,
|
|
46
47
|
children
|
|
47
48
|
}) {
|
|
48
|
-
const { locale } = useLocale();
|
|
49
49
|
const searchClient = useMemo2(
|
|
50
50
|
() => createSearchClient({
|
|
51
51
|
url: serverUrl,
|
|
@@ -54,22 +54,7 @@ function SearchProviderInner({
|
|
|
54
54
|
}),
|
|
55
55
|
[serverUrl, locale]
|
|
56
56
|
);
|
|
57
|
-
return /* @__PURE__ */ jsx2(
|
|
58
|
-
InstantSearch,
|
|
59
|
-
{
|
|
60
|
-
searchClient,
|
|
61
|
-
indexName,
|
|
62
|
-
children
|
|
63
|
-
}
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
function SearchProvider({
|
|
67
|
-
serverUrl,
|
|
68
|
-
indexName,
|
|
69
|
-
locale,
|
|
70
|
-
children
|
|
71
|
-
}) {
|
|
72
|
-
return /* @__PURE__ */ jsx2(LocaleProvider, { initialLocale: locale, children: /* @__PURE__ */ jsx2(SearchProviderInner, { serverUrl, indexName, children }) });
|
|
57
|
+
return /* @__PURE__ */ jsx2(InstantSearch, { searchClient, indexName, children });
|
|
73
58
|
}
|
|
74
59
|
|
|
75
60
|
// src/components/SearchBox.tsx
|
|
@@ -391,7 +376,151 @@ function HitsSkeleton({ count = 8, overrides }) {
|
|
|
391
376
|
] })
|
|
392
377
|
] }) }, i)) }) });
|
|
393
378
|
}
|
|
379
|
+
|
|
380
|
+
// src/components/Autocomplete.tsx
|
|
381
|
+
import { useState as useState3, useRef as useRef2, useEffect as useEffect2, useCallback as useCallback2 } from "react";
|
|
382
|
+
import { jsx as jsx12, jsxs as jsxs6 } from "react/jsx-runtime";
|
|
383
|
+
function Autocomplete({
|
|
384
|
+
serverUrl,
|
|
385
|
+
collection = "products",
|
|
386
|
+
limit = 5,
|
|
387
|
+
placeholder = "Search...",
|
|
388
|
+
debounce = 200,
|
|
389
|
+
onSelect,
|
|
390
|
+
overrides
|
|
391
|
+
}) {
|
|
392
|
+
const [query, setQuery] = useState3("");
|
|
393
|
+
const [suggestions, setSuggestions] = useState3([]);
|
|
394
|
+
const [open, setOpen] = useState3(false);
|
|
395
|
+
const [activeIndex, setActiveIndex] = useState3(-1);
|
|
396
|
+
const timerRef = useRef2(void 0);
|
|
397
|
+
const inputRef = useRef2(null);
|
|
398
|
+
const root = getOverride("div", overrides?.Root);
|
|
399
|
+
const input = getOverride("input", overrides?.Input);
|
|
400
|
+
const list = getOverride("ul", overrides?.List);
|
|
401
|
+
const item = getOverride("li", overrides?.Item);
|
|
402
|
+
const highlight = getOverride("span", overrides?.Highlight);
|
|
403
|
+
const fetchSuggestions = useCallback2(
|
|
404
|
+
async (q) => {
|
|
405
|
+
if (!q.trim()) {
|
|
406
|
+
setSuggestions([]);
|
|
407
|
+
setOpen(false);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const res = await fetch(
|
|
412
|
+
`${serverUrl}/api/suggestions?q=${encodeURIComponent(q)}&collection=${collection}&limit=${limit}`
|
|
413
|
+
);
|
|
414
|
+
const data = await res.json();
|
|
415
|
+
setSuggestions(data.suggestions || []);
|
|
416
|
+
setOpen((data.suggestions || []).length > 0);
|
|
417
|
+
setActiveIndex(-1);
|
|
418
|
+
} catch {
|
|
419
|
+
setSuggestions([]);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
[serverUrl, collection, limit]
|
|
423
|
+
);
|
|
424
|
+
const handleChange = (e) => {
|
|
425
|
+
const value = e.target.value;
|
|
426
|
+
setQuery(value);
|
|
427
|
+
clearTimeout(timerRef.current);
|
|
428
|
+
timerRef.current = setTimeout(() => fetchSuggestions(value), debounce);
|
|
429
|
+
};
|
|
430
|
+
const handleSelect = (suggestion) => {
|
|
431
|
+
setQuery(suggestion.query);
|
|
432
|
+
setOpen(false);
|
|
433
|
+
onSelect?.(suggestion);
|
|
434
|
+
};
|
|
435
|
+
const handleKeyDown = (e) => {
|
|
436
|
+
if (!open) return;
|
|
437
|
+
if (e.key === "ArrowDown") {
|
|
438
|
+
e.preventDefault();
|
|
439
|
+
setActiveIndex((i) => Math.min(i + 1, suggestions.length - 1));
|
|
440
|
+
} else if (e.key === "ArrowUp") {
|
|
441
|
+
e.preventDefault();
|
|
442
|
+
setActiveIndex((i) => Math.max(i - 1, 0));
|
|
443
|
+
} else if (e.key === "Enter" && activeIndex >= 0) {
|
|
444
|
+
e.preventDefault();
|
|
445
|
+
const s = suggestions[activeIndex];
|
|
446
|
+
if (s) handleSelect(s);
|
|
447
|
+
} else if (e.key === "Escape") {
|
|
448
|
+
setOpen(false);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
useEffect2(() => {
|
|
452
|
+
const handleClickOutside = () => setOpen(false);
|
|
453
|
+
document.addEventListener("click", handleClickOutside);
|
|
454
|
+
return () => document.removeEventListener("click", handleClickOutside);
|
|
455
|
+
}, []);
|
|
456
|
+
return /* @__PURE__ */ jsxs6(
|
|
457
|
+
root.Component,
|
|
458
|
+
{
|
|
459
|
+
...root.resolveProps({
|
|
460
|
+
style: { position: "relative" },
|
|
461
|
+
onClick: (e) => e.stopPropagation()
|
|
462
|
+
}),
|
|
463
|
+
children: [
|
|
464
|
+
/* @__PURE__ */ jsx12(
|
|
465
|
+
input.Component,
|
|
466
|
+
{
|
|
467
|
+
...input.resolveProps({
|
|
468
|
+
ref: inputRef,
|
|
469
|
+
type: "search",
|
|
470
|
+
value: query,
|
|
471
|
+
onChange: handleChange,
|
|
472
|
+
onKeyDown: handleKeyDown,
|
|
473
|
+
onFocus: () => suggestions.length > 0 && setOpen(true),
|
|
474
|
+
placeholder,
|
|
475
|
+
autoComplete: "off",
|
|
476
|
+
role: "combobox",
|
|
477
|
+
"aria-expanded": open,
|
|
478
|
+
"aria-autocomplete": "list"
|
|
479
|
+
})
|
|
480
|
+
}
|
|
481
|
+
),
|
|
482
|
+
open && suggestions.length > 0 && /* @__PURE__ */ jsx12(
|
|
483
|
+
list.Component,
|
|
484
|
+
{
|
|
485
|
+
...list.resolveProps({
|
|
486
|
+
role: "listbox",
|
|
487
|
+
style: {
|
|
488
|
+
position: "absolute",
|
|
489
|
+
top: "100%",
|
|
490
|
+
left: 0,
|
|
491
|
+
right: 0,
|
|
492
|
+
zIndex: 50
|
|
493
|
+
}
|
|
494
|
+
}),
|
|
495
|
+
children: suggestions.map((s, i) => /* @__PURE__ */ jsx12(
|
|
496
|
+
item.Component,
|
|
497
|
+
{
|
|
498
|
+
...item.resolveProps({
|
|
499
|
+
role: "option",
|
|
500
|
+
"aria-selected": i === activeIndex,
|
|
501
|
+
"data-active": i === activeIndex || void 0,
|
|
502
|
+
onClick: () => handleSelect(s),
|
|
503
|
+
onMouseEnter: () => setActiveIndex(i)
|
|
504
|
+
}),
|
|
505
|
+
children: /* @__PURE__ */ jsx12(
|
|
506
|
+
highlight.Component,
|
|
507
|
+
{
|
|
508
|
+
...highlight.resolveProps({
|
|
509
|
+
dangerouslySetInnerHTML: { __html: s.highlight }
|
|
510
|
+
})
|
|
511
|
+
}
|
|
512
|
+
)
|
|
513
|
+
},
|
|
514
|
+
s.objectID
|
|
515
|
+
))
|
|
516
|
+
}
|
|
517
|
+
)
|
|
518
|
+
]
|
|
519
|
+
}
|
|
520
|
+
);
|
|
521
|
+
}
|
|
394
522
|
export {
|
|
523
|
+
Autocomplete,
|
|
395
524
|
Hits,
|
|
396
525
|
HitsSkeleton,
|
|
397
526
|
LocaleContext,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsproxy/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Headless React components for tsproxy with BaseUI-style overrides",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
-
"dist"
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
16
17
|
],
|
|
17
18
|
"publishConfig": {
|
|
18
19
|
"access": "public"
|
|
@@ -30,8 +31,12 @@
|
|
|
30
31
|
"headless",
|
|
31
32
|
"search"
|
|
32
33
|
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
37
|
+
},
|
|
33
38
|
"dependencies": {
|
|
34
|
-
"@tsproxy/js": "0.0.
|
|
39
|
+
"@tsproxy/js": "^0.0.3"
|
|
35
40
|
},
|
|
36
41
|
"peerDependencies": {
|
|
37
42
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -39,16 +44,9 @@
|
|
|
39
44
|
"react-instantsearch": "^7.0.0"
|
|
40
45
|
},
|
|
41
46
|
"devDependencies": {
|
|
42
|
-
"@types/react": "^
|
|
43
|
-
"@types/react-dom": "^
|
|
44
|
-
"react": "^18.3.0",
|
|
45
|
-
"react-dom": "^18.3.0",
|
|
46
|
-
"react-instantsearch": "^7.29.0",
|
|
47
|
+
"@types/react": "^19",
|
|
48
|
+
"@types/react-dom": "^19",
|
|
47
49
|
"tsup": "^8.4.0",
|
|
48
50
|
"typescript": "^5.5.3"
|
|
49
|
-
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"build": "tsup src/index.ts --format esm --dts --external react --external react-dom --external react-instantsearch --external @tsproxy/js --tsconfig tsconfig.build.json",
|
|
52
|
-
"typecheck": "tsc --noEmit"
|
|
53
51
|
}
|
|
54
|
-
}
|
|
52
|
+
}
|