diff --git a/types/react-instantsearch-core/react-instantsearch-core-tests.ts b/types/react-instantsearch-core/react-instantsearch-core-tests.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/types/react-instantsearch-core/react-instantsearch-core-tests.tsx b/types/react-instantsearch-core/react-instantsearch-core-tests.tsx new file mode 100644 index 0000000000..6389261461 --- /dev/null +++ b/types/react-instantsearch-core/react-instantsearch-core-tests.tsx @@ -0,0 +1,79 @@ +import * as React from 'react'; +import { + createInstantSearch, + createIndex, + createConnector +} from 'react-instantsearch-core'; + +() => { + const InstantSearch = createInstantSearch(() => {}, {Root: 'div', props: {className: `widget`}}); + + +
+
; +}; + +() => { + const Index = createIndex({Root: 'div', props: {className: `widget`}}); + + +
+
; +}; + +// https://community.algolia.com/react-instantsearch/guide/Custom_connectors.html +() => { + const CoolWidget = createConnector({ + displayName: 'CoolWidget', + + getProvidedProps(props: any, searchState: any) { + // Since the `queryAndPage` searchState entry isn't necessarily defined, we need + // to default its value. + const [query, page] = searchState.queryAndPage || ['', 0]; + + // Connect the underlying component to the `queryAndPage` searchState entry. + return { + query, + page, + }; + }, + + refine(props: any, searchState: any, newQuery: any, newPage: any) { + // When the underlying component calls its `refine` prop, update the searchState + // with the new query and page. + return { + // `searchState` represents the search state of *all* widgets. We need to extend it + // instead of replacing it, otherwise other widgets will lose their + // respective state. + ...searchState, + queryAndPage: [newQuery, newPage], + }; + }, + })(props => +
+ The query is {props.query}, the page is {props.page}. + {/* + Clicking on this button will update the searchState to: + { + ...otherSearchState, + query: 'algolia', + page: 20, + } + */} +
+ ); + + +
+
; +}; diff --git a/types/react-instantsearch-core/tsconfig.json b/types/react-instantsearch-core/tsconfig.json index 8bc3203b55..f250ba980d 100644 --- a/types/react-instantsearch-core/tsconfig.json +++ b/types/react-instantsearch-core/tsconfig.json @@ -18,6 +18,6 @@ "files": [ "index.d.ts", "types.d.ts", - "react-instantsearch-core-tests.ts" + "react-instantsearch-core-tests.tsx" ] }