Created
April 21, 2022 14:03
-
-
Save cenan/bd4d147a13992219cf4a782fd10211bb to your computer and use it in GitHub Desktop.
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
import * as React from "react"; | |
export type TLinkRef = HTMLSpanElement; | |
export interface ILinkProps { | |
/** | |
* Caption of the link element | |
*/ | |
caption: string; | |
/** | |
* Optional class names | |
*/ | |
className?: string; | |
/** | |
* Click handler callback. | |
* | |
* Also fired when enter or space key is pressed while the element is focused. | |
*/ | |
onClick: () => void; | |
} | |
/** | |
* Clickable span element. | |
* | |
* Accepts enter and space key as click. | |
*/ | |
// tslint:disable-next-line: variable-name | |
const Link = React.forwardRef<TLinkRef, ILinkProps>(({ caption, className, onClick }: ILinkProps, ref?) => { | |
const handleKeyDown = (event: React.KeyboardEvent<HTMLSpanElement>) => { | |
if (event.keyCode === 13 || event.keyCode === 32) { | |
onClick(); | |
event.preventDefault(); | |
event.stopPropagation(); | |
} | |
}; | |
const classes = (className ?? "") + " action"; | |
return ( | |
<span className={classes} | |
tabIndex={0} | |
onClick={onClick} | |
onKeyDown={handleKeyDown} | |
ref={ref} | |
>{caption}</span> | |
); | |
}); | |
Link.displayName = "Link"; | |
export { Link }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment