1

How can i make this kind of layout. this is something like radio button. live link - codesandbox.io/s/amazing-sunset-54916 but please read the question too.

initally --

enter image description here

after we select Male

enter image description here

or Female

enter image description here

I need to make this, i already have the images of male & female of without color and with color.

I tried to make it with tag and images into it, but how will i know which one is selected and suppose the option increase in other part like having 6-7 options then? how will i get to know which one is being selected.

In this, onclick of images also not working.

my code--

    <div className="gender">
        <h2 className="title">Gender</h2>
        <a className="round">
          <img
            onClick={(e) => {
              e.src = require("../images/enables_png/1a.png");
            }}
            src={require("../images/disables_png/1a.png")}
            onMouseOver={(e) =>
              (e.currentTarget.src = require("../images/enables_png/1a.png"))
            }
            onMouseOut={(e) =>
              (e.currentTarget.src = require("../images/disables_png/1a.png"))
            }
            className="round_icon"
          ></img>
          <p className="button_detail">Male</p>
        </a>
        <a className="round">
          <img
            src={require("../images/disables_png/1b.png")}
            onMouseOver={(e) =>
              (e.currentTarget.src = require("../images/enables_png/1b.png"))
            }
            onMouseOut={(e) =>
              (e.currentTarget.src = require("../images/disables_png/1b.png"))
            }
            className="round_icon"
          ></img>
          <p className="button_detail">Female</p>
        </a>
      </div>
4
  • Can you create a demo of your output here: codesandbox.io and share the link? Commented Jun 24, 2020 at 16:05
  • Please show more of your React component's code or create an runnable example. Commented Jun 24, 2020 at 16:07
  • ok, wait, i am creating on codesandbox.io and gonna share on post itself. Commented Jun 24, 2020 at 16:09
  • codesandbox.io/s/amazing-sunset-54916 here is the link Commented Jun 24, 2020 at 16:24

1 Answer 1

1

I would do smt like this: This example not handling the onMouseOver and onMouseOut events. Handle it depending on your needs. (If gets hovered a highlighted element, or a base element gets hovered) If you want to track the data in upper component, just lift out the state from the <Selection /> component

function Comp() {
  return (
    <>
      <div className="gender">
        <h2 className="title">Gender</h2>
        <Selection
          elements={[
            {
              base: require("../images/disables_png/1a.png"),
              highlighted: require("../images/enables_png/1a.png"),
              label: "Male"
            },
            {
              base: require("../images/disables_png/1b.png"),
              highlighted: require("../images/enables_png/1b.png"),
              label: "Female"
            }
          ]}
        />
      </div>
    </>
  );
}

const Selection = props => {
  const [selected, setSelected] = useState(null);

  const [mouseOver, setMouseOver] = useState(null);

  const isSelected = inx => inx === selected;
  return (
    <div>
      {props.elements.map((element, index) => {
        return (
          <a className="round" key={"index-" + index}>
            <img
              key={"i" + index}
              alt="andmatkatola"
              onClick={e => {
                return setSelected(index);
              }}
              src={
                (mouseOver || mouseOver === 0)
                  ? mouseOver === index
                    ? element.highlighted
                    : element.base
                  : isSelected(index)
                    ? element.highlighted
                    : element.base
                }
              onMouseOver={e => {
                setMouseOver(index);
                if(!isSelected(index)) {
                  e.currentTarget.src = element.highlighted
                }
              }}
              onMouseOut={e => {
                setMouseOver(null);
                if(!isSelected(index)) {
                  e.currentTarget.src = element.base
                }
              }}
              className="round_icon"
            />
            <p className="button_detail">
              {element.label} - {index}
            </p>
          </a>
        );
      })}
      {selected}
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, i already did the same thing after few hours of struggle. still thanks.
can you still make an effort to add mouseover and mouseout? i also want to add that. if you don;t mind.
I would make just smt. like this
I think this answer was cover your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.