File size: 3,551 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// @flow

import * as React from "react";
import cn from "classnames";
import { Icon } from "../";
import ButtonList from "./ButtonList.react";
import ButtonDropdown from "./ButtonDropdown.react";

import type { MouseEvents, PointerEvents } from "../../";

type PropsForAll = {|
  ...MouseEvents,
  ...PointerEvents,
  +size?: "sm" | "lg",
  +outline?: boolean,
  +link?: boolean,
  +block?: boolean,
  +className?: string,
  +children?: React.Node,
  +disabled?: boolean,
  +color?: string,
  +square?: boolean,
  +pill?: boolean,
  +icon?: string,
  +social?: string,
  +loading?: boolean,
  +tabIndex?: number,
  +isDropdownToggle?: boolean,
  +to?: string,
  +isOption?: boolean,
  +rootRef?: (?HTMLElement) => void,
|};

type DefaultButtonComponent = {|
  ...PropsForAll,
  +RootComponent?: "button",
  +type?: "button" | "submit" | "reset",
  +value?: string,
|};

type BtnAComponent = {|
  ...PropsForAll,
  +RootComponent: "a",
  +href?: string,
  +target?: string,
|};

type BtnInputComponent = {|
  ...PropsForAll,
  +RootComponent: "input",
  +type?: "button" | "submit" | "reset",
  +value?: string,
|};

export type Props = DefaultButtonComponent | BtnAComponent | BtnInputComponent;

const Button = (props: Props): React.Node => {
  const {
    size = "",
    outline,
    link,
    block,
    className,
    children,
    disabled,
    color = "",
    square,
    pill,
    icon,
    social = "",
    loading,
    tabIndex,
    isDropdownToggle,
    isOption,
    rootRef,
    to,
    onClick,
    onMouseEnter,
    onMouseLeave,
    onPointerEnter,
    onPointerLeave,
  } = props;

  const classes = cn(
    {
      btn: true,
      [`btn-${size}`]: !!size,
      [`btn-block`]: block,
      [`btn-outline-${color}`]: outline && !!color,
      [`btn-link`]: link,
      disabled: disabled,
      [`btn-${color}`]: !!color && !outline,
      [`btn-${social}`]: !!social,
      "btn-square": square,
      "btn-pill": pill,
      "btn-icon": !children,
      "btn-loading": loading,
      "dropdown-toggle": isDropdownToggle,
      "btn-option": isOption,
    },
    className
  );

  const propsForAll = {
    className: classes,
    disabled: disabled,
    onClick: onClick,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onPointerEnter: onPointerEnter,
    onPointerLeave: onPointerLeave,
    tabIndex: tabIndex,
  };

  const childrenForAll = (
    <React.Fragment>
      {social ? (
        <Icon name={social} prefix="fa" className={children ? "mr-2" : ""} />
      ) : icon ? (
        <Icon name={icon} className={children ? "mr-2" : ""} />
      ) : null}
      {children}
    </React.Fragment>
  );

  if (!props.RootComponent || props.RootComponent === "button") {
    const { type, value } = props;
    return (
      <button {...propsForAll} type={type} value={value} ref={rootRef}>
        {childrenForAll}
      </button>
    );
  } else if (props.RootComponent === "input") {
    const { type, value } = props;
    return <input {...propsForAll} type={type} value={value} ref={rootRef} />;
  } else if (props.RootComponent === "a") {
    const { href, target } = props;
    return (
      <a {...propsForAll} href={href} target={target} ref={rootRef}>
        {childrenForAll}
      </a>
    );
  } else {
    const Component: React.ElementType = props.RootComponent;
    return (
      <Component {...propsForAll} to={to}>
        {childrenForAll}
      </Component>
    );
  }
};

Button.List = ButtonList;
Button.Dropdown = ButtonDropdown;

Button.displayName = "Button";

export default Button;