Skip to content

Instantly share code, notes, and snippets.

@alexdisdier
Last active March 6, 2020 05:38
Show Gist options
  • Save alexdisdier/e50564b17bf6c300d64b351ff7ca6a44 to your computer and use it in GitHub Desktop.
Save alexdisdier/e50564b17bf6c300d64b351ff7ca6a44 to your computer and use it in GitHub Desktop.
A simple react button in typescript using react-jss for styling
import * as React from 'react';
import useStyles from './button.style';
export type Props = {
children: React.ReactNode;
onClick?: () => void;
color?: string;
type?: string;
disabled?: boolean;
dataTestId?: string;
} & React.ButtonHTMLAttributes<HTMLButtonElement>;
const Button: React.FC<Props> = ({
children,
onClick = () => {},
disabled = false,
type = 'submit',
color = '',
dataTestId = 'button'
}) => {
const classes = useStyles({ color });
const handleClick = () => {
if (!disabled && onClick) onClick();
};
const rootProps = {
className: classes.root,
type,
onClick: handleClick,
disabled,
'data-testid': dataTestId
};
return (
<button {...rootProps}>
<span className={classes.label}>{children}</span>
</button>
);
};
export default Button;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment