Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Switch is not switching #44

Open
donfiftyidaho opened this issue Feb 10, 2020 · 6 comments
Open

Switch is not switching #44

donfiftyidaho opened this issue Feb 10, 2020 · 6 comments

Comments

@donfiftyidaho
Copy link

When I try to use my switch like the one in the demo, it doesn't move.

I can see it register the click in my handler when I click it or use the spacebar when it's focused.

Since I'm loading these dynamically, I'm generating my id, name and label "for" with all the same value, IE: isActive-n.

The switch is loading properly on the page, it looks great, it just doesn't move. I've tried a few different id/lable value formats and nothing works yet.

Here is the switch part of my react component:

      <div className="column is-2">
          <div className="field is-pulled-right">
            <input
              id={`isActive-${props.id}`}
              type="checkbox"
              name={`isActive-${props.id}`}
              className="switch is-success is-rounded"
              checked={checked}
              onChange={swithOnChangeHandler}
            />
            <label htmlFor={`isActive-${props.id}`}>{label}</label>
          </div>
        </div>
@donfiftyidaho
Copy link
Author

It was a problem with the state of the input checked property. Also, now using true or false for the checked value

@reinux
Copy link

reinux commented Aug 5, 2020

It was a problem with the state of the input checked property. Also, now using true or false for the checked value

Can you explain what you mean by this? How did you get it working?

@donfiftyidaho
Copy link
Author

I'll try and remember, but it was many projects ago.
Here is my final use of switch, this is in a component that creates a row in a list of items. The switch is to activate or disable the item.

To clarify, it was how I was setting the state onChange and I was using 1 or 0, changed that to using true or false, which seems to make a difference. I hope the code sample helps.

// current state of switch from database
const initActive = props.is_active === 'false';

const [isActive, setIsActive] = useState(initActive);

// here the checkVal is set using true/false, not 1/0
if (isActive) {
    label = 'Activate';
    checkVal = false;
  } else {
    label = 'Active';
    checkVal = true;
  }

const handleSwitchChange = index => {
    setIsActive(index);
    setIsDisabled(false);
  };

<input
     id={`isActive-${props.id}`}
     type="checkbox"
     name={`isActive-${props.id}`}
     className="switch is-success is-rounded is-small"
     checked={checkVal}
     onChange={() => handleSwitchChange(checkVal)} // handle onChange differently
/>

@reinux
Copy link

reinux commented Aug 5, 2020

Thanks, I finally figured out what was wrong in my case: it needs an adjacent label element, and it needs to have an id and a for on the label. Otherwise it doesn't do anything.

@HDRamosMendoza
Copy link

@silentrob
Copy link

My issue was coping the html code to react. label for element needs to be changed to htmlFor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants