-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): add react router v4 example
* Add React Router v4 example * Linted * fix linter
- Loading branch information
Showing
7 changed files
with
5,662 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
packages/react-instantsearch/examples/react-router-v4/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
This example shows how to synchronize your instantsearch url | ||
if you are using react-router. | ||
|
||
To start the example: | ||
|
||
```sh | ||
yarn | ||
yarn start | ||
``` | ||
|
||
Read more about react-instantsearch [in our documentation](https://community.algolia.com/instantsearch.js/react/). |
20 changes: 20 additions & 0 deletions
20
packages/react-instantsearch/examples/react-router-v4/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"private": true, | ||
"devDependencies": { | ||
"react-scripts": "^0.8.5" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.16.6", | ||
"qs": "^6.3.0", | ||
"react": "^15.3.2", | ||
"react-dom": "^15.3.2", | ||
"react-instantsearch": "latest", | ||
"react-instantsearch-theme-algolia": "latest", | ||
"react-router": "4.0.0-alpha.6" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start" | ||
}, | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/react-instantsearch/examples/react-router-v4/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>react-router feat react-instantsearch</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
95 changes: 95 additions & 0 deletions
95
packages/react-instantsearch/examples/react-router-v4/src/App.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, {Component} from 'react'; | ||
import { | ||
InstantSearch, HierarchicalMenu, | ||
Hits, Menu, Pagination, PoweredBy, StarRating, | ||
RefinementList, SearchBox, ClearAll, | ||
} from 'react-instantsearch/dom'; | ||
import 'react-instantsearch-theme-algolia/style.css'; | ||
import qs from 'qs'; | ||
|
||
export default class App extends Component { | ||
constructor (props) { | ||
super(props); | ||
this.state = {}; | ||
} | ||
|
||
onSearchStateChange (nextSearchState) { | ||
const THRESHOLD = 700; | ||
const newPush = Date.now(); | ||
const {router} = this.context; | ||
this.setState({lastPush: newPush}); | ||
|
||
if (this.state.lastPush && newPush - this.state.lastPush <= THRESHOLD) { | ||
router.replaceWith(nextSearchState ? `?${qs.stringify(nextSearchState)}` : ''); | ||
} else { | ||
router.transitionTo(nextSearchState ? `?${qs.stringify(nextSearchState)}` : ''); | ||
} | ||
} | ||
|
||
render() { | ||
const {location} = this.props; | ||
return ( | ||
<InstantSearch | ||
appId="latency" | ||
apiKey="6be0576ff61c053d5f9a3225e2a90f76" | ||
indexName="ikea" | ||
searchState={qs.parse(location.query)} | ||
onSearchStateChange={this.onSearchStateChange.bind(this)} | ||
createURL={state => `?${qs.stringify(state)}`} | ||
> | ||
|
||
<div> | ||
<div style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
marginBottom: 10, | ||
}}> | ||
<SearchBox/> | ||
<PoweredBy /> | ||
</div> | ||
<div style={{display: 'flex'}}> | ||
<div style={{padding: '0px 20px'}}> | ||
<p>Hierarchical Menu</p> | ||
<HierarchicalMenu | ||
id="categories" | ||
attributes={[ | ||
'category', | ||
'sub_category', | ||
'sub_sub_category', | ||
]} | ||
/> | ||
<p>Menu</p> | ||
<Menu attributeName="type"/> | ||
<p>Refinement List</p> | ||
<RefinementList attributeName="colors"/> | ||
<p>Range Ratings</p> | ||
<StarRating attributeName="rating" max={6}/> | ||
|
||
</div> | ||
<div style={{display: 'flex', flexDirection: 'column', flex: 1}}> | ||
<div style={{display: 'flex', justifyContent: 'space-around'}}> | ||
<ClearAll/> | ||
</div> | ||
<div> | ||
<Hits /> | ||
</div> | ||
<div style={{alignSelf: 'center'}}> | ||
<Pagination showLast={true}/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</InstantSearch> | ||
); | ||
} | ||
} | ||
|
||
App.propTypes = { | ||
location: React.PropTypes.object, | ||
}; | ||
|
||
App.contextTypes = { | ||
router: React.PropTypes.object, | ||
}; |
14 changes: 14 additions & 0 deletions
14
packages/react-instantsearch/examples/react-router-v4/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import App from './App'; | ||
import {BrowserRouter, Match} from 'react-router'; | ||
|
||
ReactDOM.render( | ||
<BrowserRouter> | ||
<Match | ||
pattern="/" | ||
component={App} | ||
/> | ||
</BrowserRouter>, | ||
document.getElementById('root') | ||
); |
Oops, something went wrong.