Skip to content

Commit

Permalink
Merge pull request #370 from reactioncommerce/369-feat-machikoyasuda-…
Browse files Browse the repository at this point in the history
…cartitem-subtotal

feat(#369): CartItem - Display Subtotal
  • Loading branch information
machikoyasuda authored Nov 28, 2018
2 parents 59f1a57 + e398e88 commit 2ab26f5
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 46 deletions.
66 changes: 52 additions & 14 deletions package/src/components/CartItem/v1/CartItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withComponents } from "@reactioncommerce/components-context";
import { addTypographyStyles, applyTheme, CustomPropTypes } from "../../../utils";

const Item = styled.div`
position: relative;
align-items: flex-start;
border-bottom-color: ${applyTheme("CartItem.borderBottomColor")};
border-bottom-style: solid;
Expand Down Expand Up @@ -79,17 +80,39 @@ const ItemContentQuantityInput = styled.div`
`;

const ItemContentPrice = styled.div`
bottom: 0;
flex: 0 1 auto;
position: absolute;
position: ${(props) => (props.isMiniCart ? "absolute" : "initial")};
bottom: ${applyTheme("CartItem.paddingBottom")};
right: 0;
text-align: right;
@media (max-width: 768px) {
position: absolute;
}
@media (min-width: 768px) {
margin-left: 1.5rem;
position: ${({ isMiniCart }) => (isMiniCart ? "absolute" : "relative")};
}
`;

const ItemContentSubtotal = styled.div`
position: ${(props) => (props.isMiniCart ? "initial" : "absolute")};
margin-top: ${(props) => (props.isMiniCart ? applyTheme("CartItem.subtotalDisplaySpacingAbove")(props) : "0")};
bottom: ${applyTheme("CartItem.paddingBottom")};
right: 0;
text-align: right;
@media (max-width: 768px) {
position: initial;
margin-top: ${applyTheme("CartItem.subtotalDisplaySpacingAbove")};
}
`;

const ItemContentSubtotalTitle = styled.div`
${addTypographyStyles("ItemContentSubtotalTitle", "labelText")};
white-space: pre;
`;

const ItemContentSubtotalDisplay = styled.div`
${addTypographyStyles("ItemContentSubtotalDisplay", "bodyTextSemiBold")};
`;

const ItemRemoveButton = styled.button`
${addTypographyStyles("CartItemRemoveButton", "labelText")}
align-self: flex-start;
Expand Down Expand Up @@ -219,6 +242,12 @@ class CartItem extends Component {
/**
* Chosen items title
*/
subtotal: PropTypes.shape({
/**
* The display subtotal
*/
displayAmount: PropTypes.string
}),
title: PropTypes.string,
/**
* Quantity of chosen item in cart
Expand Down Expand Up @@ -293,10 +322,12 @@ class CartItem extends Component {
title,
quantity,
isLowQuantity,
price: { displayAmount: displayPrice }
price: { displayAmount: displayPrice },
subtotal
}
} = this.props;

const { displayAmount: displaySubtotal } = subtotal || {};
const { displayAmount: displayCompareAtPrice } = compareAtPrice || {};

const {
Expand Down Expand Up @@ -339,15 +370,22 @@ class CartItem extends Component {

{!isReadOnly && <ItemRemoveButton onClick={this.handleRemoveItemFromCart}>Remove</ItemRemoveButton>}
</ItemContentDetail>

<ItemContentPrice isMiniCart={isMiniCart}>
<Price
displayPrice={displayPrice}
displayCompareAtPrice={displayCompareAtPrice}
hasPriceBottom={isMiniCart}
/>
</ItemContentPrice>
</ItemContent>
<ItemContentPrice isMiniCart={isMiniCart}>
<Price
displayPrice={displayPrice}
displayCompareAtPrice={displayCompareAtPrice}
hasPriceBottom={isMiniCart}
/>
{ quantity !== 1 ?
<ItemContentSubtotal isMiniCart={isMiniCart}>
<ItemContentSubtotalTitle>Total ({quantity}):</ItemContentSubtotalTitle>
<ItemContentSubtotalDisplay>{displaySubtotal}</ItemContentSubtotalDisplay>
</ItemContentSubtotal>
:
null
}
</ItemContentPrice>
</Item>
);
}
Expand Down
38 changes: 38 additions & 0 deletions package/src/components/CartItem/v1/CartItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const item = {
},
productSlug: "product-slug",
productVendor: "Patagonia",
subtotal: {
displayAmount: "$40.00",
},
title: "A Great Product",
quantity: 2
};
Expand All @@ -37,6 +40,41 @@ const item = {
/>
```

#### Default: isMiniCart

```jsx
const item = {
_id: "123",
attributes: [{ label: "Color", value: "Red" }, { label: "Size", value: "Medium" }],
compareAtPrice: {
displayAmount: "$45.00"
},
currentQuantity: 3,
imageURLs: {
small: "//placehold.it/150",
thumbnail: "//placehold.it/100"
},
isLowQuantity: true,
price: {
displayAmount: "$20.00"
},
productSlug: "product-slug",
productVendor: "Patagonia",
subtotal: {
displayAmount: "$40.00",
},
title: "A Great Product",
quantity: 2
};

<CartItem
isMiniCart
item={item}
onChangeCartItemQuantity={(value, _id) => console.log("cart item quantity changed to", value, "for item", _id)}
onRemoveItemFromCart={() => console.log("Item removed from cart")}
/>
```

#### Without Compare At Price
```jsx
const item = {
Expand Down
3 changes: 3 additions & 0 deletions package/src/components/CartItem/v1/CartItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const mockItem = {
},
productSlug: "product-slug",
productVendor: "Patagonia",
subtotal: {
displayAmount: "$40.00"
},
title: "A Great Product",
quantity: 2
};
Expand Down
Loading

0 comments on commit 2ab26f5

Please sign in to comment.