@searchcraft/react-sdk 0.1.0 → 0.1.1
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 +154 -1
- package/dist/index.d.ts +22 -18
- package/dist/index.js +414 -345
- package/dist/index.js.map +1 -1
- package/dist/style.css +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1 +1,154 @@
|
|
|
1
|
-
# Searchcraft React SDK
|
|
1
|
+
# Searchcraft's React SDK
|
|
2
|
+
|
|
3
|
+
This kit provides basic tools for building out a simple search interface using the Searchcraft API. Searching, tracking the request state, and displaying results are all available in this kit.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Install with Yarn:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
$ yarn add @searchcraft/react-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Install with NPM:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
$ npm install @searchcraft/react-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Wrap your app or target a specific interface and wrap them with the `SearchcraftProvider` to expose the functionalities of the SDK.
|
|
20
|
+
|
|
21
|
+
```jsx
|
|
22
|
+
|
|
23
|
+
import { useMemo } from 'react'
|
|
24
|
+
|
|
25
|
+
import { SearchcraftProvider, Searchcraft } from '@searchcraft/react-sdk'
|
|
26
|
+
|
|
27
|
+
// values are created via the Searchcraft Vektron Customer Portal
|
|
28
|
+
const searchcraftConfig = {
|
|
29
|
+
index: ['test_data'],
|
|
30
|
+
apiKey: '1234.909.jmk',
|
|
31
|
+
endpointURL: 'http://127.0.0.1:8000',
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const Main = () => {
|
|
35
|
+
// initialize and pass an instance of Searchcraft into the provider
|
|
36
|
+
const searchcraft = useMemo(() => new Searchcraft(searchcraftConfig),[]);
|
|
37
|
+
return (
|
|
38
|
+
<SearchcraftProvider {...{ searchcraft }}>
|
|
39
|
+
<YourApplicationOrScreenGoHere />
|
|
40
|
+
</SearchcraftProvider>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
### Quickstart with useSearchcraft hook
|
|
46
|
+
|
|
47
|
+
Once your app is wrapped in the `SearchcraftProvider`, the `useSearchcraft` hook functionalities are available for use.
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
import {
|
|
51
|
+
useSearchcraft
|
|
52
|
+
} from '@searchcraft/react-sdk';
|
|
53
|
+
|
|
54
|
+
// import the styles from the SDK
|
|
55
|
+
import '../node_modules/@searchcraft/react-sdk/dist/style.css'
|
|
56
|
+
|
|
57
|
+
const SearchWithResultsComponent = () => {
|
|
58
|
+
const { isRequesting, query, search, searchResults } = useSearchcraft()
|
|
59
|
+
|
|
60
|
+
const handleClearInput = () => {
|
|
61
|
+
setQuery('')
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<>
|
|
66
|
+
<AutoSearchForm
|
|
67
|
+
handleSubmit={handleSubmitForm}
|
|
68
|
+
inputCaptionValue="Search here"
|
|
69
|
+
onClearedInput={handleClearInput}
|
|
70
|
+
rightToLeftOrientation
|
|
71
|
+
/>
|
|
72
|
+
<BaseSearchResults />
|
|
73
|
+
</>
|
|
74
|
+
)
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## UI Components
|
|
79
|
+
|
|
80
|
+
The SDK provides 4 pre-built UI components with data provided to them via the `SearchcraftProvider` and `useSearchcraft` hook.
|
|
81
|
+
|
|
82
|
+
```jsx
|
|
83
|
+
import {
|
|
84
|
+
// Search input without submit button
|
|
85
|
+
AutoSearchForm,
|
|
86
|
+
// Search input with submit button
|
|
87
|
+
BasicSearchForm,
|
|
88
|
+
// Single Search Result component that can be customized while iterating over SearchResults
|
|
89
|
+
BasicSearchResult,
|
|
90
|
+
// Fully encapsulated list of search results with no iteration required
|
|
91
|
+
BasicSearchResults,
|
|
92
|
+
useSearchcraft
|
|
93
|
+
} from '@searchcraft/react-sdk';
|
|
94
|
+
|
|
95
|
+
const AutoSearchImplementation = () => {
|
|
96
|
+
const { query, search, searchResults } = useSearchcraft()
|
|
97
|
+
|
|
98
|
+
const handleSubmitForm = async () => await search(query, 'fuzzy')
|
|
99
|
+
|
|
100
|
+
const handleClearInput = () => setQuery('');
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<>
|
|
104
|
+
<AutoSearchForm
|
|
105
|
+
handleSubmit={handleSubmitForm}
|
|
106
|
+
inputCaptionValue="Search here"
|
|
107
|
+
onClearedInput={handleClearInput}
|
|
108
|
+
rightToLeftOrientation={true}
|
|
109
|
+
/>
|
|
110
|
+
</>
|
|
111
|
+
)
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const BasicSearchAndResultsImplementation = () => {
|
|
115
|
+
const { query, search, searchResults } = useSearchcraft();
|
|
116
|
+
|
|
117
|
+
const handleSubmitForm = async () => await search(query, 'fuzzy');
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<>
|
|
121
|
+
<BaseSearchForm
|
|
122
|
+
rightToLeftOrientation={false}
|
|
123
|
+
handleSubmit={handleSubmitForm}
|
|
124
|
+
/>
|
|
125
|
+
{searchResults?.hits ? (
|
|
126
|
+
searchResults?.hits?.map((document, index) => {
|
|
127
|
+
const { doc: result } = document;
|
|
128
|
+
const callback = () => {
|
|
129
|
+
console.log('interactive');
|
|
130
|
+
};
|
|
131
|
+
const buttonCallback = () => {
|
|
132
|
+
console.log('button callback');
|
|
133
|
+
};
|
|
134
|
+
return (
|
|
135
|
+
<BaseSearchResult
|
|
136
|
+
key={`${result?.id}-${index}`}
|
|
137
|
+
buttonLabel='View More'
|
|
138
|
+
buttonCallbackFn={buttonCallback}
|
|
139
|
+
callbackFn={callback}
|
|
140
|
+
interactiveResult
|
|
141
|
+
imageSrc={result?.poster}
|
|
142
|
+
resultBodyContent={result?.overview}
|
|
143
|
+
resultHeading={result?.title}
|
|
144
|
+
resultSubheading={result?.release_date}
|
|
145
|
+
/>
|
|
146
|
+
);
|
|
147
|
+
})
|
|
148
|
+
) : (
|
|
149
|
+
<div>no search results found</div>
|
|
150
|
+
)}
|
|
151
|
+
</>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -4,21 +4,26 @@ import { FC } from 'react';
|
|
|
4
4
|
import { JSX as JSX_2 } from 'react/jsx-runtime';
|
|
5
5
|
import { PropsWithChildren } from 'react';
|
|
6
6
|
import { ReactNode } from 'react';
|
|
7
|
-
import {
|
|
7
|
+
import { CoreSDK as Searchcraft } from '@searchcraft/core';
|
|
8
|
+
import { SearchcraftInstance } from '@searchcraft/core';
|
|
9
|
+
import { SearchError } from '@searchcraft/core';
|
|
10
|
+
import { SearchResult } from '@searchcraft/core';
|
|
8
11
|
import { SetStateAction } from 'react';
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
|
-
* *
|
|
14
|
+
* * Button-less form used to submit the search query with a timing configurable debounced function.
|
|
12
15
|
*/
|
|
13
16
|
export declare const AutoSearchForm: FC<AutoSearchFormProps>;
|
|
14
17
|
|
|
15
18
|
declare interface AutoSearchFormProps {
|
|
19
|
+
autoSearchFormClass?: string;
|
|
16
20
|
handleSubmit: (query: string) => void;
|
|
17
21
|
inputCaptionValue?: string;
|
|
18
22
|
labelForInput?: string;
|
|
19
23
|
onClearedInput?: () => void;
|
|
20
24
|
placeholderValue?: string;
|
|
21
25
|
rightToLeftOrientation?: boolean;
|
|
26
|
+
searchContainerClass?: string;
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
/**
|
|
@@ -49,11 +54,12 @@ declare interface BaseSearchResultProps {
|
|
|
49
54
|
resultSubheading?: string;
|
|
50
55
|
}
|
|
51
56
|
|
|
57
|
+
export declare const BaseSearchResults: FC;
|
|
58
|
+
|
|
52
59
|
export declare const Button: FC<ButtonProps>;
|
|
53
60
|
|
|
54
61
|
declare type ButtonProps = {
|
|
55
62
|
className?: string;
|
|
56
|
-
isRequesting?: boolean;
|
|
57
63
|
iconElement?: ReactNode;
|
|
58
64
|
iconOnly?: boolean;
|
|
59
65
|
iconPosition?: string;
|
|
@@ -83,7 +89,6 @@ declare type InputProps = {
|
|
|
83
89
|
inputCaptionClassName?: string;
|
|
84
90
|
inputCaptionValue?: string;
|
|
85
91
|
inputClassName?: string;
|
|
86
|
-
isRequesting?: boolean;
|
|
87
92
|
onClearInput?: () => void;
|
|
88
93
|
onSearchInputChange: ChangeEventHandler<HTMLInputElement>;
|
|
89
94
|
placeholderValue?: string;
|
|
@@ -91,14 +96,14 @@ declare type InputProps = {
|
|
|
91
96
|
query: string;
|
|
92
97
|
};
|
|
93
98
|
|
|
94
|
-
export
|
|
99
|
+
export { Searchcraft }
|
|
100
|
+
|
|
101
|
+
export declare const SearchcraftProvider: ({ children, searchcraft, }: PropsWithChildren & SearchcraftInstance) => JSX_2.Element;
|
|
95
102
|
|
|
96
103
|
export declare interface SearchcraftProviderConfig {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
endpointPath: string;
|
|
101
|
-
};
|
|
104
|
+
index: string[];
|
|
105
|
+
apiKey: string;
|
|
106
|
+
endpointURL: string;
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
/** The context provided by the Searchcraft React SDK */
|
|
@@ -108,9 +113,9 @@ export declare interface SearchcraftProviderContext {
|
|
|
108
113
|
*/
|
|
109
114
|
error: Error | null;
|
|
110
115
|
/**
|
|
111
|
-
* The db name created in Vektron to query.
|
|
116
|
+
* The db name(s) created in Vektron to query.
|
|
112
117
|
*/
|
|
113
|
-
index: string;
|
|
118
|
+
index: string[];
|
|
114
119
|
/**
|
|
115
120
|
* Value exposed when a search is in progress.
|
|
116
121
|
*/
|
|
@@ -130,12 +135,11 @@ export declare interface SearchcraftProviderContext {
|
|
|
130
135
|
/**
|
|
131
136
|
* Function for searching that requires search mode and search type.
|
|
132
137
|
*/
|
|
133
|
-
search:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
} | null;
|
|
138
|
+
search: (query: string, mode: 'fuzzy' | 'normal') => void;
|
|
139
|
+
/**
|
|
140
|
+
* Object that is returned from the Searchcraft API when a search request has resolved
|
|
141
|
+
*/
|
|
142
|
+
searchResults?: SearchResult | SearchError | null;
|
|
139
143
|
}
|
|
140
144
|
|
|
141
145
|
export declare const useSearchcraft: () => SearchcraftProviderContext;
|