Skip to content

Commit

Permalink
fix(react-native): use View as a container for react-native (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobylito authored and vvo committed Dec 14, 2016
1 parent 75bec0d commit 5b76f75
Show file tree
Hide file tree
Showing 43 changed files with 2,113 additions and 96 deletions.
93 changes: 1 addition & 92 deletions docgen/src/guide/React_native.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,98 +8,7 @@ navWeight: 35

`react-instantsearch` is compatible with [react-native](https://facebook.github.io/react-native/).

Here's an example showing you how to build a `SearchBox` and an infinite scroll view:

```jsx
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
TextInput,
Image,
} from 'react-native';
import {InstantSearch} from 'react-instantsearch/native';
import {connectSearchBox, connectInfiniteHits} from 'react-instantsearch/connectors';

export default InfiniteSearch = () =>
<View style={styles.maincontainer}>
<InstantSearch
className="container-fluid"
appId="appId"
apiKey="apiKey"
indexName="indexName"
>
<SearchBox/>
<Hits/>
</InstantSearch>
</View>;

const SearchBox = connectSearchBox(({currentRefinement, refine}) =>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={(text) => refine(text)}
value={currentRefinement}
/>
);

const Hits = connectInfiniteHits(React.createClass({
onEndReached: function () {
if (this.props.hasMore) {
this.props.refine();
}
},

render: function () {
const dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
const hits = this.props.hits.length > 0 ?
<View style={styles.maincontainer}>
<ListView
style={styles.list}
dataSource={dataSource.cloneWithRows(this.props.hits)}
renderRow={this._renderRow}
onEndReached={this.onEndReached}/>
</View> :
null;

return hits;
},

_renderRow: function (hit) {
return (
<View style={styles.item}>
<Image
style={{height: 100, width: 100}}
source={{uri: hit.image}}
/>
<Text>{hit.name}</Text>
</View> );
},
}));

const styles = StyleSheet.create({
maincontainer: {
backgroundColor: 'white',
flex: 1,
paddingTop: 20,
paddingBottom: 10,
flexDirection: 'column',
},
list: {
flex: 1,
flexDirection: 'column',
},
item: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white'
}
});

AppRegistry.registerComponent('InfiniteSearch', () => InfiniteSearch);
```
[Read the example](https://github.com/algolia/instantsearch.js/tree/v2/packages/react-instantsearch/examples/reactnative) linking `react-instantsearch` to [react-native](https://facebook.github.io/react-native/).

<div class="guide-nav">
<div class="guide-nav-left">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"private": true,
"version": "2.0.0",
Expand Down Expand Up @@ -83,6 +82,7 @@
"react-hot-loader": "^3.0.0-beta.4",
"react-instantsearch": "file:./packages/react-instantsearch/",
"react-instantsearch-theme-algolia": "file:./packages/react-instantsearch/",
"react-native": "^0.39.2",
"react-tap-event-plugin": "^2.0.1",
"react-test-renderer": "^15.4.1",
"recursive-readdir": "^2.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/react-instantsearch/examples/reactnative/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["react-native"]
}
6 changes: 6 additions & 0 deletions packages/react-instantsearch/examples/reactnative/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[android]
target = Google Inc.:Google APIs:23

[maven_repositories]
central = https://repo1.maven.org/maven2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
42 changes: 42 additions & 0 deletions packages/react-instantsearch/examples/reactnative/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log

# BUCK
buck-out/
\.buckd/
android/app/libs
*.keystore
103 changes: 103 additions & 0 deletions packages/react-instantsearch/examples/reactnative/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/

import React from 'react';
import {
StyleSheet,
Text,
View,
ListView,
TextInput,
Image,
} from 'react-native';
import {InstantSearch} from 'react-instantsearch/native';
import {connectSearchBox, connectInfiniteHits} from 'react-instantsearch/connectors';

const styles = StyleSheet.create({
maincontainer: {
paddingTop: 20,
paddingBottom: 10,
},
item: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white',
},
});

export default React.createClass({
render() {
return (
<View style={styles.maincontainer}>
<InstantSearch
className="container-fluid"
appId="latency"
apiKey="6be0576ff61c053d5f9a3225e2a90f76"
indexName="ikea"
>
<ConnectedSearchBox/>
<ConnectedHits/>
</InstantSearch>
</View>
);
},
});

class SearchBox extends React.Component {
render() {
return <TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={text => this.props.refine(text)}
value={this.props.currentRefinement}
/>;
}
}

SearchBox.propTypes = {
refine: React.PropTypes.func.isRequired,
currentRefinement: React.PropTypes.string,
};

const ConnectedSearchBox = connectSearchBox(SearchBox);

class Hits extends React.Component {
onEndReached = () => {
if (this.props.hasMore) {
this.props.refine();
}
};

render() {
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
const hits = this.props.hits.length > 0 ?
<View >
<ListView
dataSource={ds.cloneWithRows(this.props.hits)}
renderRow={this._renderRow}
onEndReached={this.onEndReached}/>
</View> : null;
return hits;
}

_renderRow = hit =>
<View style={styles.item}>
<Image
style={{height: 100, width: 100}}
source={{uri: hit.image}}
/>
<Text>
{hit.name}
</Text>
</View>;
}

Hits.propTypes = {
hits: React.PropTypes.array.isRequired,
refine: React.PropTypes.func.isRequired,
hasMore: React.PropTypes.bool.isRequired,
};

const ConnectedHits = connectInfiniteHits(Hits);
66 changes: 66 additions & 0 deletions packages/react-instantsearch/examples/reactnative/android/app/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import re

# To learn about Buck see [Docs](https://buckbuild.com/).
# To run your application with Buck:
# - install Buck
# - `npm start` - to start the packager
# - `cd android`
# - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"`
# - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck
# - `buck install -r android/app` - compile, install and run application
#

lib_deps = []
for jarfile in glob(['libs/*.jar']):
name = 'jars__' + re.sub(r'^.*/([^/]+)\.jar$', r'\1', jarfile)
lib_deps.append(':' + name)
prebuilt_jar(
name = name,
binary_jar = jarfile,
)

for aarfile in glob(['libs/*.aar']):
name = 'aars__' + re.sub(r'^.*/([^/]+)\.aar$', r'\1', aarfile)
lib_deps.append(':' + name)
android_prebuilt_aar(
name = name,
aar = aarfile,
)

android_library(
name = 'all-libs',
exported_deps = lib_deps
)

android_library(
name = 'app-code',
srcs = glob([
'src/main/java/**/*.java',
]),
deps = [
':all-libs',
':build_config',
':res',
],
)

android_build_config(
name = 'build_config',
package = 'com.reactnative',
)

android_resource(
name = 'res',
res = 'src/main/res',
package = 'com.reactnative',
)

android_binary(
name = 'app',
package_type = 'debug',
manifest = 'src/main/AndroidManifest.xml',
keystore = '//android/keystores:debug',
deps = [
':app-code',
],
)
Loading

0 comments on commit 5b76f75

Please sign in to comment.