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
|
import cx from 'clsx';
import * as React from 'react';
import s0 from './Button.module.scss';
import { LoadingDot } from './shared/Basic';
const { forwardRef, useCallback } = React;
type ButtonInternalProps = {
children?: React.ReactNode;
label?: string;
text?: string;
start?: React.ReactNode | (() => React.ReactNode);
};
type ButtonProps = {
isLoading?: boolean;
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => unknown;
disabled?: boolean;
kind?: 'primary' | 'minimal';
className?: string;
title?: string;
} & ButtonInternalProps;
function Button(props: ButtonProps, ref: React.Ref<HTMLButtonElement>) {
const {
onClick,
disabled = false,
isLoading,
kind = 'primary',
className,
children,
label,
text,
start,
...restProps
} = props;
const internalProps = { children, label, text, start };
const internalOnClick = useCallback(
(e) => {
if (isLoading) return;
onClick && onClick(e);
},
[isLoading, onClick]
);
const btnClassName = cx(
s0.btn,
{ [s0.minimal]: kind === 'minimal' },
className
);
return (
<button
className={btnClassName}
ref={ref}
onClick={internalOnClick}
disabled={disabled}
{...restProps}
>
{isLoading ? (
<>
<span style={{ display: 'inline-flex', opacity: 0 }}>
<ButtonInternal {...internalProps} />
</span>
<span className={s0.loadingContainer}>
<LoadingDot />
</span>
</>
) : (
<ButtonInternal {...internalProps} />
)}
</button>
);
}
function ButtonInternal({ children, label, text, start }: ButtonInternalProps) {
return (
<>
{start ? (
<span className={s0.btnStart}>
{typeof start === 'function' ? start() : start}
</span>
) : null}
{children || label || text}
</>
);
}
export default forwardRef(Button);
|