Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create Stat component and stories #11574

Merged
merged 12 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/components/Stat/Stat.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Meta, StoryObj } from "@storybook/react"

import StatComponent from "."

const meta = {
title: "Molecules / Display Content / Stat",
component: StatComponent,
} satisfies Meta<typeof StatComponent>

export default meta

export const Stat: StoryObj<typeof meta> = {
args: {
tooltipProps: {
content: "Tooltip content",
},
isError: false,
label: "Label",
value: "000",
},
argTypes: {
isError: {
table: {
disable: true,
},
},
},
}
wackerow marked this conversation as resolved.
Show resolved Hide resolved

export const StatError: StoryObj<typeof meta> = {
args: {
...Stat.args,
isError: true,
value: undefined,
},
argTypes: {
value: {
table: {
disable: true,
},
},
},
}
58 changes: 58 additions & 0 deletions src/components/Stat/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useState } from "react"
import type { IconType } from "react-icons/lib"
import { MdInfoOutline, MdWarning } from "react-icons/md"
import { Flex, HStack, Icon, Text } from "@chakra-ui/react"

import { NULL_VALUE } from "@/lib/constants"

import Tooltip, { type TooltipProps } from "../Tooltip"

const initialContent = {
contentValue: NULL_VALUE,
tooltipIcon: MdInfoOutline,
}

export type StatProps = {
tooltipProps?: TooltipProps
value: string | undefined
label: string
isError: boolean
}

const Stat = ({ tooltipProps, value, label, isError }: StatProps) => {
const [content, setContent] = useState<{
contentValue: string | JSX.Element
tooltipIcon: IconType
}>(initialContent)

useEffect(() => {
if (isError) {
return setContent({
contentValue: NULL_VALUE,
tooltipIcon: MdWarning,
})
}

if (!value) return

return setContent({ ...initialContent, contentValue: value })
}, [isError, value])

return (
<Flex flexDirection="column-reverse">
<HStack lineHeight="initial" spacing="0.5" color="body.medium">
<Text as="span">{label}</Text>
{!!tooltipProps && (
<Tooltip {...tooltipProps}>
<Icon as={content.tooltipIcon} />
</Tooltip>
)}
</HStack>
<Text as="span" size="5xl" fontWeight="bold">
{content.contentValue}
</Text>
</Flex>
)
}

export default Stat
6 changes: 3 additions & 3 deletions src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import {

import { isMobile } from "@/lib/utils/isMobile"

export interface IProps extends PopoverProps {
export interface TooltipProps extends PopoverProps {
content: ReactNode
children?: ReactNode
onBeforeOpen?: () => void
}

const Tooltip: React.FC<IProps> = ({
const Tooltip = ({
content,
children,
onBeforeOpen,
...rest
}) => {
}: TooltipProps) => {
const { isOpen, onOpen, onClose } = useDisclosure()

// Close the popover when the user scrolls.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const TRANSLATED_IMAGES_DIR = "/content/translations"
export const PLACEHOLDER_IMAGE_DIR = "src/data/placeholders"
export const INTL_JSON_DIR = "src/intl"

export const NULL_VALUE = "—"

// i18n
export const DEFAULT_LOCALE = "en"
export const FAKE_LOCALE = "default"
Expand Down
Loading