将项添加到数组React时删除占位符

时间:2017-09-12 09:48:31

标签: javascript arrays reactjs

我有5个位置,数字占位符可用于添加到数组中的项目,问题是我需要这些编号的占位符在项目添加到数组后消失。

如何在项目添加到数组时删除编号的占位符?

https://codesandbox.io/s/qlp47q8j26

Hello.js

import React from 'react';
import update from 'immutability-helper'
import styled from 'styled-components'
import Product from './Product'

const NumberWrap = styled.div`
  display: flex;
  flex-wrap:wrap
  border: 1px solid #ccc;
  flex-direction: row;
`

const Numbers = styled.div`
  display:flex;
  background: #fafafa;
  color: #808080;
  font-size: 32px;
  flex: 0 0 20%;
  min-height: 200px;
  justify-content: center;
  align-items:center;
  border-right: 1px solid #ccc;
`

const CardWrap = styled.div`
  display:flex;
  flex-wrap: wrap;
  flex-direction: row;
  margin-top: 20px;
`

export default class Hello extends React.Component {
  constructor() {
    super()
    this.state = {
      placeholder: [1,2,3,4,5],
      data: [
        { id: 1, header: 'Item 1'},
        { id: 2, header: 'Item 2'},
        { id: 3, header: 'Item 3'},
        { id: 4, header: 'Item 4'}
      ],
      addedItems: [],
    }
  }

  handleAdd = (id) => {
    this.setState({
        addedItems: update(this.state.addedItems, {
        $push: [
          id,
        ],
      })
    })
  }

  handleRemove = (id) => {
    const index = this.state.addedItems.indexOf(id);
    this.setState({
      addedItems: update(this.state.addedItems, {
        $splice: [
          [index, 1]
        ],
      })
    })
  }

  render() {
    return(
      <div>
        <NumberWrap>
          {
            this.state.placeholder.map(num =>
              <Numbers>{num}
              {
                this.state.data.filter(item => {
                  return this.state.addedItems.indexOf(item.id) === num - 1;
                }).slice(0, 5).map(item =>
                  <Product {...item} remove={() => { this.handleRemove(item.id) }} />
                )
              } 
              </Numbers>
            )
          }
        </NumberWrap>


        <CardWrap>
        {
          this.state.data.map(item =>
            <Product
              {...item}
              add={()=> {
                this.handleAdd(item.id) 
              }}
            />
          )}   
        </CardWrap>
      </div>
    )
  }
}

Product.js

import React from "react";
import styled from "styled-components";

const Card = styled.div`
  flex: 0 0 20%;
  border: 1px solid #ccc;
`;

const Header = styled.div`
  padding: 20px;
`;

const AddBtn = styled.button`
  width:100%;
  height: 45px;
`;

const Product = props => {
  const { add, id, header, remove } = props;
  return (
    <Card>
      <Header key={id}>
        {header}
      </Header>
      <AddBtn onClick={add}>Add</AddBtn>
      <AddBtn onClick={remove}>Remove</AddBtn>
    </Card>
  );
};

export default Product;

1 个答案:

答案 0 :(得分:1)

您应该向状态添加一个已添加数字的数组,并仅在该数组中不存在时才显示该数字。

  1. 我已将addedArray添加到构造函数中的状态。
  2. 修改了handleAddhandleRemove以删除和添加项目 新阵列。

  3. 添加了在渲染方法中呈现{num}的条件。

  4. 这是Hello.js组件的修改代码:

    import React from 'react';
    import update from 'immutability-helper'
    import styled from 'styled-components'
    import Product from './Product'
    
    const NumberWrap = styled.div`
      display: flex;
      flex-wrap:wrap
      border: 1px solid #ccc;
      flex-direction: row;
    `
    
    const Numbers = styled.div`
      display:flex;
      background: #fafafa;
      color: #808080;
      font-size: 32px;
      flex: 0 0 20%;
      min-height: 200px;
      justify-content: center;
      align-items:center;
      border-right: 1px solid #ccc;
    `
    
    const CardWrap = styled.div`
      display:flex;
      flex-wrap: wrap;
      flex-direction: row;
      margin-top: 20px;
    `
    
    export default class Hello extends React.Component {
      constructor() {
        super()
        this.state = {
          placeholder: [1,2,3,4,5],
          addedArray: [],
          data: [
            { id: 1, header: 'Item 1'},
            { id: 2, header: 'Item 2'},
            { id: 3, header: 'Item 3'},
            { id: 4, header: 'Item 4'}
          ],
          addedItems: [],
        }
      }
    
      handleAdd = (id) => {
        const nextAdded = [id,...this.state.addedArray];
        this.setState({
            addedArray: nextAdded,
            addedItems: update(this.state.addedItems, {
            $push: [
              id,
            ],
          })
        })
      }
    
      handleRemove = (id) => {
        const index = this.state.addedItems.indexOf(id);
        const nextAdded = this.state.addedArray.filter(n => n != id);
        this.setState({
          addedArray: nextAdded,
          addedItems: update(this.state.addedItems, {
            $splice: [
              [index, 1]
            ],
          })
        })
      }
    
      render() {
        return(
          <div>
            <NumberWrap>
              {
                this.state.placeholder.map(num =>
                  <Numbers>
                  {this.state.addedArray.filter(n=> n ==num).length == 0 && num}
                  {
                    this.state.data.filter(item => {
                      return this.state.addedItems.indexOf(item.id) === num - 1;
                    }).slice(0, 5).map(item =>
                      <Product {...item} remove={() => { this.handleRemove(item.id) }} />
                    )
                  } 
                  </Numbers>
                )
              }
            </NumberWrap>
    
    
            <CardWrap>
            {
              this.state.data.map(item =>
                <Product
                  {...item}
                  add={()=> {
                    this.handleAdd(item.id) 
                  }}
                />
              )}   
            </CardWrap>
          </div>
        )
      }
    }