映射复选框中的复选框Reactjs



我具有一个函数,该功能会在检查主复选框后触发儿童复选框,并且所有这些复选框均从JSON绘制。单击及其工作效果很好,主要的复选框(最高级别)及其所有儿童的复选框(第二级)显示了,我试图显示的是主要复选框的孩子的孩子(第三级)第二级项目。

基本上,请检查所有三个订单,然后在我当前的代码中添加第三订单,因此选项group 显示 options ,以及>Options 是我要显示的,它是选项1 选项2 选项3 等等。P>

复选框值以 conscy> Ceckbox.js 的props的方式传递给 itemlist.js fetch/map发生的地方。

(p.s。我对每个部分的选择限制,以防任何人对选择过程感到困惑,这与此问题无关,可以忽略)

主摘要:https://codesandbox.io/embed/6jykwp3x6n?fontsize=14

仅用于演示的第三级目标框:https://codesandbox.io/embed/o932z4yr6y?fontsize=14,

复选框.js

import React from "react";
import "./Checkbox.css";
class Checkboxes extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      currentData: 0,
      limit: 2,
      checked: false
    };
  }
  selectData(id, event) {
    let isSelected = event.currentTarget.checked;
    if (isSelected) {
      if (this.state.currentData < this.props.max) {
        this.setState({ currentData: this.state.currentData + 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = false;
      }
    } else {
      if (this.state.currentData >= this.props.min) {
        this.setState({ currentData: this.state.currentData - 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = true;
      }
    }
  }
  render() {
    const input2Checkboxes =
      this.props.options &&
      this.props.options.map(item => {
        return (
          <div className="inputGroup2">
            {" "}
            <div className="inputGroup">
              <input
                id={this.props.childk + (item.name || item.description)}
                name="checkbox"
                type="checkbox"
                onChange={this.selectData.bind(
                  this,
                  this.props.childk + (item.name || item.description)
                )}
              />
              <label
                htmlFor={this.props.childk + (item.name || item.description)}
              >
                {item.name || item.description}{" "}
              </label>
            </div>
          </div>
        );
      });
    return (
      <form className="form">
        <div>
          {/** <h2>{this.props.title}</h2>*/}
          <div className="inputGroup">
            <input
              id={this.props.childk + this.props.name}
              name="checkbox"
              type="checkbox"
              checked={this.state.checked}
              onChange={this.selectData.bind(
                this,
                this.props.childk + this.props.uniq
              )}
              onChange={() => {
                this.setState({
                  checked: !this.state.checked,
                  currentData: 0
                });
              }}
            />
            <label htmlFor={this.props.childk + this.props.name}>
              {this.props.name}{" "}
            </label>
          </div>{" "}
          {this.state.checked ? input2Checkboxes : undefined}
        </div>
      </form>
    );
  }
}
export default Checkboxes;

itemlist.js 映射功能发生

...
      const selectedItem =
        selectedChild.children && selectedChild.children.length
          ? selectedChild.children[this.state.itemSelected]
          : null;
...
  <div>
                {selectedItem &&
                  selectedItem.children &&
                  selectedItem.children.map((item, index) => (
                    <Checkboxes
                      key={index}
                      name={item.name || item.description}
                      myKey={index}
                      options={item.children}
                      childk={item.id}
                      max={item.max}
                      min={item.min}
                    />
                  ))}
              </div>
...

问题是递归的问题。显示选项组时,将显示组复选框及其选项。显示选项时,必须考虑其类型。

  1. 如果该选项是ingredient,则可以显示一个简单的复选框。
  2. 如果是group,则必须显示一个新的选项组,如上所述。

这是您的代码中缺少的内容。这是一个工作的沙箱,这是您可以修复它的方法:

在复选框中:

const input2Checkboxes =
  this.props.options &&
  this.props.options.map((item, index) => {
    // Is the option a 'group' or an 'ingredient'?
    return item.type === "group" ? (
      {/* It's a group so display its options recursively */}
      <Checkboxes
        key={index}
        name={item.name || item.description}
        options={item.children}
        childk={this.props.childk}
        max={item.max}
        min={item.min}
      />
    ) : (
      {/* It's an ingredient so display a simple checkbox */}
      <div className="inputGroup2" key={item.name || item.description}>
        {" "}
        <div className="inputGroup">
          <input
            id={this.props.childk + (item.name || item.description)}
            name="checkbox"
            type="checkbox"
            onChange={this.selectData.bind(
              this,
              this.props.childk + (item.name || item.description)
            )}
          />
          <label
            htmlFor={this.props.childk + (item.name || item.description)}
          >
            {item.name || item.description}{" "}
          </label>
        </div>
      </div>
    );
  });

最新更新