Skip to content

Commit

Permalink
v
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed May 11, 2024
1 parent 08ee565 commit 0df0a6e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-mugs-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"basehub": patch
---

expose onHitSelect
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"blue-files-thank",
"brown-mayflies-enjoy",
"calm-cobras-glow",
"chilled-mugs-appear",
"dull-paws-arrive",
"giant-insects-jump",
"long-roses-juggle",
Expand Down
6 changes: 6 additions & 0 deletions packages/basehub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# basehub

## 4.0.16-canary.12

### Patch Changes

- expose onHitSelect

## 4.0.16-canary.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basehub",
"description": "The first AI-native content hub.",
"author": "JB <[email protected]>",
"version": "4.0.16-canary.11",
"version": "4.0.16-canary.12",
"license": "MIT",
"repository": "basehub-ai/basehub",
"bugs": "https://github.com/basehub-ai/basehub/issues",
Expand Down
39 changes: 28 additions & 11 deletions packages/basehub/src/react/search/primitive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ export type SearchBoxContext<Document = Record<string, unknown>> =
| { type: "set"; value: number }
)
) => void;
onHitSelect?: (hit: Hit<Document>) => void;
};

const Context = React.createContext<SearchBoxContext | undefined>(undefined);
Expand All @@ -444,9 +445,11 @@ const Root = <
>({
children,
search,
onHitSelect,
}: {
children?: React.ReactNode;
search: ReturnType<typeof useSearch<Document>>;
onHitSelect?: (hit: Hit<Document>) => void;
}) => {
const id = React.useId();
const [selectedIndex, setSelectedIndex] = React.useState(0);
Expand Down Expand Up @@ -547,7 +550,13 @@ const Root = <
if (search.valid === false) return null;
return (
<Context.Provider
value={{ ...(search as any), id, selectedIndex, onIndexChange }}
value={{
...(search as any),
id,
selectedIndex,
onIndexChange,
onHitSelect,
}}
>
{children}
</Context.Provider>
Expand Down Expand Up @@ -575,8 +584,15 @@ const Input = React.forwardRef<
{ asChild, onChange, onKeyDown, disableSelectionPrefill, ...props },
ref
) => {
const { id, query, onQueryChange, onIndexChange, recentSearches, result } =
useContext();
const {
id,
query,
onQueryChange,
onIndexChange,
recentSearches,
result,
onHitSelect,
} = useContext();
const Comp = asChild ? Slot : "input";

const onQueryChangeRef = React.useRef(onQueryChange);
Expand Down Expand Up @@ -622,19 +638,19 @@ const Input = React.forwardRef<
);
if (selectedNode) {
const href = selectedNode.getAttribute("href");
if (href) {
const hit = [
...(result?.hits ?? []), // first search in results
...(recentSearches?.hits ?? []), // then in recent searches
].find((h) => h._key === selectedNode.dataset.basehubHitKey);
if (href && hit) {
if (e.metaKey) {
window.open(href, "_blank");
} else {
window.location.href = href;
}
onHitSelect?.(hit);
if (recentSearches) {
const hit = result?.hits.find(
(h) => h._key === selectedNode.dataset.basehubHitKey
);
if (hit) {
recentSearches.add(hit);
}
recentSearches.add(hit);
}
}
}
Expand Down Expand Up @@ -737,7 +753,7 @@ const HitItem = React.forwardRef<
"ref"
>
>(({ asChild, hit, onClick, onFocus, ...props }, ref) => {
const { id, recentSearches, onIndexChange } = useContext();
const { id, recentSearches, onIndexChange, onHitSelect } = useContext();
const Comp = asChild ? Slot : "a";

return (
Expand All @@ -749,6 +765,7 @@ const HitItem = React.forwardRef<
ref={ref}
onClick={(e) => {
onClick?.(e as React.MouseEvent<HTMLAnchorElement, MouseEvent>);
onHitSelect?.(hit);
if (recentSearches) {
recentSearches.add(hit);
}
Expand Down
7 changes: 7 additions & 0 deletions playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# playground

## 0.0.86-canary.12

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.86-canary.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "playground",
"private": true,
"version": "0.0.86-canary.11",
"version": "0.0.86-canary.12",
"scripts": {
"dev": "basehub dev & next dev",
"build": "next build",
Expand Down

0 comments on commit 0df0a6e

Please sign in to comment.