import cn from 'classnames'; import { Counter } from '@snack-uikit/counter'; import { Sun } from '@snack-uikit/loaders'; import { WithSupportProps } from '@snack-uikit/utils'; import { ICON_POSITION } from '../../constants'; import { ButtonPrivateProps } from './ButtonPrivate'; import { Variant } from './constants'; import styles from './styles.module.scss'; type GetVariantProps = Pick; export function getVariant({ label, icon, iconPosition }: GetVariantProps) { if (label && icon && iconPosition === ICON_POSITION.After) { return Variant.IconAfter; } if (label && icon && iconPosition === ICON_POSITION.Before) { return Variant.IconBefore; } if (label) { return Variant.LabelOnly; } return Variant.IconOnly; } type GetWrappedCounterProps = Pick & WithSupportProps<{ counterForIcon: boolean | undefined; }>; type WrappedCounterProp = { wrappedCounter?: JSX.Element | undefined; }; export function getWrappedCounter({ counter, loading, disabled, counterForIcon, 'data-test-id': testId, }: GetWrappedCounterProps) { return typeof counter?.value === 'number' && !loading && !disabled ? ( ) : undefined; } type GetWrappedIconProps = Pick & WrappedCounterProp; export function getWrappedIcon({ icon, iconClassName, loading, wrappedCounter }: GetWrappedIconProps) { if (loading) { return ( ); } if (icon) { return ( {icon} {wrappedCounter} ); } return undefined; } type GetWrappedLabelProps = Pick & WrappedCounterProp; export function getWrappedLabel({ label, labelClassName, wrappedCounter }: GetWrappedLabelProps) { return label ? ( {label} {wrappedCounter} ) : undefined; } type GetChildrenProps = Pick< ButtonPrivateProps, | 'icon' | 'label' | 'iconPosition' | 'iconClassName' | 'labelClassName' | 'loading' | 'disabled' | 'counter' | 'data-test-id' >; export function getChildren({ icon, label, iconPosition, iconClassName, labelClassName, loading, disabled, counter, 'data-test-id': testId, }: GetChildrenProps) { const counterForIcon = icon && (iconPosition === ICON_POSITION.After || !label); const wrappedCounter = getWrappedCounter({ counter, loading, disabled, counterForIcon, 'data-test-id': testId }); const wrappedIcon = getWrappedIcon({ icon, iconClassName, loading, wrappedCounter: counterForIcon ? wrappedCounter : undefined, }); const wrappedLabel = getWrappedLabel({ label, labelClassName, wrappedCounter: counterForIcon ? undefined : wrappedCounter, }); switch (iconPosition) { case ICON_POSITION.Before: { return ( <> {wrappedIcon} {wrappedLabel} ); } case ICON_POSITION.After: default: { return ( <> {wrappedLabel} {wrappedIcon} ); } } }