Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Button.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React, { useRef } from 'react';
  2. import PropType from 'prop-types';
  3. const Button = ({
  4. variant,
  5. size,
  6. children,
  7. authButton,
  8. type,
  9. onClick,
  10. textTransform,
  11. className,
  12. disabled,
  13. hidden,
  14. minWidth,
  15. ...restProps
  16. }) => {
  17. const buttonRef = useRef(null);
  18. function styles() {
  19. let style = 'c-btn';
  20. if (variant) {
  21. style += ` c-btn--${variant}`;
  22. }
  23. if (size) {
  24. style += ` c-btn--${size}`;
  25. }
  26. if (textTransform) {
  27. style += ` c-btn--${textTransform}`;
  28. }
  29. if (authButton) {
  30. style += ` c-btn--auth`;
  31. }
  32. if (minWidth) {
  33. style += ` c-btn--${minWidth}`;
  34. }
  35. if (hidden) {
  36. style += ` c-btn--hidden`;
  37. }
  38. if (className) {
  39. style += ` ${className}`;
  40. }
  41. return style;
  42. }
  43. function handleClick() {
  44. buttonRef.current.blur();
  45. if (typeof onClick === 'function') {
  46. onClick();
  47. }
  48. }
  49. return (
  50. <button
  51. ref={buttonRef}
  52. className={styles()}
  53. onClick={handleClick}
  54. type={type}
  55. disabled={disabled}
  56. {...restProps}
  57. >
  58. {children}
  59. </button>
  60. );
  61. };
  62. Button.propTypes = {
  63. children: PropType.node,
  64. textTransform: PropType.oneOf(['uppercase', 'capitalize']),
  65. size: PropType.oneOf(['sm', 'md', 'lg', 'xl']),
  66. authButton: PropType.bool,
  67. variant: PropType.string,
  68. type: PropType.oneOf(['button', 'submit', 'reset']),
  69. onClick: PropType.func,
  70. className: PropType.string,
  71. disabled: PropType.bool,
  72. minWidth: PropType.oneOf(['auto']),
  73. hidden: PropType.bool,
  74. };
  75. Button.defaultProps = {
  76. type: 'button',
  77. };
  78. export default Button;