Skip to content

Instantly share code, notes, and snippets.

@natemartinsf
Created November 27, 2016 05:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemartinsf/b9d4ea7995d0009522b49c474b2cf613 to your computer and use it in GitHub Desktop.
Save natemartinsf/b9d4ea7995d0009522b49c474b2cf613 to your computer and use it in GitHub Desktop.
Wrapped @blueprintjs Button to work with React-Router
import React, {Component} from 'react';
import { Button } from '@blueprintjs/core';
import withRouter from 'react-router/lib/withRouter';
let typeOf = (o) => toString.call(o).slice(8, -1).toLowerCase();
function createLocationDescriptor({to, query, hash, state}) {
if (typeOf(to) === 'string') {
return {pathname: to, query, hash, state};
}
return {query, hash, state, ...to};
}
function resolveToLocation(to, router) {
return typeof to === 'function' ? to(router.location) : to;
}
class ActiveButton extends Component {
render() {
let {
to, query, hash, state, onlyActiveOnIndex,
router,
...allProps,
} = this.props;
let location = createLocationDescriptor({to, query, hash, state});
let active = false;
if (router) {
active = router.isActive(location, onlyActiveOnIndex);
}
let handleClick =(event) => {
event.preventDefault();
router.push(resolveToLocation(this.props.to, router));
};
return (
<Button {...allProps} disabled={active} onClick={handleClick}/>
);
}
}
const LinkButton = withRouter(ActiveButton);
export default LinkButton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment