import React, { FunctionComponent, ReactNode } from 'react'; import { getClassName } from '../../helpers/getClassName'; import { classNames } from '../../lib/classNames'; import Tappable, { TappableProps } from '../Tappable/Tappable'; import Title from '../Typography/Title/Title'; import Text from '../Typography/Text/Text'; import Subhead from '../Typography/Subhead/Subhead'; import Caption from '../Typography/Caption/Caption'; import { HasAlign } from '../../types'; import { usePlatform } from '../../hooks/usePlatform'; import { withAdaptivity, AdaptivityProps, SizeType } from '../../hoc/withAdaptivity'; import { Platform, VKCOM } from '../../lib/platform'; export interface VKUIButtonProps extends HasAlign { mode?: 'primary' | 'secondary' | 'tertiary' | 'outline' | 'commerce' | 'destructive' | 'overlay_primary' | 'overlay_secondary' | 'overlay_outline'; size?: 's' | 'm' | 'l'; stretched?: boolean; before?: ReactNode; after?: ReactNode; } export interface ButtonProps extends Omit, VKUIButtonProps {} const getContent = (size: ButtonProps['size'], children: ButtonProps['children'], hasIcons: boolean, sizeY: AdaptivityProps['sizeY'], platform: Platform) => { switch (size) { case 'l': return ( sizeY === SizeType.COMPACT ? {children} : {children} ); case 'm': return ( sizeY === SizeType.COMPACT ? {children} : {children} ); case 's': default: if (hasIcons) { return ( {children} ); } return ( sizeY === SizeType.COMPACT ? {children} : {children} ); } }; const Button: FunctionComponent = (props: ButtonProps) => { const platform = usePlatform(); const { className, size, mode, stretched, align, children, before, after, getRootRef, Component, sizeY, ...restProps } = props; const hasIcons = Boolean(before || after); return
{before &&
{before}
} {children && getContent(size, children, hasIcons, sizeY, platform)} {after &&
{after}
}
; }; Button.defaultProps = { mode: 'primary', Component: 'button', align: 'center', size: 's', stretched: false, stopPropagation: true, }; export default withAdaptivity(Button, { sizeY: true, });