@searchcraft/react-sdk 0.1.2 → 0.1.4
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 +108 -54
- package/dist/index.d.ts +43 -12
- package/dist/index.js +621 -414
- package/dist/index.js.map +1 -1
- package/dist/style.css +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Searchcraft
|
|
1
|
+
# Searchcraft React SDK
|
|
2
2
|
|
|
3
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
4
|
|
|
@@ -16,13 +16,13 @@ Install with NPM:
|
|
|
16
16
|
$ npm install @searchcraft/react-sdk
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
Wrap your app or target a specific interface and wrap them with the `
|
|
19
|
+
Wrap your app or target a specific interface and wrap them with the `Searchcraft.Provider` to expose the functionalities of the SDK.
|
|
20
20
|
|
|
21
21
|
```jsx
|
|
22
22
|
|
|
23
23
|
import { useMemo } from 'react'
|
|
24
24
|
|
|
25
|
-
import {
|
|
25
|
+
import Searchcraft, { SearchcraftCore } from '@searchcraft/react-sdk'
|
|
26
26
|
|
|
27
27
|
// values are created via the Searchcraft Vektron Customer Portal
|
|
28
28
|
const searchcraftConfig = {
|
|
@@ -32,30 +32,43 @@ const searchcraftConfig = {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
const Main = () => {
|
|
35
|
-
// initialize and pass an instance of
|
|
36
|
-
const searchcraft = useMemo(() => new
|
|
35
|
+
// initialize and pass an instance of SearchcraftCore into the provider
|
|
36
|
+
const searchcraft = useMemo(() => new SearchcraftCore(searchcraftConfig),[]);
|
|
37
|
+
// debug is optional and has a default value of false
|
|
37
38
|
return (
|
|
38
|
-
<
|
|
39
|
+
<Searchcraft.Provider {...{ searchcraft, debug: true }}>
|
|
39
40
|
<YourApplicationOrScreenGoHere />
|
|
40
|
-
</
|
|
41
|
+
</Searchcraft.Provider>
|
|
41
42
|
)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
```
|
|
45
46
|
### Quickstart with useSearchcraft hook
|
|
46
47
|
|
|
47
|
-
Once your app is wrapped in the `
|
|
48
|
+
Once your app is wrapped in the `Searchcraft.Provider`, the `useSearchcraft` hook functionalities are available for use.
|
|
48
49
|
|
|
49
50
|
```jsx
|
|
50
|
-
import {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
import Searchcraft, { useSearchcraft, AutoSearchForm, useTheme } from '@searchcraft/react-sdk'
|
|
52
|
+
|
|
53
|
+
// values available when using the useSearchcraft hook
|
|
54
|
+
const useSearchContextValues = {
|
|
55
|
+
error,
|
|
56
|
+
index,
|
|
57
|
+
isRequesting,
|
|
58
|
+
mode,
|
|
59
|
+
query,
|
|
60
|
+
search,
|
|
61
|
+
searchResults,
|
|
62
|
+
setQuery,
|
|
63
|
+
};
|
|
53
64
|
|
|
54
65
|
// import the styles from the SDK
|
|
55
66
|
import '../node_modules/@searchcraft/react-sdk/dist/style.css'
|
|
56
67
|
|
|
57
68
|
const SearchWithResultsComponent = () => {
|
|
58
|
-
const { isRequesting, query, search, searchResults } = useSearchcraft()
|
|
69
|
+
const { isRequesting, query, search, searchResults } = useSearchcraft();
|
|
70
|
+
// useTheme hook is exposed to control the dark and light UIs available. Configured values are 'light' and 'dark'.
|
|
71
|
+
const { toggleTheme, theme } = useTheme();
|
|
59
72
|
|
|
60
73
|
const handleClearInput = () => {
|
|
61
74
|
setQuery('')
|
|
@@ -63,13 +76,14 @@ const handleClearInput = () => {
|
|
|
63
76
|
|
|
64
77
|
return (
|
|
65
78
|
<>
|
|
79
|
+
<button onClick={toggleTheme}>Toggle Theme</button>
|
|
66
80
|
<AutoSearchForm
|
|
67
81
|
handleSubmit={handleSubmitForm}
|
|
68
82
|
inputCaptionValue="Search here"
|
|
69
|
-
|
|
83
|
+
onClearInput={handleClearInput}
|
|
70
84
|
rightToLeftOrientation
|
|
71
85
|
/>
|
|
72
|
-
<BaseSearchResults />
|
|
86
|
+
<Searchcraft.BaseSearchResults />
|
|
73
87
|
</>
|
|
74
88
|
)
|
|
75
89
|
};
|
|
@@ -77,21 +91,25 @@ const handleClearInput = () => {
|
|
|
77
91
|
|
|
78
92
|
## UI Components
|
|
79
93
|
|
|
80
|
-
The SDK provides 4 pre-built UI components with data provided to them via the `
|
|
94
|
+
The SDK provides 4 pre-built UI components with data provided to them via the `Searchcraft.Provider` and `useSearchcraft` hook.
|
|
81
95
|
|
|
82
96
|
```jsx
|
|
83
97
|
import {
|
|
84
98
|
// Search input without submit button
|
|
85
99
|
AutoSearchForm,
|
|
86
100
|
// Search input with submit button
|
|
87
|
-
|
|
101
|
+
BaseSearchForm,
|
|
88
102
|
// Single Search Result component that can be customized while iterating over SearchResults
|
|
89
|
-
|
|
103
|
+
BaseSearchResult,
|
|
90
104
|
// Fully encapsulated list of search results with no iteration required
|
|
91
|
-
|
|
105
|
+
BaseSearchResults,
|
|
92
106
|
useSearchcraft
|
|
93
107
|
} from '@searchcraft/react-sdk';
|
|
94
108
|
|
|
109
|
+
// OR
|
|
110
|
+
|
|
111
|
+
import Searchcraft, { useSearchcraft } from '@searchcraft/react-sdk'
|
|
112
|
+
|
|
95
113
|
const AutoSearchImplementation = () => {
|
|
96
114
|
const { query, search, searchResults } = useSearchcraft()
|
|
97
115
|
|
|
@@ -101,54 +119,90 @@ const AutoSearchImplementation = () => {
|
|
|
101
119
|
|
|
102
120
|
return (
|
|
103
121
|
<>
|
|
104
|
-
|
|
122
|
+
// example using compound component pattern
|
|
123
|
+
<Searchcraft.AutoSearchForm
|
|
105
124
|
handleSubmit={handleSubmitForm}
|
|
106
125
|
inputCaptionValue="Search here"
|
|
107
|
-
|
|
126
|
+
onClearInput={handleClearInput}
|
|
108
127
|
rightToLeftOrientation={true}
|
|
109
128
|
/>
|
|
110
129
|
</>
|
|
111
130
|
)
|
|
112
131
|
};
|
|
113
132
|
|
|
114
|
-
const
|
|
115
|
-
const {
|
|
116
|
-
|
|
117
|
-
const handleSubmitForm = async () => await search(query, 'fuzzy');
|
|
133
|
+
const BaseSearchResults: FC = () => {
|
|
134
|
+
const { searchResults, query } = useSearchcraft();
|
|
118
135
|
|
|
119
136
|
return (
|
|
120
137
|
<>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
) : (
|
|
149
|
-
<div>no search results found</div>
|
|
138
|
+
{searchResults?.data?.hits?.map((document, index) => {
|
|
139
|
+
const { doc: result } = document;
|
|
140
|
+
const callback = () => {
|
|
141
|
+
console.log('interactive element');
|
|
142
|
+
};
|
|
143
|
+
const buttonCallback = () => {
|
|
144
|
+
console.log('button callback');
|
|
145
|
+
};
|
|
146
|
+
return (
|
|
147
|
+
// example using named import component
|
|
148
|
+
<BaseSearchResult
|
|
149
|
+
key={`${result?.id}-${index}`}
|
|
150
|
+
buttonLabel='View More'
|
|
151
|
+
buttonCallbackFn={buttonCallback}
|
|
152
|
+
callbackFn={callback}
|
|
153
|
+
interactiveResult
|
|
154
|
+
imageSrc={result?.poster}
|
|
155
|
+
resultBodyContent={result?.overview}
|
|
156
|
+
resultHeading={result?.title}
|
|
157
|
+
resultSubheading={result?.release_date}
|
|
158
|
+
/>
|
|
159
|
+
);
|
|
160
|
+
})}
|
|
161
|
+
{query.length > 0 && searchResults?.data?.hits?.length === 0 && (
|
|
162
|
+
<ErrorMessage
|
|
163
|
+
errorMessage={`No search results found for ${query} query`}
|
|
164
|
+
/>
|
|
150
165
|
)}
|
|
151
166
|
</>
|
|
152
167
|
);
|
|
153
|
-
}
|
|
168
|
+
};
|
|
154
169
|
```
|
|
170
|
+
|
|
171
|
+
Additionally, you can import individual sub-components that comprise the pre-built UI components.
|
|
172
|
+
|
|
173
|
+
```jsx
|
|
174
|
+
import { Input, useSearchcraft } from '@searchcraft/react-sdk';
|
|
175
|
+
|
|
176
|
+
const InputComponent = () => {
|
|
177
|
+
const { query, setQuery } = useSearchcraft();
|
|
178
|
+
const [error, setError] = useState<boolean>(false);
|
|
179
|
+
|
|
180
|
+
const handleFormSubmit = (e: { preventDefault: () => void }) => {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
if (query.trim() === '') {
|
|
183
|
+
setError(true);
|
|
184
|
+
} else {
|
|
185
|
+
setError(false);
|
|
186
|
+
handleSubmit(query);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const handleSearchInputChange = (event: { target: { value: string } }) =>
|
|
191
|
+
setQuery(event.target.value);
|
|
192
|
+
|
|
193
|
+
const handleClearInput = () => setQuery('');
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<>
|
|
197
|
+
<Input
|
|
198
|
+
error={error}
|
|
199
|
+
onClearInput={handleClearInput}
|
|
200
|
+
onSearchInputChange={handleSearchInputChange}
|
|
201
|
+
placeholderValue={placeholderValue}
|
|
202
|
+
query={query}
|
|
203
|
+
rightToLeftOrientation={rightToLeftOrientation}
|
|
204
|
+
/>
|
|
205
|
+
</>
|
|
206
|
+
)
|
|
207
|
+
};
|
|
208
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { ChangeEventHandler } from 'react';
|
|
2
2
|
import { Dispatch } from 'react';
|
|
3
3
|
import { FC } from 'react';
|
|
4
|
-
import { JSX as JSX_2 } from 'react/jsx-runtime';
|
|
5
|
-
import { PropsWithChildren } from 'react';
|
|
6
4
|
import { ReactNode } from 'react';
|
|
7
|
-
import { CoreSDK as
|
|
8
|
-
import { SearchcraftInstance } from '@searchcraft/core';
|
|
5
|
+
import { CoreSDK as SearchcraftCore } from '@searchcraft/core';
|
|
9
6
|
import { SearchError } from '@searchcraft/core';
|
|
10
7
|
import { SearchResult } from '@searchcraft/core';
|
|
11
8
|
import { SetStateAction } from 'react';
|
|
@@ -20,7 +17,7 @@ declare interface AutoSearchFormProps {
|
|
|
20
17
|
handleSubmit: (query: string) => void;
|
|
21
18
|
inputCaptionValue?: string;
|
|
22
19
|
labelForInput?: string;
|
|
23
|
-
|
|
20
|
+
onClearInput?: () => void;
|
|
24
21
|
placeholderValue?: string;
|
|
25
22
|
rightToLeftOrientation?: boolean;
|
|
26
23
|
searchContainerClass?: string;
|
|
@@ -35,6 +32,7 @@ declare interface BaseSearchFormProps {
|
|
|
35
32
|
errorMessage?: string;
|
|
36
33
|
handleSubmit: (query: string) => void;
|
|
37
34
|
labelForInput?: string;
|
|
35
|
+
onClearInput?: () => void;
|
|
38
36
|
placeholderValue?: string;
|
|
39
37
|
rightToLeftOrientation?: boolean;
|
|
40
38
|
}
|
|
@@ -76,6 +74,13 @@ declare type ErrorMessageProps = {
|
|
|
76
74
|
|
|
77
75
|
export declare const Input: FC<InputProps>;
|
|
78
76
|
|
|
77
|
+
declare type InputCaptionProps = {
|
|
78
|
+
error?: boolean;
|
|
79
|
+
inputCaptionClassName?: string;
|
|
80
|
+
inputCaptionValue: string;
|
|
81
|
+
rightToLeftOrientation?: boolean;
|
|
82
|
+
};
|
|
83
|
+
|
|
79
84
|
export declare const InputLabel: FC<InputLabelProps>;
|
|
80
85
|
|
|
81
86
|
declare type InputLabelProps = {
|
|
@@ -96,18 +101,14 @@ declare type InputProps = {
|
|
|
96
101
|
query: string;
|
|
97
102
|
};
|
|
98
103
|
|
|
99
|
-
export
|
|
100
|
-
|
|
101
|
-
export declare const SearchcraftProvider: ({ children, searchcraft, }: PropsWithChildren & SearchcraftInstance) => JSX_2.Element;
|
|
102
|
-
|
|
103
|
-
export declare interface SearchcraftProviderConfig {
|
|
104
|
+
export declare interface ProviderConfigTypes {
|
|
104
105
|
index: string[];
|
|
105
106
|
apiKey: string;
|
|
106
107
|
endpointURL: string;
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
/** The context provided by the Searchcraft React SDK */
|
|
110
|
-
export declare interface
|
|
111
|
+
export declare interface ProviderContextTypes {
|
|
111
112
|
/**
|
|
112
113
|
* Error occurred while making a search query.
|
|
113
114
|
*/
|
|
@@ -142,6 +143,36 @@ export declare interface SearchcraftProviderContext {
|
|
|
142
143
|
searchResults?: SearchResult | SearchError | null;
|
|
143
144
|
}
|
|
144
145
|
|
|
145
|
-
|
|
146
|
+
declare const Searchcraft: FC & {
|
|
147
|
+
AutoSearchForm: FC<AutoSearchFormProps>;
|
|
148
|
+
BaseSearchForm: FC<BaseSearchFormProps>;
|
|
149
|
+
BaseSearchResult: FC<BaseSearchResultProps>;
|
|
150
|
+
BaseSearchResults: FC;
|
|
151
|
+
Button: FC;
|
|
152
|
+
ClearInputButton: FC;
|
|
153
|
+
ErrorMessage: FC;
|
|
154
|
+
Input: FC<InputProps>;
|
|
155
|
+
InputCaption: FC<InputCaptionProps>;
|
|
156
|
+
InputIcon: FC;
|
|
157
|
+
InputLabel: FC<InputLabelProps>;
|
|
158
|
+
Provider: FC<{
|
|
159
|
+
searchcraft: SearchcraftCore;
|
|
160
|
+
debug?: boolean;
|
|
161
|
+
}>;
|
|
162
|
+
};
|
|
163
|
+
export default Searchcraft;
|
|
164
|
+
|
|
165
|
+
export { SearchcraftCore }
|
|
166
|
+
|
|
167
|
+
declare interface ThemeContextType {
|
|
168
|
+
theme: ThemeOptionType;
|
|
169
|
+
toggleTheme: () => void;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
declare type ThemeOptionType = 'light' | 'dark';
|
|
173
|
+
|
|
174
|
+
export declare const useSearchcraft: () => ProviderContextTypes;
|
|
175
|
+
|
|
176
|
+
export declare const useTheme: () => ThemeContextType;
|
|
146
177
|
|
|
147
178
|
export { }
|