| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import React, { useRef } from 'react';
-
- type Size = 'sm' | 'md' | 'lg' | 'xl';
- type Type = 'button' | 'submit' | 'reset';
- type TextTransform = 'uppercase' | 'capitalize';
- type MinWidth = 'auto' | 'none';
-
- interface ButtonProps {
- variant?: string;
- size?: Size;
- children?: React.ReactNode;
- authButton?: boolean;
- type?: Type;
- onClick?: () => void;
- textTransform?: TextTransform;
- className?: string;
- disabled?: boolean;
- hidden?: boolean;
- minWidth?: MinWidth;
- }
-
- const Button: React.FC<ButtonProps> = ({
- variant,
- size,
- children,
- authButton,
- type,
- onClick,
- textTransform,
- className,
- disabled,
- hidden,
- minWidth,
- ...restProps
- }) => {
- const buttonRef = useRef<HTMLButtonElement>(null);
-
- function styles() {
- let style = 'c-btn';
-
- if (variant) {
- style += ` c-btn--${variant}`;
- }
-
- if (size) {
- style += ` c-btn--${size}`;
- }
-
- if (textTransform) {
- style += ` c-btn--${textTransform}`;
- }
-
- if (authButton) {
- style += ` c-btn--auth`;
- }
-
- if (minWidth) {
- style += ` c-btn--${minWidth}`;
- }
-
- if (hidden) {
- style += ` c-btn--hidden`;
- }
-
- if (className) {
- style += ` ${className}`;
- }
-
- return style;
- }
-
- function handleClick() {
- if (buttonRef.current != null) {
- buttonRef.current.blur();
- }
-
-
-
- if (typeof onClick === 'function') {
- onClick();
- }
- }
-
- return (
- <button
- ref={buttonRef}
- className={styles()}
- onClick={handleClick}
- type={type}
- disabled={disabled}
- {...restProps}
- >
- {children}
- </button>
- );
- };
-
- Button.defaultProps = {
- type: 'button',
- };
-
- export default Button;
|