import React, { createRef, forwardRef, memo, ButtonHTMLAttributes, ReactElement, } from 'react'; import styleProps from '@techytools/ui/types/styles'; import variants from '@techytools/ui/components/Button/styles'; import IconLoading from '@ant-design/icons/LoadingOutlined'; import withCombinedProps from '@techytools/ui/hooks/withCombinedProps'; import withRipple from './withRipple'; import withStyles from '@techytools/ui/hooks/withStyles'; export type Props = { /** * Used to set padding/fontSize/height/line-height. */ size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'; /** * If true, will have very rounded corners like a "pill" or "circle". */ round?: boolean; /** * Pass true to use default loading animation. Or pass a custom loading animation component. * If button has children, loading animation will play on top of the children. * This way, if loading prop is dynamic (after user clicked submit), button size will not change. */ loading?: boolean; /** * React component to displayed to the left of the text. */ icon?: ReactElement; /** * React component to displayed to the right of the text. */ suffix?: ReactElement; /** * Alternative to `props.children`, will overwrite `props.children`. */ value?: ReactElement | string | number; } & styleProps & ButtonHTMLAttributes; /** * Button. Pass variant such as "primary", "outlined", "cancel", or "disabled" */ export const Component = (props: Props, ref: any) => { /* * Props logic */ let loading: React.ReactElement | null = null; if (props.loading) { if (props.loading === true) { // if props.loading is a boolean, show default loading animation loading = ; } else { // if props.loading is a React component, use that as loading animation loading = props.loading; } } const Children: any = []; // icon on the left if (props.icon) { Children.push( {props.icon} ); } if (props.icon && props.children) { Children.push( {' '} ); } // content if (props.value || props.children) { Children.push( {props.value || props.children} ); } if (loading) { Children.push( {loading} ); } // icon on the right if (props.suffix && props.children) { Children.push( {' '} ); } if (props.suffix) { Children.push( {props.suffix} ); } if (!ref) { ref = createRef(); } const ChildrenWithRipple = withRipple({ children: Children }); return ( ); }; /* * (1) default export is normal component ready to use (2) withButton is HOC used to predefine common props */ const Styled: React.FC = withStyles( forwardRef(Component), 'Button', variants ); export default memo(Styled); export const withButton = (props: Props) => memo(withCombinedProps(Styled, props));