Skip to content

Commit 31d9ecd

Browse files
committed
[state-router] Support programmatical intent navigation (#123)
1 parent 9c14ccf commit 31d9ecd

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

packages/@sanity/state-router/demo-server/components/Product.js

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class Product extends React.Component {
1010
static propTypes = {
1111
id: PropTypes.string
1212
}
13+
1314
render() {
1415
const {id} = this.props
1516
const nextProductId = Math.random().toString(32).substring(2)
@@ -24,6 +25,15 @@ export default class Product extends React.Component {
2425
<IntentLink intent="open" params={{type: 'product', id: 'foo'}}>
2526
Open Foo
2627
</IntentLink>
28+
<p>
29+
<WithRouter>
30+
{router => (
31+
<button onClick={() => router.navigateIntent('open', {type: 'product', id: 'foobar'})}>
32+
Click to programmatically navigate to intent <b>open:</b> product foobar
33+
</button>
34+
)}
35+
</WithRouter>
36+
</p>
2737
<WithRouter>{router => <pre>{JSON.stringify(router.state)}</pre>}</WithRouter>
2838
<p>
2939
<StateLink toIndex>Up…</StateLink>

packages/@sanity/state-router/src/components/RouterProvider.js

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class RouterProvider extends React.Component {
3030
resolveIntentLink: this.resolveIntentLink,
3131
navigateUrl: this.navigateUrl,
3232
navigate: this.navigateState,
33+
navigateIntent: this.navigateIntent,
3334
getState: this.getState,
3435
channel: pubsub()
3536
}
@@ -44,6 +45,10 @@ export default class RouterProvider extends React.Component {
4445
this.navigateUrl(this.resolvePathFromState(nextState), options)
4546
}
4647

48+
navigateIntent = (intentName : string, params : Object, options : NavigateOptions = {}) : void => {
49+
this.navigateUrl(this.resolveIntentLink(intentName, params), options)
50+
}
51+
4752
getState = () => this._state
4853

4954
resolvePathFromState = (state : Object) : string => {

packages/@sanity/state-router/src/components/withRouterHOC.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export default function withRouter(ComposedComponent: ReactClass<{}>) : ReactCla
4040
render() {
4141
const router = {
4242
state: this.state.routerState,
43-
navigate: this.context.__internalRouter.navigate
43+
navigate: this.context.__internalRouter.navigate,
44+
navigateIntent: this.context.__internalRouter.navigateIntent
4445
}
4546
return <ComposedComponent {...this.props} router={router} />
4647
}

0 commit comments

Comments
 (0)