Skip to content

Commit

Permalink
fix: changed the button props type to enum (#2760)
Browse files Browse the repository at this point in the history
  • Loading branch information
devilkiller-ag authored Mar 13, 2024
1 parent f0c2014 commit c5328a1
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 30 deletions.
34 changes: 18 additions & 16 deletions components/buttons/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Link from 'next/link';
import { twMerge } from 'tailwind-merge';

import { ButtonIconPosition, ButtonSize, ButtonType } from '@/types/components/buttons/ButtonPropsType';

type IButtonProps = {
text: string;
icon?: React.ReactNode;
iconPosition?: 'left' | 'right';
iconPosition?: ButtonIconPosition;
target?: string;
bgClassName?: string;
textClassName?: string;
buttonSize?: 'small' | 'default';
type?: 'submit' | 'reset' | 'button';
buttonSize?: ButtonSize;
type?: ButtonType;
} & (
| ({
href: string;
Expand All @@ -22,14 +24,14 @@ type IButtonProps = {
/**
* @name Button
* @param {string} props.text - The text to be displayed on the button.
* @param {string} props.type - The type of the button. Defaults to 'button'.
* @param {ButtonType} props.type - The type of the button. Defaults to 'button'.
* @param {string} props.target - The target attribute for the anchor tag. Defaults to '_self'.
* @param {React.ReactNode} props.icon - The icon to be displayed on the button.
* @param {string} props.iconPosition - The position of the icon. Defaults to 'right'.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon. Defaults to 'right'.
* @param {string} props.className - The class name to be applied to the button.
* @param {string} props.bgClassName - The class name to be applied to the button's background.
* @param {string} props.textClassName - The class name to be applied to the button's text.
* @param {string} props.buttonSize - The size of the button. Defaults to 'default'.
* @param {ButtonSize} props.buttonSize - The size of the button. Defaults to 'default'.
* @param {string} props.href - The href attribute for the anchor tag.
* @description The Button component is a reusable button component that can be used to render a button or an anchor tag
* @description The component accepts button or anchor tag props based on the presence of the href prop.
Expand All @@ -38,36 +40,36 @@ type IButtonProps = {
*/
export default function Button({
text,
type = 'button',
type = ButtonType.BUTTON,
target = '_self',
icon,
iconPosition = 'right',
iconPosition = ButtonIconPosition.RIGHT,
className,
bgClassName = twMerge('bg-primary-500 hover:bg-primary-400'),
textClassName = twMerge('text-white'),
buttonSize,
...props
}: IButtonProps): React.ReactElement {
const smallButtonClasses = twMerge(`${bgClassName} ${textClassName} transition-all duration-500
const smallButtonClasses = twMerge(`${bgClassName} ${textClassName} transition-all duration-500
ease-in-out rounded-md px-3 py-2 text-sm font-medium tracking-heading ${className || ''}`);
const classNames = twMerge(`${bgClassName} ${textClassName} transition-all duration-500 ease-in-out
const classNames = twMerge(`${bgClassName} ${textClassName} transition-all duration-500 ease-in-out
rounded-md px-4 py-3 text-md font-semibold tracking-heading ${className || ''}`);

if (!props.href) {
return (
<button
{...(props as React.ButtonHTMLAttributes<HTMLButtonElement>)}
type={type}
className={buttonSize === 'small' ? smallButtonClasses : classNames}
className={buttonSize === ButtonSize.SMALL ? smallButtonClasses : classNames}
data-testid='Button-main'
>
{icon && iconPosition === 'left' && (
{icon && iconPosition === ButtonIconPosition.LEFT && (
<span className='mr-2 inline-block' data-testid='Button-icon-left'>
{icon}
</span>
)}
<span className='inline-block'>{text}</span>
{icon && iconPosition === 'right' && (
{icon && iconPosition === ButtonIconPosition.RIGHT && (
<span className='ml-2 inline-block' data-testid='Button-icon-right'>
{icon}
</span>
Expand All @@ -82,12 +84,12 @@ export default function Button({
{...props}
target={target}
rel='noopener noreferrer'
className={buttonSize === 'small' ? smallButtonClasses : classNames}
className={buttonSize === ButtonSize.SMALL ? smallButtonClasses : classNames}
data-testid='Button-link'
>
{icon && iconPosition === 'left' && <span className='mr-2 inline-block'>{icon}</span>}
{icon && iconPosition === ButtonIconPosition.LEFT && <span className='mr-2 inline-block'>{icon}</span>}
<span className='inline-block'>{text}</span>
{icon && iconPosition === 'right' && <span className='ml-2 inline-block'>{icon}</span>}
{icon && iconPosition === ButtonIconPosition.RIGHT && <span className='ml-2 inline-block'>{icon}</span>}
</Link>
);
}
8 changes: 5 additions & 3 deletions components/buttons/GithubButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition, ButtonSize } from '@/types/components/buttons/ButtonPropsType';

import IconGithub from '../icons/Github';
import Button from './Button';
import type { IButtonDefaultProps } from './types';
Expand All @@ -13,14 +15,14 @@ interface IGithubButtonProps extends IButtonDefaultProps {
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function GithubButton({
text = 'githubButton',
href = 'https://github.com/asyncapi',
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className,
inNav
}: IGithubButtonProps) {
Expand All @@ -39,7 +41,7 @@ export default function GithubButton({
className={className}
data-testid='Github-button'
bgClassName='bg-gray-800 hover:bg-gray-700'
buttonSize={inNav ? 'small' : 'default'}
buttonSize={inNav ? ButtonSize.SMALL : ButtonSize.DEFAULT}
/>
);
}
6 changes: 4 additions & 2 deletions components/buttons/GoogleCalendarButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

import IconGoogleCalendar from '../icons/GoogleCalendar';
import Button from './Button';
import type { IButtonDefaultProps } from './types';
Expand All @@ -11,14 +13,14 @@ interface IGoogleCalendarButtonProps extends IButtonDefaultProps {}
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function GoogleCalendarButton({
text = 'googleCalendarBtn',
href,
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className
}: IGoogleCalendarButtonProps) {
// TODO: add this again when we have i18n
Expand Down
6 changes: 4 additions & 2 deletions components/buttons/ICSFileButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

import IconCalendar from '../icons/Calendar';
import Button from './Button';
// TODO: add this again when we have i18n
Expand All @@ -11,14 +13,14 @@ interface IICSFButtonProps extends IButtonDefaultProps {}
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function ICSFButton({
text = 'icsFileBtn',
href,
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className
}: IICSFButtonProps) {
// TODO: add this again when we have i18n
Expand Down
6 changes: 4 additions & 2 deletions components/buttons/SlackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

import IconSlack from '../icons/Slack';
import Button from './Button';
import type { IButtonDefaultProps } from './types';
Expand All @@ -9,14 +11,14 @@ interface ISlackButtonProps extends IButtonDefaultProps {}
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function SlackButton({
text = 'Join on Slack',
href = '/slack-invite',
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className
}: ISlackButtonProps) {
return (
Expand Down
6 changes: 4 additions & 2 deletions components/buttons/SubscribeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

import IconSubscribe from '../icons/Subscribe';
import Button from './Button';
// import { useTranslation } from '../../lib/i18n';
Expand All @@ -10,14 +12,14 @@ interface IGoogleCalendarButtonProps extends IButtonDefaultProps {}
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function GoogleCalendarButton({
text = 'subscribeBtn',
href,
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className
}: IGoogleCalendarButtonProps) {
// const { t } = useTranslation('common');
Expand Down
6 changes: 4 additions & 2 deletions components/buttons/YoutubeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

import IconYoutube from '../icons/YouTube';
import Button from './Button';
import type { IButtonDefaultProps } from './types';
Expand All @@ -9,14 +11,14 @@ interface IYoutubeButtonProps extends IButtonDefaultProps {}
* @param {string} props.text - The text to display on the button.
* @param {string} props.href - The href attribute for the anchor tag.
* @param {string} props.target - The target attribute for the anchor tag.
* @param {string} props.iconPosition - The position of the icon in the button.
* @param {ButtonIconPosition} props.iconPosition - The position of the icon in the button.
* @param {string} props.className - The class name to be applied to the button.
*/
export default function YoutubeButton({
text = 'Watch on YouTube',
href,
target = '_blank',
iconPosition = 'left',
iconPosition = ButtonIconPosition.LEFT,
className
}: IYoutubeButtonProps) {
return (
Expand Down
4 changes: 3 additions & 1 deletion components/buttons/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { HTMLAttributeAnchorTarget } from 'react';

import type { ButtonIconPosition } from '@/types/components/buttons/ButtonPropsType';

export interface IButtonDefaultProps {
text?: string;
href?: string;
target?: HTMLAttributeAnchorTarget;
iconPosition?: 'left' | 'right';
iconPosition?: ButtonIconPosition;
className?: string;
}
15 changes: 15 additions & 0 deletions types/components/buttons/ButtonPropsType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export enum ButtonSize {
SMALL = 'small',
DEFAULT = 'default',
}

export enum ButtonType {
SUBMIT = 'submit',
RESET = 'reset',
BUTTON = 'button',
}

export enum ButtonIconPosition {
LEFT = 'left',
RIGHT = 'right',
}

0 comments on commit c5328a1

Please sign in to comment.