Guest User

Untitled

a guest
Jan 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import * as React from 'react';
  2. import StyledButton from './styled/StyledButton';
  3.  
  4. export interface ButtonProps {
  5. /** Callback function to be called when user clicks on button */
  6. onClick: () => void;
  7. children: React.ReactNode;
  8. /** Renders a disabled button and prevents onClick */
  9. disabled?: boolean;
  10. /** Primary Button */
  11. primary?: boolean;
  12. /** Secondary Button */
  13. secondary?: boolean;
  14. /** Tertiary Button */
  15. tertiary?: boolean;
  16. /** You Only Click Once (YOCO) */
  17. singleClick?: boolean;
  18. /** Label of the Button */
  19. label: string;
  20. /** Extra styles */
  21. style?: object;
  22. }
  23.  
  24. interface ButtonState {
  25. disableClick: boolean; // like this
  26. }
  27.  
  28. class Button extends React.Component<ButtonProps, ButtonState> {
  29. public static defaultProps: ButtonProps = {
  30. disabled: false,
  31. label: 'default',
  32. onClick: f => f,
  33. primary: false,
  34. secondary: false,
  35. singleClick: true,
  36. style: {},
  37. tertiary: true,
  38. };
  39. state: ButtonState = {
  40. disableClick: false,
  41. };
  42.  
  43. click = () => {
  44. if (this.state.disableClick && this.props.singleClick) {
  45. return;
  46. }
  47. this.props.onClick();
  48. this.setState(state => ({
  49. disableClick: true,
  50. }));
  51. }
  52.  
  53. render() {
  54. const { disabled, label } = this.props;
  55. return (
  56. <StyledButton {...this.props} onClick={!disabled ? this.click : f => f}>
  57. {label}
  58. </StyledButton>
  59. );
  60. }
  61. }
  62.  
  63. export default Button;
Add Comment
Please, Sign In to add comment