-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #99
- Loading branch information
Showing
4 changed files
with
90 additions
and
56 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,58 +1,72 @@ | ||
|
||
import React, { MouseEvent } from 'react'; | ||
import React, { MouseEvent, Ref } from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
export interface Props { | ||
/** Bootstrap theme name (e.g. `danger`, `success`) */ | ||
theme?: string; | ||
/** Bootstrap theme name (e.g. `danger`, `success`) */ | ||
theme?: string; | ||
|
||
/** Additional class names to apply to the button */ | ||
className?: string; | ||
/** Additional class names to apply to the button */ | ||
className?: string; | ||
|
||
/** Should click events / link redirects be ignored */ | ||
disabled?: boolean; | ||
/** Should click events / link redirects be ignored */ | ||
disabled?: boolean; | ||
|
||
/** | ||
* Button link target. Will render as a React Router <Link> if supplied. | ||
* | ||
* Buttons with a `to` will not execute `onClick`. | ||
*/ | ||
to?: string; | ||
/** | ||
* Button link target. Will render as a React Router <Link> if supplied. | ||
* | ||
* Buttons with a `to` will not execute `onClick`. | ||
*/ | ||
to?: string; | ||
|
||
/** Click event callback */ | ||
onClick?(event: MouseEvent<HTMLButtonElement>): void; | ||
/** Click event callback */ | ||
onClick?(event: MouseEvent<HTMLButtonElement>): void; | ||
|
||
/** Type */ | ||
type?: 'button' | 'reset' | 'submit' | ||
/** Type */ | ||
type?: 'button' | 'reset' | 'submit'; | ||
} | ||
|
||
const Button: React.FC<Props> = ({ | ||
const Button = React.forwardRef<HTMLButtonElement, Props>((props, ref) => { | ||
const { | ||
children, | ||
to, | ||
theme = 'primary', | ||
className = '', | ||
disabled = false, | ||
onClick = undefined, | ||
type = 'button' | ||
}) => { | ||
// Sanity check for developers - make sure they do not specify `to` and `onClick` at once | ||
if (to && onClick) { | ||
console.warn('[Button] `onClick` property will not execute if you specify `to`. These are mutually exclusive.'); | ||
} | ||
|
||
if (to && !disabled) { | ||
return ( | ||
<Link role="button" to={to} className={`btn btn-${theme} ${className}`}>{children}</Link> | ||
); | ||
} | ||
type = 'button', | ||
} = props; | ||
|
||
// Sanity check for developers - make sure they do not specify `to` and `onClick` at once | ||
if (to && onClick) { | ||
console.warn( | ||
'[Button] `onClick` property will not execute if you specify `to`. These are mutually exclusive.' | ||
); | ||
} | ||
|
||
if (to && !disabled) { | ||
return ( | ||
<button type={type} disabled={disabled} | ||
className={`btn btn-${theme} ${className}`} | ||
onClick={!disabled ? onClick : undefined}> | ||
{children} | ||
</button> | ||
<Link | ||
ref={ref as unknown as Ref<HTMLAnchorElement>} | ||
role="button" | ||
to={to} | ||
className={`btn btn-${theme} ${className}`} | ||
> | ||
{children} | ||
</Link> | ||
); | ||
}; | ||
} | ||
|
||
return ( | ||
<button | ||
ref={ref} | ||
type={type} | ||
disabled={disabled} | ||
className={`btn btn-${theme} ${className}`} | ||
onClick={!disabled ? onClick : undefined} | ||
> | ||
{children} | ||
</button> | ||
); | ||
}); | ||
|
||
export default Button; |
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