Giter VIP home page Giter VIP logo

Comments (1)

U-4-E-A avatar U-4-E-A commented on May 17, 2024

I ended up creating my own checkbox by copying this repo and modifying it to fit my needs.

One of the issues I had was with including useMergedState in my module.... this seems to be an export issue with the rc-util package not being developed for ESM.

My solution was to get rid of useMergedState entirely. The alternative was actually quite simple - useEffect and useState combined: -

const [isChecked, setIsChecked] = useState<boolean>(false)

  useEffect(() => {
    setIsChecked(checked || defaultChecked || false)
  }, [setIsChecked, checked, defaultChecked])
  const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
    if (disabled) return
    setIsChecked(!isChecked)
    [...]
  }

Then I can just use the state isChecked as normal (where rawValue would have been used).

I created a test harness of all possibilities and it passed: -

const arr = [
  { checked: undefined, defaultChecked: undefined, expected: false },
  { checked: undefined, defaultChecked: false, expected: false },
  { checked: undefined, defaultChecked: true, expected: true },
  { checked: false, defaultChecked: undefined, expected: false },
  { checked: false, defaultChecked: false, expected: false },
  { checked: false, defaultChecked: true, expected: true },
  { checked: true, defaultChecked: undefined, expected: true },
  { checked: true, defaultChecked: false, expected: true },
  { checked: true, defaultChecked: true, expected: true }
]

All elements displayed and behaved as expected.

Although I am considering moving the defaultChecked option to force this to be a controlled component (personal preference).

from checkbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.