-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove links from qari to surah * add surah button to other qaris. closes #64
- Loading branch information
Showing
2 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
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
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,105 @@ | ||
import React from 'react'; | ||
|
||
function isLeftClickEvent(event) { | ||
return event.button === 0; | ||
} | ||
|
||
function isModifiedEvent(event) { | ||
return !!( | ||
event.metaKey || | ||
event.altKey || | ||
event.ctrlKey || | ||
event.shiftKey | ||
); | ||
} | ||
|
||
function createLocationDescriptor(to, query, hash, state) { | ||
if (query || hash || state) { | ||
return { pathname: to, query, hash, state }; | ||
} | ||
|
||
return to; | ||
} | ||
|
||
const propTypes = { | ||
onlyActiveOnIndex: React.PropTypes.bool.isRequired, | ||
to: React.PropTypes.oneOfType([ | ||
React.PropTypes.string, | ||
React.PropTypes.object, | ||
]).isRequired, | ||
query: React.PropTypes.string, | ||
hash: React.PropTypes.string, | ||
state: React.PropTypes.object, | ||
action: React.PropTypes.oneOf([ | ||
'push', | ||
'replace', | ||
]).isRequired, | ||
onClick: React.PropTypes.func, | ||
active: React.PropTypes.bool, | ||
target: React.PropTypes.string, | ||
children: React.PropTypes.node.isRequired, | ||
}; | ||
|
||
const contextTypes = { | ||
router: React.PropTypes.object, | ||
}; | ||
|
||
const defaultProps = { | ||
onlyActiveOnIndex: false, | ||
action: 'push', | ||
}; | ||
|
||
class LinkContainer extends React.Component { | ||
onClick = (event) => { | ||
const { | ||
to, query, hash, state, children, onClick, target, action, | ||
} = this.props; | ||
|
||
if (children.props.onClick) { | ||
children.props.onClick(event); | ||
} | ||
|
||
if (onClick) { | ||
onClick(event); | ||
} | ||
|
||
if ( | ||
target || | ||
event.defaultPrevented || | ||
isModifiedEvent(event) || | ||
!isLeftClickEvent(event) | ||
) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
|
||
this.context.router[action]( | ||
createLocationDescriptor(to, query, hash, state) | ||
); | ||
}; | ||
|
||
render() { | ||
const { router } = this.context; | ||
const { onlyActiveOnIndex, to, children, ...props } = this.props; | ||
|
||
props.onClick = this.onClick; | ||
|
||
// Ignore if rendered outside Router context; simplifies unit testing. | ||
if (router) { | ||
props.href = router.createHref(to); | ||
|
||
if (props.active === null) { | ||
props.active = router.isActive(to, onlyActiveOnIndex); | ||
} | ||
} | ||
|
||
return React.cloneElement(React.Children.only(children), props); | ||
} | ||
} | ||
|
||
LinkContainer.propTypes = propTypes; | ||
LinkContainer.contextTypes = contextTypes; | ||
LinkContainer.defaultProps = defaultProps; | ||
|
||
export default LinkContainer; |