css 是否可以根据状态值为true或false来设置按钮样式?

4zcjmb1e  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(203)

我有两个按钮,在切换它们时会显示两个不同的组件。出于UX的原因(为了知道哪个组件在显示),我想根据状态值是true还是false来设置按钮的样式(如果状态为true,则给它们加下划线,颜色更深)。这在任何方面都是可能的吗?
这是我的GitHub存储库:https://github.com/uohman/Portfolio2022
这是我处理按钮的组件:
`

import React, { useState } from 'react'
import ReactDOM from 'react-dom';
import { Subheading } from 'GlobalStyles';
import { FrontendProjects } from './FrontendProjects'
import { GraphicDesignProjects } from './GraphicDesignProjects';
import 'index.css'

export const FeaturedProjects = () => {
  const [buttons, setButtons] = useState([
    { label: 'Development', value: true },
    { label: 'Graphic design', value: false }
  ]);

  const handleButtonsChange = () => (label) => {
    const newButtonsState = buttons.map((button) => {
      if (button.label === label) {
        return (button = { label: button.label, value: true });
      }
      return {
        label: button.label,
        value: false
      };
    });
    setButtons(newButtonsState);
  };

  return (
    <>
      <Subheading><span>Featured projects</span></Subheading>
      <SpecialButton {...{ buttons, setButtons, handleButtonsChange }} />
      {buttons[0].value && <FrontendProjects />}
      {buttons[1].value && <GraphicDesignProjects />}
    </>
  );
};

const SpecialButton = ({ buttons, setButtons, handleButtonsChange }) => {
  return (
    <div className="button-container">

      {buttons.map((button, index) => (

        <button
          key={`${button.label}-${index}`}
          onClick={() => handleButtonsChange({ buttons, setButtons })(button.label)}>
          {button.label.toUpperCase()}
        </button>
      ))}

    </div>
  );
};

const rootElement = document.getElementById('root');
ReactDOM.render(<FeaturedProjects />, rootElement);

`
我已经给了按钮一个伪元素:focus,这几乎解决了我的问题,但作为默认设置,按钮的颜色仍然是相同的,尽管它是显示的组件之一。感谢您关于如何解决这个问题的建议!

jk9hmnmh

jk9hmnmh1#

您可以为任何html组件提供样式属性。
您应该传递属性为Camelcased的对象。

<button
  style={{ // double bracket to pass an object
    backgroundColor: yourVariable ? 'red' : undefined // notice css background-color became backgroundColor
  }}
>
  {button.label.toUpperCase()}
</button>

您可以对类执行相同的操作

<button
  className={yourVariable && "yourClass"}
>
  {button.label.toUpperCase()}
</button>
gab6jxml

gab6jxml2#

您可以根据条件设定button的样式。

在这个用例中,您已经有了状态button.value,它可以用作为Map的button设置内联样式(或类)的条件。
示例:

const SpecialButton = ({ buttons, setButtons, handleButtonsChange }) => {
  return (
    <div className="button-container">
      {buttons.map((button, index) => (
        <button
          key={`${button.label}-${index}`}

          // 👇 This property is added
          style={{
            backgroundColor: button.value ? "#aaa" : "#eee",
            textDecoration: button.value ? "underline" : "none",
          }}

          onClick={() =>
            handleButtonsChange({ buttons, setButtons })(button.label)
          }
        >
          {button.label.toUpperCase()}
        </button>
      ))}
    </div>
  );
};

在上面的示例中,按钮被设置为在选定时变暗,但您可以进一步自定义样式以获得所需的结果。
More about inline styles
顺便说一句,没有必要通过onClick={() => handleButtonsChange({ buttons, setButtons })(button.label)}将状态值传递给事件。
父组件始终具有这些值,因此您不需要将其向下传递到SpecialButton并将其传递回来。
希望这会有所帮助!
完整示例:

import React, { useState } from "react";
import ReactDOM from "react-dom";
import { Subheading } from "GlobalStyles";
import { FrontendProjects } from "./FrontendProjects";
import { GraphicDesignProjects } from "./GraphicDesignProjects";
import "index.css";

export const FeaturedProjects = () => {
  const [buttons, setButtons] = useState([
    { label: "Development", value: true },
    { label: "Graphic design", value: false },
  ]);

  const handleButtonsChange = (label) => {
    const newButtonsState = buttons.map((button) => {
      if (button.label === label) {
        return (button = { label: button.label, value: true });
      }
      return {
        label: button.label,
        value: false,
      };
    });
    setButtons(newButtonsState);
  };

  return (
    <>
      <Subheading>
        <span>Featured projects</span>
      </Subheading>
      <SpecialButton {...{ buttons, handleButtonsChange }} />
      {buttons[0].value && <FrontendProjects />}
      {buttons[1].value && <GraphicDesignProjects />}
    </>
  );
};

const SpecialButton = ({ buttons, handleButtonsChange }) => {
  return (
    <div className="button-container">
      {buttons.map((button, index) => (
        <button
          key={`${button.label}-${index}`}

          // 👇 This property is added
          style={{
            backgroundColor: button.value ? "#aaa" : "#eee",
            textDecoration: button.value ? "underline" : "none",
          }}

          onClick={() =>
            handleButtonsChange(button.label)
          }
        >
          {button.label.toUpperCase()}
        </button>
      ))}
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<FeaturedProjects />, rootElement);

相关问题