简体   繁体   English

如何在某些条件下同时激活多个按钮?

[英]How to make several buttons simultaneously active but with some conditions?

I have a problem.我有个问题。 I want to make buttons section, where user can click buttons to filter some content.我想制作按钮部分,用户可以在其中单击按钮来过滤某些内容。 When user click on 'all' button, all other should be turn off (change its color to initial, not active) in this moment.当用户单击“全部”按钮时,此时应关闭所有其他按钮(将其颜色更改为初始颜色,不活动)。 Also, user can check multiple buttons.此外,用户可以检查多个按钮。 I can't get how to do this.我不知道该怎么做。

Example of JSON: JSON 示例:

{
    title: 'All',
    id: 53,
  },
  {
    title: 'Im a parent',
    icon: <Parent />,
    id: 0,
  },
  {
    title: 'I live here',
    icon: <ILiveHere />,
    id: 2,
  },

example of code: https://codesandbox.io/s/sleepy-haze-35htx?file=/src/App.js代码示例: https://codesandbox.io/s/sleepy-haze-35htx?file=/src/App.js

Its wrong, I know.这是错误的,我知道。 I tried some solutions, but I guess I can't get how to do it correctly.我尝试了一些解决方案,但我想我不知道如何正确地做到这一点。

With this code I can do active multiple buttons, but I can't get how to make conditions like使用此代码,我可以激活多个按钮,但我不知道如何创建条件

if (item.title === 'all){ TURN_OFF_ANY_OTHER_BTNS }如果 (item.title === 'all){ TURN_OFF_ANY_OTHER_BTNS }
I guess I should store checked buttons in temporary array to make these operations.我想我应该将选中的按钮存储在临时数组中以进行这些操作。

Will be really thankfull for help.非常感谢您的帮助。

If I understand your problem, I think this is what you are looking for:如果我理解你的问题,我想这就是你要找的:

const roles = [
  {
    title: "All",
    id: 53
  },
  {
    title: "I live here",
    id: 0
  },
  {
    title: "I live here too",
    id: 2
  }
];
// button
const SocialRole = ({ item, selected, setSelected }) => {
  const isActive = selected === item.title || selected === 'All';

  return (
    <button
      style={isActive ? { color: "red" } : { color: "blue" }}
      onClick={() => setSelected(item.title)}
    >
      {item.icon}
      <h1>{item.title}</h1>
    </button>
  );
};

export default function App() {
  const [selected, setSelected] = useState(roles[0].title);
  return (
    <div>
      {roles.map((item, index) => (
        <SocialRole
          key={index}
          item={item}
          selected={selected}
          setSelected={setSelected}
        />
      ))}
    </div>
  );
}

The problem was you were setting a new state into each button, when you should just use the state from the App.问题是你在每个按钮中设置了一个新的 state,而你应该只使用 App 中的 state。

Is this something you would like? 是你想要的吗?

const SocialRole = ({ item, selected, setSelected }) => {
  let style =
    [...selected].indexOf(item.id) !== -1
      ? { color: "red" }
      : { color: "blue" };
  return (
    <button
      style={style}
      onClick={() => {
        if (item.id === 53) {
          setSelected(null);
        } else {
          setSelected(item.id);
        }
      }}
    >
      {item.icon}
      <h1>{item.title}</h1>
    </button>
  );
};

export default function App() {
  // We keep array of selected item ids
  const [selected, setSelected] = useState([roles[0]]);
  const addOrRemove = (item) => {
    const exists = selected.includes(item);

    if (exists) {
      return selected.filter((c) => {
        return c !== item;
      });
    } else {
      return [...selected, item];
    }
  };
  return (
    <div>
      {roles.map((item, index) => (
        <SocialRole
          key={index}
          item={item}
          selected={selected}
          setSelected={(id) => {
            if (id === null) setSelected([]);
            else {
              setSelected(addOrRemove(id));
            }
          }}
        />
      ))}
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM