gravity-ui / uikit

https://gravity-ui.com
MIT License
520 stars 84 forks source link

Uncontrolled Checkbox Component #1561

Closed ivancheprasov closed 2 weeks ago

ivancheprasov commented 2 months ago

Please add an ability to use uncontrolled checkboxes, so that a checkbox can be used with libraries which require it to not have checked prop. For example, 'react-hook-form' with basic register solution.

Current behavior: checkbox checked prop is always set due to the fallback of defaultValue ?? false.

KalachevDev commented 2 months ago

reproduced here https://codesandbox.io/p/sandbox/kind-thunder-q4ntyf?file=%2Fsrc%2FFakeForm.tsx%3A84%2C7

amje commented 2 months ago

@ivancheprasov @KalachevDev

Current behavior: checkbox checked prop is always set due to the fallback of defaultValue ?? false

That's incorrect. defaultValue ?? false is set for the hook's value and checked input's attribute is only set when checked prop is set. See source code here here

amje commented 2 months ago

I digged the problem. There is no change event on inputs when form is reset. So our styling (custom tick svg) renders stale state. The only way to fix this I see is to not relay on our state but rather on browser's internal state (:checked, :indeterminate CSS selectors) to do our styling.