aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/components/button/ButtonRowV2.tsx
blob: 5129e7f1be10685bdd44761592051a3da8977afd (plain)
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
import { ComponentPropsWithoutRef, Ref } from "react";
import classNames from "classnames";

import { Text, ThemeColor } from "../common";

import Button from "./Button";
import FlatButton from "./FlatButton";
import IconButton from "./IconButton";
import LoadingButton from "./LoadingButton";

import "./ButtonRow.css";

interface ButtonRowV2ButtonBase {
  key: string | number;
  action?: "primary" | "secondary";
  color?: ThemeColor;
  disabled?: boolean;
  onClick?: () => void;
}

interface ButtonRowV2ButtonWithNoType extends ButtonRowV2ButtonBase {
  type?: undefined | null;
  text: Text;
  outline?: boolean;
  props?: ComponentPropsWithoutRef<typeof Button>;
}

interface ButtonRowV2NormalButton extends ButtonRowV2ButtonBase {
  type: "normal";
  text: Text;
  outline?: boolean;
  props?: ComponentPropsWithoutRef<typeof Button>;
}

interface ButtonRowV2FlatButton extends ButtonRowV2ButtonBase {
  type: "flat";
  text: Text;
  props?: ComponentPropsWithoutRef<typeof FlatButton>;
}

interface ButtonRowV2IconButton extends ButtonRowV2ButtonBase {
  type: "icon";
  icon: string;
  props?: ComponentPropsWithoutRef<typeof IconButton>;
}

interface ButtonRowV2LoadingButton extends ButtonRowV2ButtonBase {
  type: "loading";
  text: Text;
  loading?: boolean;
  props?: ComponentPropsWithoutRef<typeof LoadingButton>;
}

type ButtonRowV2Button =
  | ButtonRowV2ButtonWithNoType
  | ButtonRowV2NormalButton
  | ButtonRowV2FlatButton
  | ButtonRowV2IconButton
  | ButtonRowV2LoadingButton;

interface ButtonRowV2Props {
  className?: string;
  containerRef?: Ref<HTMLDivElement>;
  buttons: ButtonRowV2Button[];
  buttonsClassName?: string;
}

export default function ButtonRowV2({
  className,
  containerRef,
  buttons,
  buttonsClassName,
}: ButtonRowV2Props) {
  return (
    <div ref={containerRef} className={classNames("cru-button-row", className)}>
      {buttons.map((button) => {
        const { key, action, color, disabled, onClick } = button;

        const realAction = action ?? "primary";
        const realColor =
          color ?? (realAction === "primary" ? "primary" : "secondary");

        const commonProps = { key, color: realColor, disabled, onClick };
        const newClassName = classNames(
          button.props?.className,
          buttonsClassName,
        );

        switch (button.type) {
          case null:
          case undefined:
          case "normal": {
            const { text, outline, props } = button;
            return (
              <Button
                {...commonProps}
                text={text}
                outline={outline ?? realAction !== "primary"}
                {...props}
                className={newClassName}
              />
            );
          }
          case "flat": {
            const { text, props } = button;
            return (
              <FlatButton
                {...commonProps}
                text={text}
                {...props}
                className={newClassName}
              />
            );
          }
          case "icon": {
            const { icon, props } = button;
            return (
              <IconButton
                {...commonProps}
                icon={icon}
                {...props}
                className={newClassName}
              />
            );
          }
          case "loading": {
            const { text, loading, props } = button;
            return (
              <LoadingButton
                {...commonProps}
                text={text}
                loading={loading}
                {...props}
                className={newClassName}
              />
            );
          }
          default:
            throw new Error();
        }
      })}
    </div>
  );
}