-1

I'm trying to do two "radio-div" in React.js . A "radio-div" is a div that, if selected, changes its backgroung-color. Could anyone help me, please?

The final result must be like this

Thank you all

3

2 Answers 2

0

If a custom radio button is what you want, you can achieve that by hiding the radio button with display: none and target your div using css adjacent selector.

Check out this example I found online: https://codepen.io/AngelaVelasquez/pen/Eypnq

Look for input[type=radio]:checked ~ label

More Info: https://css-tricks.com/child-and-sibling-selectors/

Sign up to request clarification or add additional context in comments.

3 Comments

This solution is not really suitable for React (which has its own application state).
This is not the right solution for me... please look this example i.sstatic.net/GcGrc.png
@ChrisG Why is not suitable for React, its simple CSS. I think that is the most accessible way to do this. FedericoPapetti, that can also be achieved as you can customize the label. You will get focus, active, and checked attribute for accessibility plus points.
-1

Here's the stackblitz example. The idea is to add an active class on the selected radio div.

constructor() {
    this.state = {
        activeIndex: ''
    };

    this.radioButtons = [
        {
            text: 'India'
        },
        {
            text: 'England'
        }
    ]
}

updateRadioGroup(i) {
    this.setState({ activeIndex: i })
}

render() {
    let { activeIndex } = this.state;
    return (
        <React.Fragment>
            <h4> Who will win the match </h4>
            {
                this.radioButtons.map((element, i) => (
                    <div key={i} className={activeIndex === i ? 'radio_div active' : 'radio_div'} onClick={() => this.updateRadioGroup(i)}>
                        {element.text}
                    </div>
                ))
            }
        </React.Fragment>
    );
}

In my example i have added radio options meta in a state. Rendered the html with initial radio activeIndex ' ' empty (so that no radio is selected by default). Now whenever the user clicks on any of the div i update the activeIndex with the clicked radioDiv index and attach the active class on the respective radioDiv conditionally.

Thanks let me know if this is the case you wanted to achieve.

4 Comments

I didn't downvote but I wouldn't make the button names part of the state.
It is just an example to dynamically render the buttons. In the [image] (i.sstatic.net/GcGrc.png) the buttons are having label and there might be some unique id which would then be sent to the backend API based on the user input. So in the state you would keep this meta information instead of just button names.
My point is that no, you wouldn't. That information never changes, so it's not supposed to be part of the state.
You are right we mostly use this because we render forms dynamically but since in this case the information is static I think storing it in a variable would be enough. I have updated my answer. Thanks for pointing that out.

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.