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: Price atom #760

Merged
merged 6 commits into from
Jun 16, 2021
Merged

feat: Price atom #760

merged 6 commits into from
Jun 16, 2021

Conversation

icazevedo
Copy link
Contributor

@icazevedo icazevedo commented Jun 11, 2021

What's the purpose of this pull request?

This PR adds the Price atom to our store-ui library.

UI

import { Price } from '@vtex/store-ui'
import { useMemo } from 'react'

function useIntlFormatter(price: number) {
  return useMemo(() => {
    const formattedPrice = new Intl.NumberFormat('en-GB', {
      style: 'currency',
      currency: 'EUR',
    }).format(price)

    return formattedPrice
  }, [price])
}

function useIntlPartsFormatter(price: number) {
  return useMemo(() => {
    return new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
    })
      .formatToParts(price)
      .map((part) => {
        const props = {
          [`data-store-price-${part.type}`]: true,
        } as Record<string, unknown>

        if (part.type === 'integer') {
          props.style = { fontWeight: 700 }
        }

        return (
          <span key={part.type} {...props}>
            {part.value}
          </span>
        )
      })
  }, [price])
}

function customFormatter(price: number) {
  const unformattedPrice = `${price}`
  const formattedPrice = `${unformattedPrice.replace('.', ',')} reais`

  return formattedPrice
}

export default function Prices() {
  return (
    <div className="space-x-2">
      <Price value={32.5} />
      <Price value={32.5} formatter={useIntlFormatter} />
      <Price value={32.5} formatter={useIntlPartsFormatter} />
      <Price value={32.5} formatter={useIntlFormatter} listing />
      <Price value={32.5} formatter={customFormatter} />
    </div>
  )
}

Styling

[data-store-price] {
  padding-right: 50px;
}

[data-store-price][data-listing] {
  text-decoration: line-through;
}

[data-store-price] [data-store-price-integer] {
  font-weight: bold;
}

Screen Shot 2021-06-11 at 11 57 46

@icazevedo icazevedo self-assigned this Jun 11, 2021
@icazevedo icazevedo requested a review from a team as a code owner June 11, 2021 14:35
@netlify
Copy link

netlify bot commented Jun 11, 2021

✔️ Deploy Preview for storeui ready!

🔨 Explore the source changes: 2693909

🔍 Inspect the deploy log: https://app.netlify.com/sites/storeui/deploys/60c8eae974e10d00086da244

😎 Browse the preview: https://deploy-preview-760--storeui.netlify.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 11, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2693909:

Sandbox Source
Store UI Typescript Configuration

Copy link
Member

@emersonlaurentino emersonlaurentino left a comment

Choose a reason for hiding this comment

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

I don't understand the listing proposal. Can you explain?

packages/store-ui/src/atoms/Price/Price.stories.tsx Outdated Show resolved Hide resolved
packages/store-ui/src/atoms/Price/Price.stories.tsx Outdated Show resolved Hide resolved
packages/store-ui/src/atoms/Price/Price.stories.tsx Outdated Show resolved Hide resolved
packages/store-ui/src/atoms/Price/Price.tsx Outdated Show resolved Hide resolved
@icazevedo icazevedo requested review from tlgimenes and matheusps June 11, 2021 15:25
@icazevedo icazevedo requested a review from tlgimenes June 15, 2021 17:54
Copy link
Contributor

@tlgimenes tlgimenes left a comment

Choose a reason for hiding this comment

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

Nice work!

Copy link
Member

@emersonlaurentino emersonlaurentino left a comment

Choose a reason for hiding this comment

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

Great Job @icazevedo 🎉

@icazevedo icazevedo merged commit 1e4966c into master Jun 16, 2021
@icazevedo icazevedo deleted the feature/ui-price branch June 16, 2021 18:11
bentoper pushed a commit that referenced this pull request Jun 17, 2021
* Adding Price atom to store-ui

* Removing unused children arg from Price storybook

* Adding variant to Price and memoizing storybook formatters

* Removing useless condition on Price atom

* Fixing default variant name and data attributes on Price atom

* Exporting Price component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants