@searchcraft/react-sdk 0.1.3 → 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 CHANGED
@@ -1,4 +1,4 @@
1
- # Searchcraft's React SDK
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
 
@@ -34,7 +34,7 @@ const searchcraftConfig = {
34
34
  const Main = () => {
35
35
  // initialize and pass an instance of SearchcraftCore into the provider
36
36
  const searchcraft = useMemo(() => new SearchcraftCore(searchcraftConfig),[]);
37
- // debug is optional
37
+ // debug is optional and has a default value of false
38
38
  return (
39
39
  <Searchcraft.Provider {...{ searchcraft, debug: true }}>
40
40
  <YourApplicationOrScreenGoHere />
@@ -48,13 +48,27 @@ const Main = () => {
48
48
  Once your app is wrapped in the `Searchcraft.Provider`, the `useSearchcraft` hook functionalities are available for use.
49
49
 
50
50
  ```jsx
51
- import Searchcraft, { useSearchcraft, AutoSearchForm } from '@searchcraft/react-sdk'
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
+ };
52
64
 
53
65
  // import the styles from the SDK
54
66
  import '../node_modules/@searchcraft/react-sdk/dist/style.css'
55
67
 
56
68
  const SearchWithResultsComponent = () => {
57
- 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();
58
72
 
59
73
  const handleClearInput = () => {
60
74
  setQuery('')
@@ -62,10 +76,11 @@ const handleClearInput = () => {
62
76
 
63
77
  return (
64
78
  <>
79
+ <button onClick={toggleTheme}>Toggle Theme</button>
65
80
  <AutoSearchForm
66
81
  handleSubmit={handleSubmitForm}
67
82
  inputCaptionValue="Search here"
68
- onClearedInput={handleClearInput}
83
+ onClearInput={handleClearInput}
69
84
  rightToLeftOrientation
70
85
  />
71
86
  <Searchcraft.BaseSearchResults />
@@ -90,7 +105,9 @@ import {
90
105
  BaseSearchResults,
91
106
  useSearchcraft
92
107
  } from '@searchcraft/react-sdk';
93
- OR
108
+
109
+ // OR
110
+
94
111
  import Searchcraft, { useSearchcraft } from '@searchcraft/react-sdk'
95
112
 
96
113
  const AutoSearchImplementation = () => {
@@ -102,56 +119,53 @@ const AutoSearchImplementation = () => {
102
119
 
103
120
  return (
104
121
  <>
122
+ // example using compound component pattern
105
123
  <Searchcraft.AutoSearchForm
106
124
  handleSubmit={handleSubmitForm}
107
125
  inputCaptionValue="Search here"
108
- onClearedInput={handleClearInput}
126
+ onClearInput={handleClearInput}
109
127
  rightToLeftOrientation={true}
110
128
  />
111
129
  </>
112
130
  )
113
131
  };
114
132
 
115
- const BaseSearchAndResultsImplementation = () => {
116
- const { query, search, searchResults } = useSearchcraft();
117
-
118
- const handleSubmitForm = async () => await search(query, 'fuzzy');
133
+ const BaseSearchResults: FC = () => {
134
+ const { searchResults, query } = useSearchcraft();
119
135
 
120
136
  return (
121
137
  <>
122
- <Searchcraft.BaseSearchForm
123
- rightToLeftOrientation={false}
124
- handleSubmit={handleSubmitForm}
125
- />
126
- {searchResults?.hits ? (
127
- searchResults?.hits?.map((document, index) => {
128
- const { doc: result } = document;
129
- const callback = () => {
130
- console.log('interactive');
131
- };
132
- const buttonCallback = () => {
133
- console.log('button callback');
134
- };
135
- return (
136
- <Searchcraft.BaseSearchResult
137
- key={`${result?.id}-${index}`}
138
- buttonLabel='View More'
139
- buttonCallbackFn={buttonCallback}
140
- callbackFn={callback}
141
- interactiveResult
142
- imageSrc={result?.poster}
143
- resultBodyContent={result?.overview}
144
- resultHeading={result?.title}
145
- resultSubheading={result?.release_date}
146
- />
147
- );
148
- })
149
- ) : (
150
- <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
+ />
151
165
  )}
152
166
  </>
153
167
  );
154
- }
168
+ };
155
169
  ```
156
170
 
157
171
  Additionally, you can import individual sub-components that comprise the pre-built UI components.
package/dist/index.d.ts CHANGED
@@ -101,35 +101,14 @@ declare type InputProps = {
101
101
  query: string;
102
102
  };
103
103
 
104
- declare const Searchcraft: FC & {
105
- AutoSearchForm: FC<AutoSearchFormProps>;
106
- BaseSearchForm: FC<BaseSearchFormProps>;
107
- BaseSearchResult: FC<BaseSearchResultProps>;
108
- BaseSearchResults: FC;
109
- Button: FC;
110
- ClearInputButton: FC;
111
- ErrorMessage: FC;
112
- Input: FC<InputProps>;
113
- InputCaption: FC<InputCaptionProps>;
114
- InputIcon: FC;
115
- InputLabel: FC<InputLabelProps>;
116
- Provider: FC<{
117
- searchcraft: SearchcraftCore;
118
- debug?: boolean;
119
- }>;
120
- };
121
- export default Searchcraft;
122
-
123
- export { SearchcraftCore }
124
-
125
- export declare interface SearchcraftProviderConfig {
104
+ export declare interface ProviderConfigTypes {
126
105
  index: string[];
127
106
  apiKey: string;
128
107
  endpointURL: string;
129
108
  }
130
109
 
131
110
  /** The context provided by the Searchcraft React SDK */
132
- export declare interface SearchcraftProviderContext {
111
+ export declare interface ProviderContextTypes {
133
112
  /**
134
113
  * Error occurred while making a search query.
135
114
  */
@@ -164,6 +143,36 @@ export declare interface SearchcraftProviderContext {
164
143
  searchResults?: SearchResult | SearchError | null;
165
144
  }
166
145
 
167
- export declare const useSearchcraft: () => SearchcraftProviderContext;
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;
168
177
 
169
178
  export { }