您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CartCard.jsx 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Box, Button, ButtonGroup, Paper, Typography } from '@mui/material';
  2. import Image from 'next/image';
  3. import PropType from 'prop-types';
  4. import { useState } from 'react';
  5. const CartCard = ({ product, initialQuantity, remove, updateQuantity }) => {
  6. const [quantity, setQuantity] = useState(initialQuantity);
  7. // useEffect(() => {
  8. // updateQuantity(product?.customID, quantity);
  9. // }, [quantity]);
  10. return (
  11. <Paper
  12. sx={{
  13. p: 1,
  14. width: { lg: '88%', xs: '73%', md: '80%' },
  15. mb: 2,
  16. ml: 12,
  17. backgroundColor: '#f2f2f2',
  18. display: 'flex',
  19. justifyContent: { xs: 'center', md: 'none' },
  20. }}
  21. elevation={3}
  22. >
  23. <Box sx={{ width: '30%', display: { xs: 'none', md: 'block' } }}>
  24. <Image
  25. src="/images/coffee-mug.svg"
  26. alt="profile"
  27. width={500}
  28. height={300}
  29. />
  30. </Box>
  31. <Box
  32. sx={{
  33. ml: -4,
  34. mr: 2,
  35. display: 'flex',
  36. alignItems: 'center',
  37. width: '30%',
  38. }}
  39. >
  40. <Typography
  41. sx={{
  42. width: '100%',
  43. textAlign: 'center',
  44. height: 25,
  45. fontWeight: 600,
  46. ml: { xs: -1, sm: 0 },
  47. fontSize: { xs: 16, sm: 20 },
  48. }}
  49. >
  50. {product?.name}
  51. </Typography>
  52. </Box>
  53. <Box
  54. sx={{
  55. display: 'flex',
  56. flexDirection: 'column',
  57. width: '20%',
  58. justifyContent: 'center',
  59. alignItems: 'center',
  60. ml: { xs: 2, sm: 0 },
  61. }}
  62. >
  63. <Typography
  64. sx={{
  65. width: '100%',
  66. textAlign: 'center',
  67. height: 16,
  68. fontSize: 14,
  69. }}
  70. >
  71. Quantity
  72. </Typography>
  73. <ButtonGroup
  74. size="small"
  75. aria-label="small outlined button group"
  76. sx={{
  77. height: 35,
  78. width: 125,
  79. mt: 1,
  80. backgroundColor: 'primary.main',
  81. color: 'white',
  82. border: 0,
  83. }}
  84. >
  85. <Button
  86. sx={{
  87. color: 'white',
  88. fontSize: 17,
  89. width: 25,
  90. }}
  91. onClick={() => {
  92. if (quantity > 0) {
  93. updateQuantity(product?.customID, quantity - 1);
  94. setQuantity((prevState) => prevState - 1);
  95. }
  96. }}
  97. >
  98. -
  99. </Button>
  100. <Button
  101. sx={{
  102. color: 'white',
  103. fontSize: 15,
  104. width: 25,
  105. }}
  106. >
  107. {quantity}
  108. </Button>
  109. <Button
  110. sx={{
  111. color: 'white',
  112. fontSize: 17,
  113. width: 25,
  114. }}
  115. onClick={() => {
  116. updateQuantity(product?.customID, quantity + 1);
  117. setQuantity((prevState) => prevState + 1);
  118. }}
  119. >
  120. +
  121. </Button>
  122. </ButtonGroup>
  123. <Button
  124. sx={{
  125. height: 35,
  126. mt: 1,
  127. width: 125,
  128. fontSize: 15,
  129. textTransform: 'none',
  130. backgroundColor: '#C6453E',
  131. color: 'white',
  132. }}
  133. startIcon={
  134. <Image src="/images/x.svg" alt="remove" width={15} height={15} />
  135. }
  136. onClick={() => remove(product.customID)}
  137. >
  138. Remove
  139. </Button>
  140. </Box>
  141. <Box
  142. sx={{
  143. ml: { xs: 5, sm: 3 },
  144. display: 'flex',
  145. flexDirection: 'column',
  146. width: '20%',
  147. justifyContent: 'center',
  148. alignItems: 'center',
  149. }}
  150. >
  151. <Typography
  152. sx={{
  153. width: '100%',
  154. textAlign: 'center',
  155. height: 25,
  156. fontSize: { xs: 15, md: 18 },
  157. }}
  158. >
  159. Price: ${product?.price}
  160. </Typography>
  161. </Box>
  162. </Paper>
  163. );
  164. };
  165. CartCard.propTypes = {
  166. product: PropType.shape({
  167. category: PropType.string,
  168. name: PropType.string,
  169. image: PropType.string,
  170. description: PropType.string,
  171. place: PropType.string,
  172. people: PropType.string,
  173. process: PropType.string,
  174. pairing: PropType.string,
  175. available: PropType.Boolean,
  176. isFeatured: PropType.Boolean,
  177. price: PropType.number,
  178. customID: PropType.string,
  179. }),
  180. initialQuantity: PropType.number,
  181. remove: PropType.func,
  182. updateQuantity: PropType.func,
  183. };
  184. export default CartCard;