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

购物车里不存在东西时应显示0 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion components/cart/CartBadge.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function CartBadge() {
return (
<div className="relative">
<span className="absolute outline outline-2 bottom-3.5 left-5 bg-red-500 rounded-md w-5 h-5 p-0.5 text-center text-xs text-white">
{formatNumber(totalItems)}
{formatNumber(totalItems, '0')}
</span>

<Icons.Cart className="icon h-7 w-7" />
Expand Down
4 changes: 2 additions & 2 deletions utils/formatNumber.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function formatNumber(n) {
export default function formatNumber(n, nullString='') {
if (n) {
let newNumber = n.toString()

return newNumber.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
return ''
return nullString
}