preact icon indicating copy to clipboard operation
preact copied to clipboard

input type=checkbox cannot be a controlled component?

Open mischnic opened this issue 3 years ago • 5 comments

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.

mischnic avatar Aug 24 '20 11:08 mischnic

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 avatar Aug 27 '20 19:08 developit

@developit Is this issue still relevant? Can I work on this?

imrishabh18 avatar May 04 '21 20:05 imrishabh18

Another sandbox https://codesandbox.io/s/frosty-rui-xh84ms?file=/src/index.js

Still not fixed it seems like.

eusahn avatar Mar 31 '23 19:03 eusahn

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

mayerraphael avatar Sep 04 '23 09:09 mayerraphael

Some context & workarounds: https://github.com/preactjs/preact/issues/3851#issuecomment-1371824709

rschristian avatar Sep 15 '23 22:09 rschristian