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.

Can't access original (undefined) value of checkbox in custom adapter component

See original GitHub issue

I’m using semantic-ui-react where the change event from the Checkbox field originates from a <label>, which makes the getValue logic useless for it. I tried implementing it myself on the adapter component which almost works (even arrays are fine, thanks to useField giving me access to the current value inside the adapter).

However, for simple boolean components I got stuck with a strange issue where I can’t find any solution except explicitly specifying with a different prop whether the checkbox is in boolean mode or string array mode. Try checking and then unchecking it in my simplified example below:

https://codesandbox.io/s/react-final-form-simple-example-hm03e

I think this is caused by this code:

  const input: FieldInputProps = { name, value, type, ...handlers }
  if (type === 'checkbox') {
    if (_value === undefined) {
      input.checked = !!value
    } else {
      input.checked = !!(Array.isArray(value) && ~value.indexOf(_value))
      input.value = _value
    }
  }

input.value defaults to the current value, and it’s only overwritten with the prop value _value if it’s not undefined. As a workaround I can check for typeof valueProp === 'boolean' in addition to undefined, but that feels very ugly.

I guess this is also why I need to specify format={v => v} for my Checkbox field, since otherwise I end up with input.value === '' inside the adapter.

Versions used: react-final-form@6.3.0, final-form@4.16.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ThiefMastercommented, Jun 24, 2019

Thanks - also because that way I don’t need to reimplement half of the getValue logic! 😃

2reactions
erikrascommented, Jun 24, 2019

Okay, for that, you’ve got to mimic a standard input. This code here needs to be able to figure out, from the “event” you pass to onChange, what the type, value, and checked values are.

Et voilà: (line 41)

Edit 🏁 React Final Form - Simple Example

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I read out the value of my checkbox?
To get the value, just get the value from the attribute you assigned: var something = component.
Read more >
How to get a checkbox value using custom adapter and listview
Call createCheckedHolder on your adapter's constructor. And if you want to get the checked value, you can get from checkedHolder.
Read more >
Element: <oj-checkboxset> - Oracle
The JET oj-checkboxset component manages a set of oj-option child elements and creates the necessary dom elements for the actual checkboxes.
Read more >
Home Page Components Tips and Considerations
Keep these considerations in mind when creating custom components that you want displayed on the Salesforce Classic Home tab.Required Editions Available i.
Read more >
Checkbox class - material library - Flutter - Dart API docs
The checkbox can optionally display three values - true, false, and null - if tristate is true. When value is null a dash...
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