@searchcraft/react-sdk 0.7.0 → 0.7.2
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 +152 -156
- package/dist/index.d.ts +51 -32
- package/dist/index.js +1695 -1393
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Searchcraft React SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Searchcraft React SDK provides tools for integrating the Searchcraft API into your React applications. It supports core functionalities like searching, managing state, and working with search results.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
7
|
Install with Yarn:
|
|
8
8
|
|
|
@@ -16,193 +16,189 @@ Install with NPM:
|
|
|
16
16
|
$ npm install @searchcraft/react-sdk
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
## Initialization and Basic Usage
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
import { useMemo } from 'react'
|
|
21
|
+
### React Components
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
The React SDK provides the following prebuilt components for use in your application:
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
const searchcraftConfig = {
|
|
29
|
-
index: ['data_test'],
|
|
30
|
-
apiKey: '1234.909.jmk',
|
|
31
|
-
endpointURL: 'http://127.0.0.1:8000',
|
|
32
|
-
}
|
|
25
|
+
```jsx
|
|
33
26
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
<Searchcraft.Provider {...{ searchcraft, debug: true }}>
|
|
40
|
-
<YourApplicationOrScreenGoHere />
|
|
41
|
-
</Searchcraft.Provider>
|
|
42
|
-
)
|
|
43
|
-
}
|
|
27
|
+
<SearchInputForm />
|
|
28
|
+
<SearchPopoverForm />
|
|
29
|
+
<FilterPanel />
|
|
30
|
+
<ResultsInfo />
|
|
31
|
+
<BaseSearchResults />
|
|
44
32
|
|
|
45
33
|
```
|
|
46
|
-
### Quickstart with useSearchcraft hook
|
|
47
34
|
|
|
48
|
-
|
|
35
|
+
These components can be rendered directly in your app and customized with configurations as needed.
|
|
36
|
+
|
|
37
|
+
Example Setup
|
|
49
38
|
|
|
50
39
|
```jsx
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
search,
|
|
61
|
-
searchResults,
|
|
62
|
-
setQuery,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// import the styles from the SDK
|
|
66
|
-
import '../node_modules/@searchcraft/react-sdk/dist/style.css'
|
|
67
|
-
|
|
68
|
-
const SearchWithResultsComponent = () => {
|
|
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();
|
|
72
|
-
|
|
73
|
-
const handleClearInput = () => {
|
|
74
|
-
setQuery('')
|
|
40
|
+
|
|
41
|
+
import React from 'react';
|
|
42
|
+
import { SearchInputForm, FilterPanel, BaseSearchResults } from '@searchcraft/react-sdk/components';
|
|
43
|
+
|
|
44
|
+
const config = {
|
|
45
|
+
index: ['your_index_from_vektron'],
|
|
46
|
+
readKey: 'your_read_key_from_vektron',
|
|
47
|
+
endpointURL: 'your_searchcraft_endpoint_url',
|
|
48
|
+
searchDebounceDelay: 50, // Optional debounce delay in milliseconds
|
|
75
49
|
};
|
|
76
50
|
|
|
51
|
+
const filterPanelItems = [
|
|
52
|
+
{
|
|
53
|
+
type: 'mostRecentToggle',
|
|
54
|
+
label: 'Most Recent',
|
|
55
|
+
options: {
|
|
56
|
+
subLabel: 'Show the most recently published articles first.',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
type: 'exactMatchToggle',
|
|
61
|
+
label: 'Exact Match',
|
|
62
|
+
options: {
|
|
63
|
+
subLabel: 'Only show results that precisely match your search.',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const App = () => {
|
|
77
69
|
return (
|
|
78
70
|
<>
|
|
79
|
-
|
|
80
|
-
<
|
|
81
|
-
|
|
82
|
-
inputCaptionValue="Search here"
|
|
83
|
-
onClearInput={handleClearInput}
|
|
84
|
-
rightToLeftOrientation
|
|
85
|
-
/>
|
|
86
|
-
<Searchcraft.BaseSearchResults />
|
|
71
|
+
<SearchInputForm config={config} />
|
|
72
|
+
<FilterPanel items={filterPanelItems} />
|
|
73
|
+
<BaseSearchResults />
|
|
87
74
|
</>
|
|
88
|
-
)
|
|
75
|
+
);
|
|
89
76
|
};
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
export default App;
|
|
80
|
+
|
|
90
81
|
```
|
|
91
82
|
|
|
92
|
-
|
|
83
|
+
Example Filter Configuration
|
|
93
84
|
|
|
94
|
-
|
|
85
|
+
Below is an example of a filter panel configuration object. It specifies the filters to be displayed in the filter panel and maps them to the corresponding index fields.
|
|
95
86
|
|
|
96
87
|
```jsx
|
|
97
|
-
import {
|
|
98
|
-
// Search input without submit button
|
|
99
|
-
AutoSearchForm,
|
|
100
|
-
// Search input with submit button
|
|
101
|
-
BaseSearchForm,
|
|
102
|
-
// Single Search Result component that can be customized while iterating over SearchResults
|
|
103
|
-
BaseSearchResult,
|
|
104
|
-
// Fully encapsulated list of search results with no iteration required
|
|
105
|
-
BaseSearchResults,
|
|
106
|
-
useSearchcraft
|
|
107
|
-
} from '@searchcraft/react-sdk';
|
|
108
88
|
|
|
109
|
-
|
|
89
|
+
const today = new Date();
|
|
90
|
+
const pastDate = new Date(today);
|
|
91
|
+
pastDate.setFullYear(today.getFullYear() - 10);
|
|
92
|
+
|
|
93
|
+
export const filterPanelItems = [
|
|
94
|
+
{
|
|
95
|
+
type: 'mostRecentToggle',
|
|
96
|
+
label: 'Most Recent',
|
|
97
|
+
options: {
|
|
98
|
+
subLabel: 'Show the most recently published articles first.',
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
type: 'exactMatchToggle',
|
|
103
|
+
label: 'Exact Match',
|
|
104
|
+
options: {
|
|
105
|
+
subLabel: 'Only show results that precisely match your search.',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: 'dateRange',
|
|
110
|
+
fieldName: 'date_published',
|
|
111
|
+
label: 'Filter by Year',
|
|
112
|
+
options: {
|
|
113
|
+
minDate: pastDate,
|
|
114
|
+
maxDate: today,
|
|
115
|
+
granularity: 'year',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
type: 'facets',
|
|
120
|
+
fieldName: 'section',
|
|
121
|
+
label: 'Filter by Section',
|
|
122
|
+
},
|
|
123
|
+
];
|
|
110
124
|
|
|
111
|
-
|
|
125
|
+
```
|
|
112
126
|
|
|
113
|
-
|
|
114
|
-
const { query, search, searchResults } = useSearchcraft()
|
|
127
|
+
Example Result Mappings
|
|
115
128
|
|
|
116
|
-
|
|
129
|
+
Customize the appearance and behavior of search results by modifying the searchResultMappings configuration. Use field names from your index to map the content.
|
|
117
130
|
|
|
118
|
-
|
|
131
|
+
```jsx
|
|
119
132
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
export const searchResultMappings = {
|
|
134
|
+
containerHref: {
|
|
135
|
+
fieldNames: [
|
|
136
|
+
{
|
|
137
|
+
fieldName: 'canonical_link',
|
|
138
|
+
dataType: 'text',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
footer: {
|
|
143
|
+
fieldNames: [
|
|
144
|
+
{
|
|
145
|
+
fieldName: 'date_published',
|
|
146
|
+
dataType: 'date',
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
fieldName: 'author_name',
|
|
150
|
+
dataType: 'text',
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
delimiter: ' • ',
|
|
154
|
+
},
|
|
155
|
+
imageSource: {
|
|
156
|
+
fieldNames: [
|
|
157
|
+
{
|
|
158
|
+
fieldName: 'medium_image',
|
|
159
|
+
dataType: 'text',
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
},
|
|
163
|
+
body: {
|
|
164
|
+
fieldNames: [{ fieldName: 'sub_headline', dataType: 'text' }],
|
|
165
|
+
},
|
|
166
|
+
title: {
|
|
167
|
+
fieldNames: [{ fieldName: 'section_name', dataType: 'text' }],
|
|
168
|
+
},
|
|
169
|
+
subtitle: {
|
|
170
|
+
fieldNames: [{ fieldName: 'headline', dataType: 'text' }],
|
|
171
|
+
},
|
|
131
172
|
};
|
|
132
173
|
|
|
133
|
-
const BaseSearchResults: FC = () => {
|
|
134
|
-
const { searchResults, query } = useSearchcraft();
|
|
135
|
-
|
|
136
|
-
return (
|
|
137
|
-
<>
|
|
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
|
-
/>
|
|
165
|
-
)}
|
|
166
|
-
</>
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
174
|
```
|
|
170
175
|
|
|
171
|
-
|
|
176
|
+
Event Handling
|
|
172
177
|
|
|
173
|
-
|
|
174
|
-
import { Input, useSearchcraft } from '@searchcraft/react-sdk';
|
|
178
|
+
The React SDK components support event handling for common actions such as query submission, input focus, or search results.
|
|
175
179
|
|
|
176
|
-
|
|
177
|
-
const { query, setQuery } = useSearchcraft();
|
|
178
|
-
const [error, setError] = useState<boolean>(false);
|
|
180
|
+
```jsx
|
|
179
181
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
182
|
+
<SearchInputForm
|
|
183
|
+
onQuerySubmit={(event) => {
|
|
184
|
+
console.log('Query submitted:', event.detail);
|
|
185
|
+
}}
|
|
186
|
+
onInputFocus={() => {
|
|
187
|
+
console.log('Input focused');
|
|
188
|
+
}}
|
|
189
|
+
onInputBlur={() => {
|
|
190
|
+
console.log('Input blurred');
|
|
191
|
+
}}
|
|
192
|
+
/>
|
|
193
|
+
|
|
194
|
+
<BaseSearchResults
|
|
195
|
+
onNoResults={() => {
|
|
196
|
+
console.log('No results found');
|
|
197
|
+
}}
|
|
198
|
+
/>
|
|
189
199
|
|
|
190
|
-
|
|
191
|
-
setQuery(event.target.value);
|
|
200
|
+
```
|
|
192
201
|
|
|
193
|
-
|
|
202
|
+
Additional Information
|
|
194
203
|
|
|
195
|
-
|
|
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
|
-
```
|
|
204
|
+
For more advanced use cases and component-specific documentation, refer to the official [Searchcraft React SDK documentation](https://docs.searchcraft.io/).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,69 +1,88 @@
|
|
|
1
1
|
import { Components } from '@searchcraft/javascript-sdk';
|
|
2
2
|
import { FC } from 'react';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* The React component for SearchcraftBaseSearchResults.
|
|
6
|
+
* @param props
|
|
7
|
+
*/
|
|
9
8
|
export declare const SearchcraftBaseSearchResults: FC<SearchcraftBaseSearchResultsProps>;
|
|
10
9
|
|
|
10
|
+
/**
|
|
11
|
+
* The props for the SearchcraftBaseSearchResults component.
|
|
12
|
+
*/
|
|
11
13
|
export declare interface SearchcraftBaseSearchResultsProps extends Components.SearchcraftBaseSearchResults {
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export declare const SearchcraftErrorMessage: FC<SearchcraftErrorMessageProps>;
|
|
20
|
-
|
|
21
|
-
export declare interface SearchcraftErrorMessageProps extends Components.SearchcraftErrorMessage {
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export declare const SearchcraftFacetList: FC<SearchcraftFacetListProps>;
|
|
25
|
-
|
|
26
|
-
export declare interface SearchcraftFacetListProps extends Components.SearchcraftFacetList {
|
|
27
|
-
}
|
|
28
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The React component for the SearchcraftFilterPanel.
|
|
18
|
+
* @param props
|
|
19
|
+
*/
|
|
29
20
|
export declare const SearchcraftFilterPanel: FC<SearchcraftFilterPanelProps>;
|
|
30
21
|
|
|
22
|
+
/**
|
|
23
|
+
* The props for the SearchcraftFilterPanel.
|
|
24
|
+
*/
|
|
31
25
|
export declare interface SearchcraftFilterPanelProps extends Components.SearchcraftFilterPanel {
|
|
32
26
|
}
|
|
33
27
|
|
|
28
|
+
/**
|
|
29
|
+
* The React component for the SearchcraftInputForm
|
|
30
|
+
* @param props
|
|
31
|
+
*/
|
|
34
32
|
export declare const SearchcraftInputForm: FC<SearchcraftInputFormProps>;
|
|
35
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The props for the SearchcraftInputForm.
|
|
36
|
+
*/
|
|
36
37
|
export declare interface SearchcraftInputFormProps extends Components.SearchcraftInputForm {
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The React component for the SearchcraftPopoverButton.
|
|
42
|
+
* @param props
|
|
43
|
+
*/
|
|
44
44
|
export declare const SearchcraftPopoverButton: FC<SearchcraftPopoverButtonProps>;
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* The props for the SearchcraftPopoverButton.
|
|
48
|
+
*/
|
|
46
49
|
export declare interface SearchcraftPopoverButtonProps extends Components.SearchcraftPopoverButton {
|
|
47
50
|
}
|
|
48
51
|
|
|
52
|
+
/**
|
|
53
|
+
* The React component for SearchcraftPopoverForm.
|
|
54
|
+
* @param props
|
|
55
|
+
*/
|
|
49
56
|
export declare const SearchcraftPopoverForm: FC<SearchcraftPopoverFormProps>;
|
|
50
57
|
|
|
58
|
+
/**
|
|
59
|
+
* The props for the SearchcraftPopoverForm component.
|
|
60
|
+
*/
|
|
51
61
|
export declare interface SearchcraftPopoverFormProps extends Components.SearchcraftPopoverForm {
|
|
52
62
|
}
|
|
53
63
|
|
|
64
|
+
/**
|
|
65
|
+
* The React component for SearchcraftResultsInfo.
|
|
66
|
+
* @param props
|
|
67
|
+
*/
|
|
54
68
|
export declare const SearchcraftResultsInfo: FC<SearchcraftResultsInfoProps>;
|
|
55
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The Props for the SearchcraftResultsInfo component.
|
|
72
|
+
*/
|
|
56
73
|
export declare interface SearchcraftResultsInfoProps extends Components.SearchcraftResultsInfo {
|
|
57
74
|
}
|
|
58
75
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export declare const SearchcraftToggleButton: FC<SearchcraftToggleButtonProps>;
|
|
76
|
+
/**
|
|
77
|
+
* The react component for SearchcraftTheme.
|
|
78
|
+
* @param props
|
|
79
|
+
*/
|
|
80
|
+
export declare const SearchcraftTheme: FC<SearchcraftThemeProps>;
|
|
65
81
|
|
|
66
|
-
|
|
82
|
+
/**
|
|
83
|
+
* The Props for the SearchcraftTheme component.
|
|
84
|
+
*/
|
|
85
|
+
export declare interface SearchcraftThemeProps extends Components.SearchcraftTheme {
|
|
67
86
|
}
|
|
68
87
|
|
|
69
88
|
export { }
|