question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Checkbox] Pass the native event as second argument of onCheckedChange

See original GitHub issue

Hello all,

Feature request

onCheckedChange used to pass the native event, and is now passing a custom state for convenience (and the “indeterminate” state).

In our case we rely on react-hook-form to handle our forms lifecycle and validation, and it’s using the native events by default in order to synchronise the inputs values with the form’s global state : https://react-hook-form.com/api/useform/register

Not having access to the native event anymore will force us to pass a custom onChange functions to our checkboxes implemented with radix :

// Checkbox is our own implementation over Radix primitives
// setValue is also from react-hook-form
<Checkbox {...register("is_happy")} onChange={(checked) => { setValue("is_happy", checked); }} />

Overview

Passing the native event as a second argument of onCheckedChange would be helpful for interoperability.

Examples in other libraries

Who does this impact? Who is this for?

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
jjenzzcommented, Mar 19, 2022

@gpeto91 check out their Controller component.

Here’s a working example https://codesandbox.io/s/weathered-worker-huynj?file=/src/App.js

2reactions
s10mcowcommented, Jun 13, 2022

Im using the useController hook to gain access to the API of react-hook-form. The API is a little cleaner than using the <Controller/> HOC, imo.

For ex:

import {
  Control,  
  FieldValues,
  RegisterOptions,
} from 'react-hook-form';
const StyledCheckbox = styled(CheckboxPrimitive.Root)``
const StyledIndicator = styled(CheckboxPrimitive.Indicator)``
const CheckLabel = styled.label``
const CheckboxButtonGroupWrapper = styled.div``

export const ControlledCheckbox = ({
  name,
  control,
  children,
  rules,
}: {
  name: string;
  rules?: RegisterOptions;
  control: Control<FieldValues, null>;
  children: React.ReactNode;
}) => {
  const { field } = useController({ name, control, rules });

  return (
    <CheckboxButtonGroupWrapper>
      <CheckLabel>
        <StyledCheckbox
          {...field}
          value={undefined}
          checked={field.value}
          onCheckedChange={field.onChange}
        >
          <StyledIndicator>
            <CheckIcon />
          </StyledIndicator>
        </StyledCheckbox>
        {children}
      </CheckLabel>
    </CheckboxButtonGroupWrapper>
  );
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use React Checkbox onChange Feature (with Code ...
In this tutorial, we'll learn how to utilize React Checkboxes onChange event handler to call functions without explicitly pressing submit.
Read more >
Assigning a value (or any other parameter) to asp:checkbox
Right now i have all of them tied to one event handler that fires on CheckedChanged, then i cast the sender to Checkbox...
Read more >
Passing Value to an OnCheckedChanged Event Handler of ...
Passing Value to an OnCheckedChanged Event Handler of asp.net Checkbox Control ... As the purpose is to focus on checkbox and passing value....
Read more >
Checkboxes - Android Developers
When the user selects a checkbox, the CheckBox object receives an on-click event. To define the click event handler for a checkbox, add...
Read more >
Native Events in Blazor CheckBox Component | Syncfusion
The onchange attribute is used to bind the onchange event for Checkbox. Here, we have explained about the sample code snippets of Checkbox....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found