Skip to content

Commit 47634d5

Browse files
authored
feat(react-native): update getting-started and migrate query-suggestions (#476)
1 parent 964ef5d commit 47634d5

30 files changed

+14008
-7313
lines changed

react-instantsearch-native/getting-started/.expo-shared/assets.json

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
# Dependencies
12
node_modules/
3+
4+
# Expo
25
.expo/
36
dist/
4-
npm-debug.*
7+
web-build/
8+
9+
# Native
10+
*.orig.*
511
*.jks
612
*.p8
713
*.p12
814
*.key
915
*.mobileprovision
10-
*.orig.*
11-
web-build/
16+
17+
# Metro
18+
.metro-health-check*
19+
20+
# debug
21+
npm-debug.*
22+
yarn-debug.*
23+
yarn-error.*
1224

1325
# macOS
1426
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5"
4+
}

react-instantsearch-native/getting-started/App.tsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import React, { useRef, useState } from 'react';
2-
import { FlatList, SafeAreaView, StyleSheet, Text, View } from 'react-native';
2+
import {
3+
FlatList,
4+
Platform,
5+
SafeAreaView,
6+
StyleSheet,
7+
Text,
8+
View,
9+
} from 'react-native';
310
import { StatusBar } from 'expo-status-bar';
411
import algoliasearch from 'algoliasearch/lite';
5-
import { InstantSearch } from 'react-instantsearch';
12+
import { Configure, InstantSearch } from 'react-instantsearch-core';
613

