首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于对象数组形成组件

问基于对象数组形成组件
EN

Stack Overflow用户
提问于 2019-08-22 11:29:00
回答 2查看 133关注 0票数 0

对于应用程序的设置页面,我已经实现了启用(绿色)或禁用(红色)状态的滑块。父级的设置是根据其子级的值计算的。

代码语言:javascript
复制
//Getting the switches configuration inside componnetDidMount something like this

var obj = [
  {
    parent_header_name: "parent1",
    children_info: [
      {
        child_switch_name: "child1",
        isEnabled: true
      },
      {
        child_switch_name: "child2",
        isEnabled: false
      }
    ]
  },
  {
    parent_header_name: "parent2",
    children_info: [
      {
        child_switch_name: "child3",
        isEnabled: true
      }
    ]
  },
  {
    parent_header_name: "parent3",
    children_info: [
      {
        child_switch_name: "child4",
        isEnabled: false
      }
    ]
  }
];

现在,基于这个值,我需要组成一个由父母和孩子组成的组,如下所示:

代码语言:javascript
复制
Label(the value should be parent_header_name) : Parent Switch Component
Label for children(children_switch_name) : Child Switch Component

另外,在每个孩子的切换切换中,我需要得到这个开关的信息,如下所示:

例如,将父母的子女改为残疾人。

代码语言:javascript
复制
[
  {
    parent_header_name: "parent1",
    children_info: [
      {
        child_switch_name: "child1",
        isEnabled: false
      }
    ]
  }
];

如果parent1已启用,我需要获得它所有的chidren值

代码语言:javascript
复制
[
  {
    parent_header_name: "parent1",
    children_info: [
      {
        child_switch_name: "child1",
        isEnabled: true
      },
      {
        child_switch_name: "child2",
        isEnabled: true
      }
    ]
  }
]

当父母开关被切换时(当父母被启用时,孩子将被启用,而残疾的孩子将被禁用;),我需要获得该家长的全部信息。

此外,我需要避免切换到“部分”状态,父只应启用或禁用。“部分”只是代表性。

为此,我使用的反应-多开关这个切换开关。

我尝试过这样的方法:https://codesandbox.io/s/parent-child-switches-gxfx6

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-23 02:48:09

您可以重构您的Setting component以允许它将您的交换机name and value according呈现到从API接收到的数据。我建议您在每个switches group中添加一个id,这将简化您的工作。这里是分叉工作沙箱。可以对代码进行更新以适合您的使用程序。关键更改是在Setting Component中完成的。

设置组件全代码

代码语言:javascript
复制
import React, { Component, Fragment } from "react";
import isEqual from "lodash.isequal";

import ChildSwitch from "./ChildSwitch";
import ParentSwitch from "./ParentSwitch";
import { PARTIAL } from "./constant";

export default class Setting extends Component {
  state = {
    parent: {
      value:
        this.props.children.length > 1
          ? PARTIAL
          : this.props.children[0].isEnabled
    },
    children: this.props.children
  };

  componentDidMount() {
    this.setParentSwitchValue();
  }

  shouldComponentUpdate(nextProps, nextState) {
    return !isEqual(this.state, nextState);
  }

  setChildSwitchValue = (id, isEnabled) => {
    let clickedChild;
    this.setState(
      prevState => ({
        ...prevState,
        children: prevState.children.map(child => {
          if (child.id === id) {
            clickedChild = { ...child, isEnabled: isEnabled };
            return clickedChild;
          } else {
            return child;
          }
        })
      }),
      () => this.setParentSwitchValue(clickedChild)
    );
  };

  setParentSwitchValue = clickedChild => {
    const { children } = this.state;
    let parentVal = PARTIAL;

    if (children.every(({ isEnabled }) => isEnabled === true)) {
      parentVal = true;
    }
    if (children.every(({ isEnabled }) => isEnabled === false)) {
      parentVal = false;
    }

    this.setState(
      prevState => ({
        ...prevState,
        parent: {
          value: parentVal
        }
      }),
      () => {
        this.handleChange();
        if (clickedChild) {
          const changed = {
            parent: {
              name: this.props.name,
              value: parentVal
            },
            child: clickedChild
          };
          console.log("This is the changed child", changed);
        }
      }
    );
  };

  setChildrenValue = value => {
    this.setState(
      prevState => ({
        ...prevState,
        parent: {
          value
        },
        children: prevState.children.map(child => ({
          ...child,
          isEnabled: value
        }))
      }),
      this.handleChange
    );
  };

  handleChange = () => {
    const { id, onChange } = this.props;
    onChange(id, this.state);
  };

  handleParentClick = parentVal => {
    if (parentVal !== PARTIAL) {
      this.setChildrenValue(parentVal);
    }
  };

  render() {
    const { parent, children } = this.state;
    const { name } = this.props;
    return (
      <div className="boxed">
        <span>{name}</span>
        <ParentSwitch
          childrenCount={children.length}
          parentSwitch={parent.value}
          onSelect={this.handleParentClick}
        />
        {children.map(({ id, name, isEnabled }) => (
          <Fragment key={id}>
            <span>{name}</span>
            <ChildSwitch
              switchName={id}
              selected={isEnabled}
              onSelect={this.setChildSwitchValue}
            />
          </Fragment>
        ))}
      </div>
    );
  }
}
票数 1
EN

Stack Overflow用户

发布于 2019-08-22 11:50:32

我会这样做:

  • 您有一个顶级Settings类,它获取设置对象,将其保存为其状态并映射数组,创建ParentSwitch实例,传递包含其子对象的完整父设置和更新Settings状态的函数(例如,使用两个整数参数--父索引和子索引)作为对ParentSwitch的支持。
  • 然后,ParentSwitch基于它的道具创建ChildSwitches,并将它通过道具获得的updateSettings函数传递给ChildSwitch。
  • 当ChildSwitch被切换时,它调用Settings中的函数,切换传递给ParentSwitch的Settings状态和显示它的ChildSwitch。

这可以理解吗?可以随意询问任何可能不清楚的事情。

编辑:,这是工作代码。你必须自己布置,我已经花了足够多的时间在这上面。https://codesandbox.io/s/parent-child-switches-jq0bp?fontsize=14我所有的代码都是用Switches.js编写的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57608349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档