@searchcraft/react-sdk 0.6.0 → 0.7.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 CHANGED
@@ -1,8 +1,8 @@
1
1
  # Searchcraft React SDK
2
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.
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
- ## Usage
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
- Wrap your app or target a specific interface and wrap them with the `Searchcraft.Provider` to expose the functionalities of the SDK.
19
+ ## Initialization and Basic Usage
20
20
 
21
- ```jsx
22
-
23
- import { useMemo } from 'react'
21
+ ### React Components
24
22
 
25
- import Searchcraft, { SearchcraftCore } from '@searchcraft/react-sdk'
23
+ The React SDK provides the following prebuilt components for use in your application:
26
24
 
27
- // For Searchcraft Cloud, values are created via the Searchcraft Vektron Customer Portal. For self-hosted refer to the API docs. https://docs.searchcraft.io/reference/api/overview/
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
- const Main = () => {
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
38
- return (
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
- Once your app is wrapped in the `Searchcraft.Provider`, the `useSearchcraft` hook functionalities are available for use.
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
- 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
- };
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
- <button onClick={toggleTheme}>Toggle Theme</button>
80
- <AutoSearchForm
81
- handleSubmit={handleSubmitForm}
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
- ## UI Components
83
+ Example Filter Configuration
93
84
 
94
- The SDK provides 4 pre-built UI components with data provided to them via the `Searchcraft.Provider` and `useSearchcraft` hook.
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
- // OR
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
- import Searchcraft, { useSearchcraft } from '@searchcraft/react-sdk'
125
+ ```
112
126
 
113
- const AutoSearchImplementation = () => {
114
- const { query, search, searchResults } = useSearchcraft()
127
+ Example Result Mappings
115
128
 
116
- const handleSubmitForm = async () => await search(query, 'fuzzy')
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
- const handleClearInput = () => setQuery('');
131
+ ```jsx
119
132
 
120
- return (
121
- <>
122
- // example using compound component pattern
123
- <Searchcraft.AutoSearchForm
124
- handleSubmit={handleSubmitForm}
125
- inputCaptionValue="Search here"
126
- onClearInput={handleClearInput}
127
- rightToLeftOrientation={true}
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
- Additionally, you can import individual sub-components that comprise the pre-built UI components.
176
+ Event Handling
172
177
 
173
- ```jsx
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
- const InputComponent = () => {
177
- const { query, setQuery } = useSearchcraft();
178
- const [error, setError] = useState<boolean>(false);
180
+ ```jsx
179
181
 
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
- };
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
- const handleSearchInputChange = (event: { target: { value: string } }) =>
191
- setQuery(event.target.value);
200
+ ```
192
201
 
193
- const handleClearInput = () => setQuery('');
202
+ Additional Information
194
203
 
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
- ```
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,31 +1,11 @@
1
1
  import { Components } from '@searchcraft/javascript-sdk';
2
2
  import { FC } from 'react';
3
3
 
4
- export declare const SearchcraftBaseSearchResult: FC<SearchcraftBaseSearchResultProps>;
5
-
6
- export declare interface SearchcraftBaseSearchResultProps extends Components.SearchcraftBaseSearchResult {
7
- }
8
-
9
4
  export declare const SearchcraftBaseSearchResults: FC<SearchcraftBaseSearchResultsProps>;
10
5
 
11
6
  export declare interface SearchcraftBaseSearchResultsProps extends Components.SearchcraftBaseSearchResults {
12
7
  }
13
8
 
14
- export declare const SearchcraftButton: FC<SearchcraftButtonProps>;
15
-
16
- export declare interface SearchcraftButtonProps extends Components.SearchcraftButton {
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
-
29
9
  export declare const SearchcraftFilterPanel: FC<SearchcraftFilterPanelProps>;
30
10
 
31
11
  export declare interface SearchcraftFilterPanelProps extends Components.SearchcraftFilterPanel {
@@ -36,24 +16,24 @@ export declare const SearchcraftInputForm: FC<SearchcraftInputFormProps>;
36
16
  export declare interface SearchcraftInputFormProps extends Components.SearchcraftInputForm {
37
17
  }
38
18
 
39
- export declare const SearchcraftInputLabel: FC<SearchcraftInputLabelProps>;
19
+ export declare const SearchcraftPopoverButton: FC<SearchcraftPopoverButtonProps>;
40
20
 
41
- export declare interface SearchcraftInputLabelProps extends Components.SearchcraftInputLabel {
21
+ export declare interface SearchcraftPopoverButtonProps extends Components.SearchcraftPopoverButton {
42
22
  }
43
23
 
44
- export declare const SearchcraftResultsInfo: FC<SearchcraftResultsInfoProps>;
24
+ export declare const SearchcraftPopoverForm: FC<SearchcraftPopoverFormProps>;
45
25
 
46
- export declare interface SearchcraftResultsInfoProps extends Components.SearchcraftResultsInfo {
26
+ export declare interface SearchcraftPopoverFormProps extends Components.SearchcraftPopoverForm {
47
27
  }
48
28
 
49
- export declare const SearchcraftSlider: FC<SearchcraftSliderProps>;
29
+ export declare const SearchcraftResultsInfo: FC<SearchcraftResultsInfoProps>;
50
30
 
51
- export declare interface SearchcraftSliderProps extends Components.SearchcraftSlider {
31
+ export declare interface SearchcraftResultsInfoProps extends Components.SearchcraftResultsInfo {
52
32
  }
53
33
 
54
- export declare const SearchcraftToggleButton: FC<SearchcraftToggleButtonProps>;
34
+ export declare const SearchcraftTheme: FC<SearchcraftThemeProps>;
55
35
 
56
- export declare interface SearchcraftToggleButtonProps extends Components.SearchcraftToggleButton {
36
+ export declare interface SearchcraftThemeProps extends Components.SearchcraftTheme {
57
37
  }
58
38
 
59
39
  export { }