Skip to content

Commit

Permalink
[state-router] Support programmatical intent navigation (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Aug 24, 2017
1 parent 9c14ccf commit 31d9ecd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/@sanity/state-router/demo-server/components/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class Product extends React.Component {
static propTypes = {
id: PropTypes.string
}

render() {
const {id} = this.props
const nextProductId = Math.random().toString(32).substring(2)
Expand All @@ -24,6 +25,15 @@ export default class Product extends React.Component {
<IntentLink intent="open" params={{type: 'product', id: 'foo'}}>
Open Foo
</IntentLink>
<p>
<WithRouter>
{router => (
<button onClick={() => router.navigateIntent('open', {type: 'product', id: 'foobar'})}>
Click to programmatically navigate to intent <b>open:</b> product foobar
</button>
)}
</WithRouter>
</p>
<WithRouter>{router => <pre>{JSON.stringify(router.state)}</pre>}</WithRouter>
<p>
<StateLink toIndex>Up…</StateLink>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class RouterProvider extends React.Component {
resolveIntentLink: this.resolveIntentLink,
navigateUrl: this.navigateUrl,
navigate: this.navigateState,
navigateIntent: this.navigateIntent,
getState: this.getState,
channel: pubsub()
}
Expand All @@ -44,6 +45,10 @@ export default class RouterProvider extends React.Component {
this.navigateUrl(this.resolvePathFromState(nextState), options)
}

navigateIntent = (intentName : string, params : Object, options : NavigateOptions = {}) : void => {
this.navigateUrl(this.resolveIntentLink(intentName, params), options)
}

getState = () => this._state

resolvePathFromState = (state : Object) : string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default function withRouter(ComposedComponent: ReactClass<{}>) : ReactCla
render() {
const router = {
state: this.state.routerState,
navigate: this.context.__internalRouter.navigate
navigate: this.context.__internalRouter.navigate,
navigateIntent: this.context.__internalRouter.navigateIntent
}
return <ComposedComponent {...this.props} router={router} />
}
Expand Down

0 comments on commit 31d9ecd

Please sign in to comment.