Skip to content

Commit eb19fde

Browse files
committed
feat: add loading state
1 parent b72e07a commit eb19fde

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

src/Shared/Components/Button/Button.component.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { Progressing } from '@Common/Progressing'
12
import { ComponentSizeType } from '@Shared/constants'
23
import { ButtonProps, ButtonStyleType, ButtonVariantType } from './types'
3-
import { BUTTON_SIZE_TO_CLASS_NAME_MAP, BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP } from './constants'
4+
import { BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP, BUTTON_SIZE_TO_LOADER_SIZE_MAP } from './constants'
5+
import { getButtonDerivedClass } from './utils'
46
import './button.scss'
57

6-
export const getButtonDerivedClass = ({ size, variant, style }: Pick<ButtonProps, 'variant' | 'size' | 'style'>) =>
7-
`button button__${ButtonVariantType[variant]}--${ButtonStyleType[style]} ${BUTTON_SIZE_TO_CLASS_NAME_MAP[size]}`
8-
98
const Button = ({
109
buttonProps = {},
1110
variant = ButtonVariantType.primary,
@@ -15,20 +14,23 @@ const Button = ({
1514
startIcon,
1615
endIcon,
1716
disabled,
17+
isLoading,
1818
}: ButtonProps) => {
19-
const iconClass = `dc__no-shrink flex dc__fill-available-space ${BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP[size]}`
19+
const visibilityClass = isLoading ? 'dc__visibility-hidden' : ''
20+
const iconClass = `dc__no-shrink flex dc__fill-available-space ${BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP[size]} ${visibilityClass}`
2021

2122
return (
2223
<button
2324
{...buttonProps}
24-
disabled={disabled}
25+
disabled={disabled || isLoading}
2526
// eslint-disable-next-line react/button-has-type
2627
type={buttonProps.type || 'button'}
27-
className={`br-4 flex cursor dc__mnw-100 dc__tab-focus ${getButtonDerivedClass({ size, variant, style })} ${disabled ? 'dc__disabled' : ''} ${buttonProps.className || ''}`}
28+
className={`br-4 flex cursor dc__mnw-100 dc__tab-focus dc__position-rel dc__capitalize ${getButtonDerivedClass({ size, variant, style })} ${disabled ? 'dc__disabled' : ''} ${buttonProps.className || ''}`}
2829
>
2930
{startIcon && <span className={iconClass}>{startIcon}</span>}
30-
<span className="dc__mxw-150 dc__align-left dc__truncate">{text}</span>
31+
<span className={`dc__mxw-150 dc__align-left dc__truncate ${visibilityClass}`}>{text}</span>
3132
{endIcon && <span className={iconClass}>{endIcon}</span>}
33+
{isLoading && <Progressing size={BUTTON_SIZE_TO_LOADER_SIZE_MAP[size]} />}
3234
</button>
3335
)
3436
}

src/Shared/Components/Button/button.scss

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
border: 1px solid $border-color;
55

66
svg,
7-
svg>* {
7+
svg * {
88
stroke: $text-color;
99
}
10+
11+
// Custom state for loader
12+
.loader {
13+
position: absolute;
14+
15+
svg,
16+
svg * {
17+
fill: $text-color;
18+
stroke: none;
19+
}
20+
}
1021
}
1122

1223
@mixin pseudo-states($hover-bg-color, $active-bg-color) {
@@ -27,8 +38,6 @@
2738
color: inherit;
2839
border: none;
2940
outline: inherit;
30-
text-decoration: none;
31-
text-transform: capitalize;
3241

3342
&__primary {
3443
$border-color: transparent;
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ComponentSizeType } from '@Shared/constants'
2+
import { ProgressingProps } from '@Common/Types'
23
import { ButtonProps } from './types'
34

45
export const BUTTON_SIZE_TO_CLASS_NAME_MAP: Record<ButtonProps['size'], string> = {
@@ -7,12 +8,20 @@ export const BUTTON_SIZE_TO_CLASS_NAME_MAP: Record<ButtonProps['size'], string>
78
[ComponentSizeType.medium]: 'px-11 py-5 fs-13 lh-20 fw-6 dc__gap-8',
89
[ComponentSizeType.large]: 'px-13 py-7 fs-13 lh-20 fw-6 dc__gap-10',
910
[ComponentSizeType.xl]: 'px-15 py-9 fs-14 lh-20 fw-6 dc__gap-12',
10-
}
11+
} as const
1112

1213
export const BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP: Record<ButtonProps['size'], string> = {
1314
[ComponentSizeType.xs]: 'icon-dim-12',
1415
[ComponentSizeType.small]: 'icon-dim-12',
1516
[ComponentSizeType.medium]: 'icon-dim-16',
1617
[ComponentSizeType.large]: 'icon-dim-16',
1718
[ComponentSizeType.xl]: 'icon-dim-20',
18-
}
19+
} as const
20+
21+
export const BUTTON_SIZE_TO_LOADER_SIZE_MAP: Record<ButtonProps['size'], ProgressingProps['size']> = {
22+
[ComponentSizeType.xs]: 12,
23+
[ComponentSizeType.small]: 12,
24+
[ComponentSizeType.medium]: 16,
25+
[ComponentSizeType.large]: 16,
26+
[ComponentSizeType.xl]: 20,
27+
} as const

src/Shared/Components/Button/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { BUTTON_SIZE_TO_CLASS_NAME_MAP } from './constants'
2+
import { ButtonProps, ButtonStyleType, ButtonVariantType } from './types'
3+
4+
export const getButtonDerivedClass = ({ size, variant, style }: Pick<ButtonProps, 'variant' | 'size' | 'style'>) =>
5+
`button button__${ButtonVariantType[variant]}--${ButtonStyleType[style]} ${BUTTON_SIZE_TO_CLASS_NAME_MAP[size]}`

0 commit comments

Comments
 (0)