|
| 1 | +import type { IconType } from "react-icons/lib" |
| 2 | + |
| 3 | +import { cn } from "@/lib/utils/cn" |
| 4 | + |
| 5 | +import { Stack } from "../flex" |
| 6 | + |
| 7 | +import { |
| 8 | + Button, |
| 9 | + ButtonLink, |
| 10 | + type ButtonLinkProps, |
| 11 | + type ButtonProps, |
| 12 | +} from "./Button" |
| 13 | + |
| 14 | +type CommonProps = { |
| 15 | + icon: IconType |
| 16 | + iconAlignment?: "left" | "right" | "start" | "end" |
| 17 | + /** |
| 18 | + * Reduced choices of the button variant. |
| 19 | + * |
| 20 | + * This component only accepts the `solid` or `outline` variant |
| 21 | + */ |
| 22 | + variant?: "solid" | "outline" |
| 23 | + /** |
| 24 | + * Reduced choices of the button size |
| 25 | + * |
| 26 | + * This component only accepts the `md` or `sm` sizes |
| 27 | + */ |
| 28 | + size?: "md" | "sm" |
| 29 | + mainText: string |
| 30 | + helperText: string |
| 31 | + /** |
| 32 | + * Should the main text be below the helper text instead of ab? |
| 33 | + */ |
| 34 | + reverseTextOrder?: boolean |
| 35 | +} |
| 36 | + |
| 37 | +type OmittedTypes = "variant" | "size" | "children" |
| 38 | + |
| 39 | +type ButtonTypeProps = CommonProps & |
| 40 | + Omit<ButtonProps, OmittedTypes> & { |
| 41 | + componentType: "button" |
| 42 | + } |
| 43 | + |
| 44 | +type ButtonLinkTypeProps = CommonProps & |
| 45 | + Omit<ButtonLinkProps, OmittedTypes> & { |
| 46 | + componentType: "link" |
| 47 | + } |
| 48 | + |
| 49 | +type ButtonTwoLinesProps = ButtonTypeProps | ButtonLinkTypeProps |
| 50 | + |
| 51 | +const ButtonTwoLines = ({ |
| 52 | + iconAlignment = "start", |
| 53 | + className, |
| 54 | + ...props |
| 55 | +}: ButtonTwoLinesProps) => { |
| 56 | + const isIconLeft = ["left", "start"].includes(iconAlignment) |
| 57 | + |
| 58 | + const commonClassStyles = cn( |
| 59 | + isIconLeft ? "text-start justify-start" : "text-end justify-end", |
| 60 | + className |
| 61 | + ) |
| 62 | + |
| 63 | + if (props.componentType === "link") { |
| 64 | + return ( |
| 65 | + <ButtonLink className={commonClassStyles} {...props}> |
| 66 | + <ChildContent {...props} isIconLeft={isIconLeft} /> |
| 67 | + </ButtonLink> |
| 68 | + ) |
| 69 | + } |
| 70 | + return ( |
| 71 | + <Button className={commonClassStyles} {...props}> |
| 72 | + <ChildContent {...props} isIconLeft={isIconLeft} /> |
| 73 | + </Button> |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +export default ButtonTwoLines |
| 78 | + |
| 79 | +const ChildContent = ( |
| 80 | + props: Omit<ButtonTwoLinesProps, "iconAlignment"> & { isIconLeft: boolean } |
| 81 | +) => { |
| 82 | + const { |
| 83 | + reverseTextOrder = false, |
| 84 | + size = "md", |
| 85 | + mainText, |
| 86 | + helperText, |
| 87 | + icon: Icon, |
| 88 | + isIconLeft, |
| 89 | + isSecondary, |
| 90 | + variant, |
| 91 | + } = props |
| 92 | + |
| 93 | + const ButtonIcon = () => ( |
| 94 | + <Icon |
| 95 | + // TODO: Text size here should not be marked important after migration |
| 96 | + className="!text-2xl" |
| 97 | + /> |
| 98 | + ) |
| 99 | + return ( |
| 100 | + <> |
| 101 | + {isIconLeft && <ButtonIcon />} |
| 102 | + <Stack |
| 103 | + className={cn( |
| 104 | + "gap-0", |
| 105 | + reverseTextOrder ? "flex-col-reverse" : "flex-col" |
| 106 | + )} |
| 107 | + > |
| 108 | + <span |
| 109 | + className={cn("text-md", size === "md" ? "text-bold" : "text-normal")} |
| 110 | + > |
| 111 | + {mainText} |
| 112 | + </span> |
| 113 | + <span |
| 114 | + className={cn( |
| 115 | + "text-xs", |
| 116 | + variant === "outline" || isSecondary |
| 117 | + ? "text-body-medium" |
| 118 | + : undefined |
| 119 | + )} |
| 120 | + > |
| 121 | + {helperText} |
| 122 | + </span> |
| 123 | + </Stack> |
| 124 | + {!isIconLeft && <ButtonIcon />} |
| 125 | + </> |
| 126 | + ) |
| 127 | +} |
0 commit comments