Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle: doesn't react on changed checked due to state changes #2091

Open
mfranzke opened this issue Dec 2, 2023 · 1 comment
Open

Toggle: doesn't react on changed checked due to state changes #2091

mfranzke opened this issue Dec 2, 2023 · 1 comment
Assignees
Labels
bug Something isn't working community feedback question Further information is requested
Milestone

Comments

@mfranzke
Copy link
Member

mfranzke commented Dec 2, 2023

When trying to use the toggle element with the checked-property (in this case in React), a state depending usage doesn't lead to a change in the checked state of the component itself. Neither does it render correctly when initiated by checked={false}.

@mfranzke mfranzke added the bug Something isn't working label Dec 2, 2023
@mfranzke mfranzke added this to the Release 1.0 milestone Dec 2, 2023
@mfranzke mfranzke self-assigned this Dec 2, 2023
@mfranzke mfranzke added question Further information is requested community feedback labels Dec 2, 2023
@mfranzke mfranzke changed the title Toggle: doesn't react on checked changed due to state changes Toggle: doesn't react on changed checked due to state changes Dec 2, 2023
@nmerget
Copy link
Member

nmerget commented Jun 20, 2024

I tested it should work but the event.target.checked isn't correct if you have a controlled component. You need to use !controlled to change the state.

react-dbtoggle.mp4

Snippet:

import "@db-ui/core/dist/css/db-ui-core.vars.css";
import { DbToggle, DbButton } from "@db-ui/react-elements-enterprise";
import { useEffect, useRef, useState } from "react";

const App = () => {
  const ref =
      useRef<HTMLDbToggleElement>(null);
  const [controlled, setControlled] =
      useState<boolean>(true);

  useEffect(() => {
    if (ref) {
      console.log("ref: ", ref.current);
    }
  }, [controlled]);

  return (
    <>
      <span>controlled: {String(controlled)}</span>
      <DbToggle
        ref={ref}
        checked={controlled}
        onDbChange={() => {
          setControlled(!controlled);
        }}
      />
      <DbButton
        onClick={() => {
          setControlled(!controlled);
        }}
      >
        Change controlled
      </DbButton>
    </>
  );
};

export default App;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community feedback question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants