react 购物车


import React, { Component } from 'react'
import './App.css';
class GoodsItem extends Component {
  constructor(props) {
    super(props)
    this.state = {
      goodsInfo: this.props.goodsInfo,
      singleCheck: false
    }
  }
  render() {
    return (
      <li>
        {/** 单选按钮点击的时候需要传参到父组件 参数:id,price ,number**/}
        <input type="checkbox" checked={this.props.ischeckAll ? true : this.state.singleCheck} onChange={() => {
          this.singleChecked()
        }} />
        <img src={this.state.goodsInfo.pic} alt="" />

        <div>
          <div>{this.state.goodsInfo.name}</div>
          <div style={{ color: "red" }}>{this.state.goodsInfo.price}</div>
        </div>

        <div>
          <button onClick={this.changeNumberCount.bind(this, this.state.goodsInfo.number, -1)}
            disabled={this.state.goodsInfo.number === 1}>-</button>
          <span>{this.state.goodsInfo.number}</span>
          <button onClick={this.changeNumberCount.bind(this, this.state.goodsInfo.number, 1)}>+</button>
        </div>

        <div>
          <button onClick={() => { this.props.removeItem(this.state.goodsInfo) }}>删除</button>
        </div>
      </li>
    )
  }
  // 加减
  changeNumberCount(number, count) {

    this.setState({
      goodsInfo: { ...this.state.goodsInfo, number: number + count }
    }, () => {
      if (this.state.singleCheck) {
        this.props.countMoney(this.state.goodsInfo, this.state.singleCheck)
      }
    })
  }
  // 单选
  singleChecked() {
    this.setState({
      singleCheck: !this.state.singleCheck
    }, () => {
      if (this.props.ischeckAll) {
        this.props.changeCheckAll()
      }
      this.props.countMoney(this.state.goodsInfo, this.state.singleCheck)
    })
  }

}
export default class App extends Component {
  state = {
    ischeckAll: false,
    selected: [],
    totalPrice: 0,
    totalNumber: 0,
    datalist: [{
      name: "商品1",
      price: 10,
      number: 1,
      id: 1,
      limit: 5, //限购
      pic: "https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg",
      checked: false
    },
    {
      name: "商品2",
      price: 20,
      number: 2,
      id: 2,
      limit: 10, //限购
      pic: "https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg",
      checked: false
    },
    {
      name: "商品3",
      price: 30,
      number: 3,
      id: 3,
      limit: 15, //限购
      pic: "https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg",
      checked: false
    }
    ]
  }
  render() {
    return (
      <div>
        <input type="checkbox" checked={this.state.ischeckAll} onChange={
          () => this.allCheck()} />全选/全不选
        <ul>
          {this.state.datalist.map((item, index) =>
            <GoodsItem key={item.id}
              goodsInfo={item}
              ischeckAll={this.state.ischeckAll}
              changeCheckAll={() => {
                this.setState({
                  ischeckAll: !this.state.ischeckAll
                })
              }}
              countMoney={(goodsInfo, singleCheck) => { this.countMoney(goodsInfo, singleCheck) }}
              removeItem={(goodsInfo) => { this.removeItem(goodsInfo) }} />
          )}
        </ul>
        <div>总金额:{this.state.totalPrice} 总数量:{this.state.totalNumber}</div>
      </div>
    )
  }
  // 计算总金额,总数量(单条数据)
  countMoney(goodsInfo, singleCheck) {
    console.log(goodsInfo)
    // 更新商品列表(状态,数量)
    this.setState({
      datalist: this.state.datalist.map(item => {
        if (item.id === goodsInfo.id) {
          return { ...item, singleCheck, number: goodsInfo.number }
        }
        return item
      })
    }, () => {
      // 计算
      let total = 0
      let num = 0
      this.state.datalist.filter(item => item.singleCheck).forEach((item) => {
        console.log(item)
        total += item.number * item.price
        num += item.number
      })
      //更新页面金额,数量
      this.setState({
        totalPrice: total,
        totalNumber: num
      })
    })
  }

  removeItem(goodsInfo) { // 删除
    this.setState({
      datalist: this.state.datalist.filter(item => item.id !== goodsInfo.id)
    }, () => {
      this.countMoney(goodsInfo, false)
    })
  }

  allCheck() { // 全选
    this.setState({
      ischeckAll: !this.state.ischeckAll
    }, () => {
      let total = 0
      let num = 0
      this.state.datalist.forEach(item => {
        total += item.number * item.price
        num += item.number
      })
      //更新页面金额,数量
      this.setState({
        totalPrice: total,
        totalNumber: num
      })
    })
  }
}

React购物车动画可以通过CSS3动画或React动画库来实现。以下是使用React动画库react-spring实现购物车动画的示例代码: ```jsx import React, { useState } from 'react';import { useSpring, animated } from 'react-spring'; function CartAnimation() { const [isCartOpen, setIsCartOpen] = useState(false); const [cartCount, setCartCount] = useState(0); const cartAnimation = useSpring({ transform: isCartOpen ? 'translate3d(0, 0, 0)' : 'translate3d(100%, 0, 0)', }); const handleAddToCart = () => { setIsCartOpen(true); setCartCount(cartCount + 1); setTimeout(() => { setIsCartOpen(false); }, 1000); }; return ( <div> <button onClick={handleAddToCart}>Add to Cart</button> <div style={{ position: 'relative' }}> <animated.div style={{ position: 'absolute', top: 0, right: 0, width: '50px', height: '50px', backgroundColor: 'green', borderRadius: '50%', ...cartAnimation, }} /> <span style={{ marginLeft: '10px' }}>Cart: {cartCount}</span> </div> </div> ); } ``` 在上面的代码中,我们使用了useState来管理购物车是否打开和购物车中商品的数量。当用户点击“Add to Cart”按钮时,我们将购物车打开并将商品数量加1。然后,我们使用setTimeout函数在1秒后将购物车关闭。在购物车的样式中,我们使用了react-spring的useSpring hook来实现购物车的动画效果。当购物车打开时,我们将其transform属性设置为translate3d(0, 0, 0),使其从右侧滑入屏幕。当购物车关闭时,我们将其transform属性设置为translate3d(100%, 0, 0),使其向右侧滑出屏幕。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值