714
import { InfiniteHits } from './src/InfiniteHits';
815
import { SearchBox } from './src/SearchBox';
@@ -28,10 +35,11 @@ export default function App() {
2835
<StatusBar style="light" />
2936
<View style={styles.container}>
3037
<InstantSearch searchClient={searchClient} indexName="instant_search">
38+
<Configure highlightPreTag="<mark>" highlightPostTag="</mark>" />
3139
<SearchBox onChange={scrollToTop} />
3240
<Filters
3341
isModalOpen={isModalOpen}
34-
onToggleModal={() => setModalOpen((isOpen) => !isOpen)}
42+
onToggleModal={() => setModalOpen(!isModalOpen)}
3543
onChange={scrollToTop}
3644
/>
3745
<InfiniteHits ref={listRef} hitComponent={Hit} />
@@ -57,6 +65,8 @@ const styles = StyleSheet.create({
5765
safe: {
5866
flex: 1,
5967
backgroundColor: '#252b33',
68+
// @ts-ignore 100vh is valid but not recognized by react-native
69+
height: Platform.OS === 'web' ? '100vh' : '100%',
6070
},
6171
container: {
6272
flex: 1,

react-instantsearch-native/getting-started/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
# ais-ecommerce-demo-app
1+
# react-instantsearch-native-getting-started
22

33
_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._
44

5+
## Clone the example
6+
7+
```sh
8+
curl https://codeload.github.com/algolia/doc-code-samples/tar.gz/master | tar -xz --strip=2 doc-code-samples-master/react-instantsearch-native/getting-started
9+
```
10+
511
## Get started
612

713
To run this project locally, install the dependencies and run the local server:

react-instantsearch-native/getting-started/app.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
{
22
"expo": {
3-
"name": "ais-ecommerce-demo-app",
4-
"slug": "ais-ecommerce-demo-app",
3+
"name": "react-instantsearch-native-getting-started",
4+
"slug": "react-instantsearch-native-getting-started",
55
"version": "1.0.0",
66
"orientation": "portrait",
77
"icon": "./assets/icon.png",
8+
"userInterfaceStyle": "light",
89
"splash": {
910
"image": "./assets/splash.png",
1011
"resizeMode": "contain",
1112
"backgroundColor": "#ffffff"
1213
},
13-
"updates": {
14-
"fallbackToCacheTimeout": 0
15-
},
1614
"assetBundlePatterns": ["**/*"],
1715
"ios": {
1816
"supportsTablet": true
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
{
2-
"name": "ais-ecommerce-demo-app",
2+
"name": "react-instantsearch-native-getting-started",
33
"version": "1.0.0",
44
"private": true,
55
"main": "node_modules/expo/AppEntry.js",
66
"scripts": {
77
"start": "expo start",
88
"android": "expo start --android",
99
"ios": "expo start --ios",
10-
"web": "expo start --web",
11-
"eject": "expo eject"
10+
"web": "expo start --web"
1211
},
1312
"dependencies": {
14-
"algoliasearch": "4.12.1",
15-
"expo": "~44.0.0",
16-
"expo-status-bar": "~1.2.0",
17-
"instantsearch.js": "4.55.0",
18-
"react": "17.0.1",
19-
"react-dom": "17.0.1",
20-
"react-instantsearch-core": "7.0.0",
21-
"react-native": "0.64.3",
22-
"react-native-web": "0.17.1"
13+
"algoliasearch": "4.14.3",
14+
"expo": "~49.0.11",
15+
"expo-status-bar": "~1.6.0",
16+
"react": "18.2.0",
17+
"react-dom": "18.2.0",
18+
"react-instantsearch-core": "7.1.0",
19+
"react-native": "0.72.4",
20+
"react-native-web": "~0.19.9"
2321
},
2422
"devDependencies": {
25-
"@babel/core": "^7.12.9",
26-
"@types/react": "~17.0.21",
27-
"@types/react-native": "~0.64.12",
28-
"expo-cli": "5.1.1",
29-
"typescript": "~4.3.5"
23+
"@babel/core": "^7.20.0",
24+
"@expo/webpack-config": "^19.0.0",
25+
"@types/react": "^18.0.5",
26+
"typescript": "5.1.3"
3027
}
3128
}

react-instantsearch-native/getting-started/src/Filters.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
useClearRefinements,
1313
useCurrentRefinements,
1414
useRefinementList,
15-
} from 'react-instantsearch';
15+
} from 'react-instantsearch-core';
1616

1717
type FiltersProps = {
1818
isModalOpen: boolean;

react-instantsearch-native/getting-started/src/Highlight.tsx

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Fragment } from 'react';
2-
import { StyleSheet, Text } from 'react-native';
2+
import { StyleProp, StyleSheet, Text, TextStyle } from 'react-native';
33
import { Hit as AlgoliaHit } from '@algolia/client-search';
44
import {
55
getHighlightedParts,
@@ -9,27 +9,28 @@ import {
99
type HighlightPartProps = {
1010
children: React.ReactNode;
1111
isHighlighted: boolean;
12+
style: StyleProp<TextStyle>;
1213
};
1314

14-
function HighlightPart({ children, isHighlighted }: HighlightPartProps) {
15-
return (
16-
<Text style={isHighlighted ? styles.highlighted : styles.nonHighlighted}>
17-
{children}
18-
</Text>
19-
);
15+
function HighlightPart({ children, style }: HighlightPartProps) {
16+
return <Text style={style}>{children}</Text>;
2017
}
2118

2219
type HighlightProps<THit> = {
2320
hit: THit;
2421
attribute: keyof THit | string[];
2522
className?: string;
2623
separator?: string;
24+
highlightedStyle?: StyleProp<TextStyle>;
25+
nonHighlightedStyle?: StyleProp<TextStyle>;
2726
};
2827

2928
export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
3029
hit,
3130
attribute,
3231
separator = ', ',
32+
highlightedStyle = styles.highlighted,
33+
nonHighlightedStyle = styles.nonHighlighted,
3334
}: HighlightProps<THit>) {
3435
const { value: attributeValue = '' } =
3536
getPropertyByPath(hit._highlightResult, attribute as string) || {};
@@ -47,6 +48,11 @@ export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
4748
<HighlightPart
4849
key={subPartIndex}
4950
isHighlighted={subPart.isHighlighted}
51+
style={
52+
subPart.isHighlighted
53+
? highlightedStyle
54+
: nonHighlightedStyle
55+
}
5056
>
5157
{subPart.value}
5258
</HighlightPart>
@@ -58,7 +64,11 @@ export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
5864
}
5965

6066
return (
61-
<HighlightPart key={partIndex} isHighlighted={part.isHighlighted}>
67+
<HighlightPart
68+
key={partIndex}
69+
isHighlighted={part.isHighlighted}
70+
style={part.isHighlighted ? highlightedStyle : nonHighlightedStyle}
71+
>
6272
{part.value}
6373
</HighlightPart>
6474
);

react-instantsearch-native/getting-started/src/InfiniteHits.tsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React, { forwardRef } from 'react';
22
import { StyleSheet, View, FlatList } from 'react-native';
33
import { Hit as AlgoliaHit } from '@algolia/client-search';
4-
import { useInfiniteHits, UseInfiniteHitsProps } from 'react-instantsearch';
4+
import {
5+
useInfiniteHits,
6+
UseInfiniteHitsProps,
7+
} from 'react-instantsearch-core';
58

69
type InfiniteHitsProps<THit> = UseInfiniteHitsProps & {
710
hitComponent: (props: { hit: THit }) => JSX.Element;
@@ -12,7 +15,10 @@ export const InfiniteHits = forwardRef(
1215
{ hitComponent: Hit, ...props }: InfiniteHitsProps<THit>,
1316
ref: React.ForwardedRef<FlatList<THit>>
1417
) => {
15-
const { hits, isLastPage, showMore } = useInfiniteHits(props);
18+
const { hits, isLastPage, showMore } = useInfiniteHits({
19+
...props,
20+
escapeHTML: false,
21+
});
1622

1723
return (
1824
<FlatList
@@ -46,7 +52,8 @@ const styles = StyleSheet.create({
4652
});
4753

4854
declare module 'react' {
49-
function forwardRef<T, P = {}>(
50-
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
51-
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
55+
// eslint-disable-next-line no-shadow
56+
function forwardRef<TRef, TProps = unknown>(
57+
render: (props: TProps, ref: React.Ref<TRef>) => React.ReactElement | null
58+
): (props: TProps & React.RefAttributes<TRef>) => React.ReactElement | null;
5259
}

react-instantsearch-native/getting-started/src/SearchBox.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { useRef, useState } from 'react';
22
import { StyleSheet, View, TextInput } from 'react-native';
3-
import { useSearchBox, UseSearchBoxProps } from 'react-instantsearch';
3+
import { useSearchBox, UseSearchBoxProps } from 'react-instantsearch-core';
44

55
type SearchBoxProps = UseSearchBoxProps & {
66
onChange: (newValue: string) => void;
@@ -38,7 +38,8 @@ export function SearchBox({ onChange, ...props }: SearchBoxProps) {
3838
autoCapitalize="none"
3939
autoCorrect={false}
4040
spellCheck={false}
41-
autoCompleteType="off"
41+
autoComplete="off"
42+
placeholder="Search for products..."
4243
/>
4344
</View>
4445
);

0 commit comments

Comments
 (0)