import "./style.pcss"; import type { ButtonHTMLAttributes, MouseEvent, ReactElement, FC, } from "react"; import { useEffect, useState, } from "react"; import type { IBtn } from "./config/types"; import { constants } from "../../constants"; import type { IComponentBaseProps } from "../../types/components"; import { getExtraSizes } from "../../utils/getExtraSizes"; import { useCN } from "../../utils/react/useCN"; import { Icon } from "../icon/index"; const fallbackSize = constants.defaultSizes[2]; /** * Компонент кнопки * @returns {ReactElement} */ const Btn: FC = ({ baseClass = "btn", href, type = "button", label = "Кнопка", title, isDisabled = false, isOutline = false, isLight = false, extraClasses = {}, onClick, extraAttrs = {}, icon = null, iconPosition = "left", iconSize, iconType, iconStyle, children, size = fallbackSize, form, target, id, }): ReactElement => { const { getCN } = useCN(baseClass); const [ isDisabledState, setIsDisabledState ] = useState(isDisabled); const [ btnLabel, setBtnLabel ] = useState(label); const [ btnIcon, setBtnIcon ] = useState(icon); useEffect(() => { setBtnIcon(icon as string); }, [ icon ]); useEffect(() => { setIsDisabledState(isDisabled as boolean); }, [ isDisabled ]); useEffect(() => { setBtnLabel(label as string); }, [ label ]); const iconLayout = !!btnIcon && ; const labelLayout = !!btnLabel && {btnLabel}; const bodyLayout = children || ((iconPosition === "left") ? <>{iconLayout} {labelLayout} : <>{labelLayout} {iconLayout}); const classMods = getExtraSizes({ size, extraClasses: { isOutline: isLight ? false : isOutline, isLight: isOutline ? false : isLight, isOnlyIcon: !btnLabel, ...extraClasses, }, }); const onLinkClick = (event: MouseEvent) => { if (isDisabled) { event.preventDefault(); event.stopPropagation(); return; } if (onClick) { onClick(event); } }; const hasVisibleText = !!btnLabel || !!children; const computedAriaLabel = (!hasVisibleText && typeof title === "string" && title.length) ? title : undefined; const linkRole = href ? undefined : "button"; return type === "link" ? {bodyLayout} : ; }; export type { IBtn, }; export { Btn, };