feat: implement cart component with mock data and styling for improved cart functionality
This commit is contained in:
45
src/app/shared/components/cart-item/cart-item.component.html
Normal file
45
src/app/shared/components/cart-item/cart-item.component.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<article class="cart-item">
|
||||
<div class="cart-item__media">
|
||||
@if (discountPercentage() && discountPercentage()! > 0) {
|
||||
<span class="cart-item__discount-badge">-{{ discountPercentage() }}%</span>
|
||||
}
|
||||
|
||||
@if (imageUrl()) {
|
||||
<img class="cart-item__image" [src]="imageUrl()" [alt]="product()" />
|
||||
} @else {
|
||||
<div class="cart-item__image cart-item__image--placeholder" aria-hidden="true"></div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="cart-item__content">
|
||||
<div class="cart-item__top-row">
|
||||
<h3 class="cart-item__product">{{ product() }}</h3>
|
||||
|
||||
<div class="cart-item__prices">
|
||||
@if (formattedOriginalPrice(); as originalPrice) {
|
||||
<span class="cart-item__price cart-item__price--original">{{ originalPrice }}</span>
|
||||
}
|
||||
|
||||
<span class="cart-item__price cart-item__price--discounted">{{ formattedDiscountedPrice() }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="cart-item__attributes">
|
||||
@for (attribute of attributes(); track attribute.label + attribute.value) {
|
||||
<div class="cart-item__attribute">
|
||||
<span class="cart-item__attribute-label">{{ attribute.label }}:</span>
|
||||
<span class="cart-item__attribute-value">{{ attribute.value }}</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="cart-item__actions" aria-hidden="true">
|
||||
<button class="cart-item__quantity-control" type="button">-</button>
|
||||
<span class="cart-item__quantity">{{ quantity() }}</span>
|
||||
<button class="cart-item__quantity-control" type="button">+</button>
|
||||
<button class="cart-item__remove" type="button">
|
||||
<i class="fa-solid fa-trash-can"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
160
src/app/shared/components/cart-item/cart-item.component.scss
Normal file
160
src/app/shared/components/cart-item/cart-item.component.scss
Normal file
@@ -0,0 +1,160 @@
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.cart-item {
|
||||
display: grid;
|
||||
grid-template-columns: 64px minmax(0, 1fr);
|
||||
gap: 0.75rem;
|
||||
padding: 10px 12px;
|
||||
border-radius: 0;
|
||||
background-color: #f5f5f5;
|
||||
|
||||
&__media {
|
||||
position: relative;
|
||||
width: 64px;
|
||||
height: 72px;
|
||||
overflow: hidden;
|
||||
border-radius: 6px 0 0 6px;
|
||||
background-color: #d9d9d9;
|
||||
}
|
||||
|
||||
&__image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
&__image--placeholder {
|
||||
background: linear-gradient(135deg, #d2d2d2, #ececec);
|
||||
}
|
||||
|
||||
&__discount-badge {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
z-index: 1;
|
||||
padding: 2px 5px;
|
||||
border-bottom-right-radius: 4px;
|
||||
background-color: #6173ff;
|
||||
color: #ffffff;
|
||||
font-size: 8px;
|
||||
font-weight: 500;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
&__content {
|
||||
display: grid;
|
||||
gap: 0.5rem;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
&__top-row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
gap: 0.75rem;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
&__product {
|
||||
margin: 0;
|
||||
color: #666666;
|
||||
font-size: 12px;
|
||||
font-weight: 325;
|
||||
line-height: 1.25;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
&__prices {
|
||||
display: grid;
|
||||
justify-items: end;
|
||||
white-space: nowrap;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
&__price {
|
||||
color: #a0a0a0;
|
||||
}
|
||||
|
||||
&__price--original {
|
||||
font-size: 10px;
|
||||
font-weight: 325;
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
&__price--discounted {
|
||||
font-size: 13px;
|
||||
font-weight: 425;
|
||||
}
|
||||
|
||||
&__attributes {
|
||||
display: grid;
|
||||
gap: 0.125rem;
|
||||
}
|
||||
|
||||
&__attribute {
|
||||
color: #666666;
|
||||
font-size: 11px;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
&__attribute-label {
|
||||
font-weight: 325;
|
||||
}
|
||||
|
||||
&__attribute-value {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
&__actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
align-items: center;
|
||||
gap: 0.125rem;
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
&__quantity-control,
|
||||
&__quantity {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
border: 1px solid #cfcfcf;
|
||||
background-color: #ffffff;
|
||||
color: #666666;
|
||||
font-size: 10px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
&__quantity-control {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
&__remove {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
margin-left: 0.35rem;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: #b3b3b3;
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 420px) {
|
||||
.cart-item {
|
||||
grid-template-columns: 56px minmax(0, 1fr);
|
||||
|
||||
&__media {
|
||||
width: 56px;
|
||||
height: 64px;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/app/shared/components/cart-item/cart-item.component.ts
Normal file
37
src/app/shared/components/cart-item/cart-item.component.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core';
|
||||
|
||||
export interface CartItemAttribute {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cart-item',
|
||||
standalone: true,
|
||||
templateUrl: './cart-item.component.html',
|
||||
styleUrl: './cart-item.component.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
})
|
||||
export class CartItemComponent {
|
||||
readonly imageUrl = input<string | null>(null);
|
||||
readonly product = input<string>('');
|
||||
readonly originalPrice = input<number | null>(null);
|
||||
readonly discountedPrice = input<number>(0);
|
||||
readonly discountPercentage = input<number | null>(null);
|
||||
readonly attributes = input<CartItemAttribute[]>([]);
|
||||
readonly quantity = input<number>(1);
|
||||
|
||||
protected readonly formattedOriginalPrice = computed(() => {
|
||||
const price = this.originalPrice();
|
||||
return price === null ? null : this.formatCurrency(price);
|
||||
});
|
||||
|
||||
protected readonly formattedDiscountedPrice = computed(() => this.formatCurrency(this.discountedPrice()));
|
||||
|
||||
private formatCurrency(value: number): string {
|
||||
const rounded = Math.round(value);
|
||||
const parts = rounded.toString().split('.');
|
||||
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
||||
return `$ ${parts.join(',')}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user