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: creating a price #10

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { createStyles } from "@travelmakers-design-v2/styles";
import { Props } from "./Price";

export default createStyles((theme, { type }: Pick<Props, "type">) => {
return {
root: {
display: "flex",
flexDirection: "row",
alignItems: "center",
width: "100%",
},

percentText: {
...theme.typography.display6,
fontWeight: "700",
width: "39px",
color: theme.colors.error,
},
nightText: {
...theme.typography.body3,
fontWeight: "700",
textAlign: "right",
width: "28px",
color: theme.colors.primary1,
},
priceText: {
...theme.typography.display4,
fontWeight: "700",
color: theme.colors.primary1,
marginLeft: theme.spacing.spacing5,
},
priceBeforeText: {
...theme.typography.body2,
fontWeight: "700",
color: theme.colors.primary1,
},
priceStartText: {
...theme.typography.body3,
fontWeight: "400",
color: theme.colors.primary2,
marginLeft: theme.spacing.spacing5,
},

// NOTE: Type is 'secondary'
labelSecondary: {
...theme.typography.body3,
fontWeight: "400",
color: theme.colors.primary1,
marginRight: theme.spacing.spacing5,
},
nightSecondaryText: {
...theme.typography.body3,
fontWeight: "700",
color: theme.colors.primary1,
textAlign: "right",
marginRight: theme.spacing.spacing5,
},
priceSecondaryText: {
...theme.typography.body3,
fontWeight: "400",
color: theme.colors.primary2,
},
priceBeforeSecondaryText: {
...theme.typography.body3,
fontWeight: "400",
color: theme.colors.primary2,
},
couponWrap: {
display: "flex",
alignItems: "center",
width: "74px",
background: theme.colors.secondaryContainer,
borderRadius: theme.spacing.spacing5,
marginLeft: theme.spacing.spacing5,
},
couponWord: {
...theme.typography.caption,
fontWeight: "700",
color: theme.colors.secondary1,
},
};
});
123 changes: 123 additions & 0 deletions packages/travelmakers-design-core/src/components/Price/Price.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { PolymorphicRef } from "@travelmakers-design-v2/styles";
import { forwardRef, PropsWithChildren } from "react";
import { View } from "../View";
import useStyles from "./Price.style";
import { PriceProps, PriceComponent } from "./Price.type";
import { Icon } from "../Icon";

export interface Props {
/** Price 컴포넌트의 타입을 정합니다. */
type?: "primary" | "secondary";

/** (secondary type 한정) Price 컴포넌트의 레이블을 표시합니다. */
label?: string;

/** Price 컴포넌트의 할인율을 표시합니다. */
percentText?: number;

/** Price 컴포넌트의 1박을 표시합니다. */
nightText?: number;

/** Price 컴포넌트의 가격을 표시합니다. */
priceText?: number;

/** Price 컴포넌트의 시작가격을 표시합니다. */
priceStartText?: string;

/** (secondary type 한정) Price 컴포넌트의 쿠폰 표시여부를 결정합니다. */
isCoupon?: boolean;
}

export const Price: PriceComponent & {
displayName?: string;
} = forwardRef(
<C extends React.ElementType = "div">(
{
type = "primary",
label,
percentText,
nightText,
priceText,
priceStartText,
isCoupon = true,
className,
children,
...props
}: PropsWithChildren<PriceProps<C>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4: children은 사용하지 않으니 다 제거해주면 좋을 것 같네용

ref: PolymorphicRef<C>
) => {
const { classes, cx } = useStyles({ type });

const Primary = () => {
return (
<View<React.ElementType>
component={"div"}
ref={ref}
className={cx(classes.root, className)}
{...props}
>
{percentText && (
<span className={cx(classes.percentText)}>{percentText}%</span>
)}
{nightText && (
<span className={cx(classes.nightText)}>{nightText}박</span>
)}
{priceText && (
<>
<span className={cx(classes.priceText)}>
{priceText.toLocaleString("ko")}
</span>
<span className={cx(classes.priceBeforeText)}>원~</span>
</>
)}
{priceStartText && (
<span className={cx(classes.priceStartText)}>
| {priceStartText}
</span>
)}
</View>
);
};

const Secondary = () => {
return (
<View<React.ElementType>
component={"div"}
ref={ref}
className={cx(classes.root, className)}
{...props}
>
{label && <span className={cx(classes.labelSecondary)}>{label}</span>}
{nightText && (
<span className={cx(classes.nightSecondaryText)}>
{nightText}박
</span>
)}
{priceText && (
<>
<span className={cx(classes.priceSecondaryText)}>
{priceText?.toLocaleString("ko")}
</span>
<span className={cx(classes.priceBeforeSecondaryText)}>원</span>
</>
)}
{isCoupon && (
<div className={cx(classes.couponWrap)}>
<Icon
src="IcDiscount"
width={16}
height={16}
style={{ margin: "0 4px" }}
/>
<span className={cx(classes.couponWord)}>쿠폰 적용가</span>
</div>
)}
</View>
);
};

return type === "primary" ? <Primary /> : <Secondary />;
}
);

Price.displayName = "Price";
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
ClassNames,
PolymorphicComponentProps,
TmComponentProps,
} from "@travelmakers-design-v2/styles";
import { Props } from "./Price";
import useStyles from "./Price.style";

type PriceStylesNames = ClassNames<typeof useStyles>;

interface SharedPriceProps extends Props, TmComponentProps<PriceStylesNames> {}

export type PriceProps<C extends React.ElementType> = PolymorphicComponentProps<
C,
SharedPriceProps
>;

export type PriceComponent = <C extends React.ElementType = "div">(
props: PriceProps<C>
) => React.ReactElement;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Price } from "./Price";
export type { PriceProps } from "./Price.type";
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React from "react";
import { Price } from "../Price";

export default {
title: "@travelmakers-design-v2/core/General/Price",
component: Price,
argTypes: {
type: {
defaultValue: "primary",
description: "Price 컴포넌트의 타입을 정합니다.",
options: ["primary", "secondary"],
control: { type: "inline-radio" },
},
label: {
defaultValue: "",
description: "(secondary type 한정)Price 컴포넌트의 레이블을 표시합니다.",
table: {
type: {
summary: "string",
},
},
control: { type: "text" },
},
percentText: {
defaultValue: 70,
description: "Price 컴포넌트의 할인율을 표시합니다.",
table: {
type: {
summary: "number",
},
},
control: { type: "number" },
},
nightText: {
defaultValue: 1,
description: "Price 컴포넌트의 1박을 표시합니다.",
table: {
type: {
summary: "number",
},
},
control: { type: "number" },
},
priceText: {
defaultValue: 10000,
description: "Price 컴포넌트의 가격을 표시합니다.",
table: {
type: {
summary: "number",
},
},
control: { type: "number" },
},
priceStartText: {
defaultValue: "1박 100,000원부터",
description: "Price 컴포넌트의 시작가격을 표시합니다.",
table: {
type: {
summary: "string",
},
},
control: { type: "text" },
},
isCoupon: {
defaultValue: true,
description:
" (secondary type 한정) Price 컴포넌트의 쿠폰 표시여부를 결정합니다.",
table: {
type: {
summary: "boolean",
},
},
control: { type: "boolean" },
},
},
};

export const Default = (props) => {
return <Price {...props} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { TmSize } from "../types/TmSize";
export type CoBreakpoints = TmSize;

export const DEFAULT_BREAKPOINTS: Record<CoBreakpoints, number> = {
xsmall: 576,
small: 768,
medium: 1024,
large: 1408,
Expand Down