preact
preact copied to clipboard
input type=checkbox cannot be a controlled component?
Reproduction
With this example, React creates a controlled input while Preact creates a uncontrolled input.
import React from "preact/compat";
function App() {
return <input type="checkbox" checked={true} />;
}
In fact, I can't figure out how to make a controlled checkbox at all.
Not sure if this is a duplicate of #2625 or #1899
Steps to reproduce
https://codesandbox.io/s/preact-input-uncontrolled-qis0i
Click on the checkbox
Expected Behavior
Clicking on the checkbox shouldn't change its state (= what React does).
Actual Behavior
The checkbox can be toggled. checked={true}
is essentially ignored.
Probably considered a duplicate of #1899. We should be patching this in compat, in core the recommendation is to use an event that either produces a render or to use preventDefault() since that's the standard approach to implementing input prevention in the DOM:
function App() {
return <input type="checkbox" checked={true} onClick={(e) => e.preventDefault()} />;
}
@developit Is this issue still relevant? Can I work on this?
Another sandbox https://codesandbox.io/s/frosty-rui-xh84ms?file=/src/index.js
Still not fixed it seems like.
Controlled does not seem to work at all.
Using <input type="text" with value and still can change the value.
https://codesandbox.io/s/gifted-cerf-5pdrdw
Some context & workarounds: https://github.com/preactjs/preact/issues/3851#issuecomment-1371824709