-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create product card Discount components
- Loading branch information
Showing
6 changed files
with
251 additions
and
3 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
apps/store/src/components/ProductCard/Discount/Discount.css.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { style } from '@vanilla-extract/css' | ||
|
||
export const formInput = style({ | ||
width: '100%', | ||
}) | ||
|
||
export const formSubmitButton = style({ | ||
minWidth: 'auto', | ||
}) |
124 changes: 124 additions & 0 deletions
124
apps/store/src/components/ProductCard/Discount/Discount.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { action } from '@storybook/addon-actions' | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
import { type FormEvent } from 'react' | ||
import { Discount } from './Discount' | ||
|
||
type Controls = { | ||
onOpenChange?: (isOpen: boolean) => void | ||
onSubmit?: (event: FormEvent<HTMLFormElement>) => void | ||
} | ||
|
||
const meta: Meta<typeof Discount.Root> = { | ||
title: 'Components / ProductCard / Discount', | ||
component: Discount.Root, | ||
} | ||
export default meta | ||
|
||
type Story = StoryObj<Controls> | ||
|
||
export const Inactive: Story = { | ||
render: (args: Controls) => { | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
args.onSubmit?.(event) | ||
} | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Discount.Root onOpenChange={args.onOpenChange}> | ||
<Discount.Form onSubmit={handleSubmit} /> | ||
</Discount.Root> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onOpenChange: action('onOpenChange'), | ||
onSubmit: action('onSubmit'), | ||
}, | ||
} | ||
|
||
export const Active: Story = { | ||
render: (args: Controls) => { | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
args.onSubmit?.(event) | ||
} | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Discount.Root onOpenChange={args.onOpenChange} defaultOpen> | ||
<Discount.Form onSubmit={handleSubmit} /> | ||
</Discount.Root> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onOpenChange: action('onOpenChange'), | ||
onSubmit: action('onSubmit'), | ||
}, | ||
} | ||
|
||
export const Loading: Story = { | ||
render: (args: Controls) => { | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
args.onSubmit?.(event) | ||
} | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Discount.Root onOpenChange={args.onOpenChange} defaultOpen> | ||
<Discount.Form onSubmit={handleSubmit} loading /> | ||
</Discount.Root> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onOpenChange: action('onOpenChange'), | ||
onSubmit: action('onSubmit'), | ||
}, | ||
} | ||
|
||
export const Error: Story = { | ||
render: (args: Controls) => { | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
args.onSubmit?.(event) | ||
} | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Discount.Root onOpenChange={args.onOpenChange} defaultOpen> | ||
<Discount.Form onSubmit={handleSubmit} errorMessage="Invalid code" /> | ||
</Discount.Root> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onOpenChange: action('onOpenChange'), | ||
onSubmit: action('onSubmit'), | ||
}, | ||
} | ||
|
||
export const Applied: Story = { | ||
render: (args: Controls) => { | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
args.onSubmit?.(event) | ||
} | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Discount.Root onOpenChange={args.onOpenChange} defaultOpen> | ||
<Discount.Code onSubmit={handleSubmit} code="SUMMER"> | ||
3 months for free | ||
</Discount.Code> | ||
</Discount.Root> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onOpenChange: action('onOpenChange'), | ||
onSubmit: action('onSubmit'), | ||
}, | ||
} |
114 changes: 114 additions & 0 deletions
114
apps/store/src/components/ProductCard/Discount/Discount.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import clsx from 'clsx' | ||
import { useTranslation } from 'next-i18next' | ||
import { type ComponentProps, useState } from 'react' | ||
import { | ||
xStack, | ||
Text, | ||
Switch, | ||
sprinkles, | ||
Button, | ||
Badge, | ||
IconButton, | ||
CrossIconSmall, | ||
tokens, | ||
} from 'ui' | ||
import Collapsible from '@/components/Collapsible/Collapsible' | ||
import { TextField } from '@/components/TextField/TextField' | ||
import { formInput, formSubmitButton } from './Discount.css' | ||
|
||
const FORM_CAMPAIGN_CODE = 'campaignCode' | ||
|
||
type RootProps = Omit<ComponentProps<typeof Collapsible.Root>, 'open'> | ||
const DiscountRoot = ({ children, defaultOpen, ...props }: RootProps) => { | ||
const { t } = useTranslation('cart') | ||
|
||
const [isOpen, setIsOpen] = useState(defaultOpen || false) | ||
|
||
return ( | ||
<Collapsible.Root open={isOpen} {...props}> | ||
<div className={xStack({ justifyContent: 'space-between', alignItems: 'center' })}> | ||
<Text>{t('CAMPAIGN_CODE_HEADING')}</Text> | ||
<Collapsible.Trigger asChild> | ||
<Switch checked={isOpen} onCheckedChange={setIsOpen} /> | ||
</Collapsible.Trigger> | ||
</div> | ||
|
||
<Collapsible.Content> | ||
<div className={sprinkles({ paddingBlock: 'sm' })}>{children}</div> | ||
</Collapsible.Content> | ||
</Collapsible.Root> | ||
) | ||
} | ||
|
||
type FormProps = ComponentProps<'form'> & { | ||
errorMessage?: string | ||
loading?: boolean | ||
} | ||
const DiscountForm = ({ className, errorMessage, loading, ...props }: FormProps) => { | ||
const { t } = useTranslation('cart') | ||
|
||
return ( | ||
<form | ||
className={clsx( | ||
xStack({ justifyContent: 'space-between', alignItems: 'flex-start', gap: 'sm' }), | ||
className, | ||
)} | ||
{...props} | ||
> | ||
<TextField | ||
className={formInput} | ||
name={FORM_CAMPAIGN_CODE} | ||
label={t('CAMPAIGN_CODE_INPUT_LABEL')} | ||
size="small" | ||
warning={!!errorMessage} | ||
message={errorMessage} | ||
required={true} | ||
upperCaseInput={true} | ||
readOnly={loading} | ||
disabled={loading} | ||
/> | ||
|
||
<Button | ||
className={formSubmitButton} | ||
type="submit" | ||
variant="primary-alt" | ||
loading={loading} | ||
disabled={loading} | ||
> | ||
{t('CHECKOUT_ADD_DISCOUNT_BUTTON')} | ||
</Button> | ||
</form> | ||
) | ||
} | ||
|
||
type CodeProps = ComponentProps<'form'> & { | ||
code: string | ||
} | ||
const DiscountCode = ({ children, className, code, ...props }: CodeProps) => { | ||
return ( | ||
<form | ||
className={clsx(xStack({ justifyContent: 'space-between', alignItems: 'center' }), className)} | ||
{...props} | ||
> | ||
<Badge | ||
className={xStack({ alignItems: 'center', paddingRight: 'none', gap: 'sm' })} | ||
color="gray200" | ||
> | ||
<Text as="span" size="xs"> | ||
{code} | ||
</Text> | ||
<input hidden value={code} readOnly /> | ||
<IconButton variant="ghost" style={{ padding: 0 }} data-no-hover> | ||
<CrossIconSmall color={tokens.colors.textTertiary} /> | ||
</IconButton> | ||
</Badge> | ||
<Text>{children}</Text> | ||
</form> | ||
) | ||
} | ||
|
||
export const Discount = { | ||
Root: DiscountRoot, | ||
Form: DiscountForm, | ||
Code: DiscountCode, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters