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.

Handling Radio Buttons in mobx-react-form

See original GitHub issue

Hi, Super cool project! The only issue that I’m finding with it, is that there’s no clear way to handle radio buttons (and their states) with this project. Here’s what I ultimately settled on in an effort to get radio buttons used with mobx-react-form

My fields looked something like [{name: 'question1', label: 'This is a question?', value: 'answer1', extra: ['answer1', 'answer2', 'answer3']}, {name: 'question2', label: 'This is also a question?', value: 'answer1', extra: ['answer1', 'answer2', 'answer3']}]

and to render them to the DOM I did something like

const questions = form.map((question, i) => {
    const answers = question.extra.map((answer, j) => (
        <div key={j}>
            <input {...question.bind()} type="radio" value={answer} checked={question.value === answer} />
        </div>
    )
    return (
        <div key={i}>
            <p>{question.label}</p>
            { answers }
        </div>
    )
}

But it feels bizarre to me to bind the input for each question to the radio button for the answer, and have to specify thechecked props for radio buttons. Is there any plan to add radio button support to this project?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
mancioshellcommented, Dec 17, 2018

Any news on radio button feature?

5reactions
SimeonCcommented, Jun 23, 2017

It would definitely be nice if I could just .bind a radio type like we can with a checkbox, though I can see how it’s not going to be easy (I had a quick look to see if I could make a PR but got lost in Fields/Bindings which I don’t understand yet).

I’ve added a slightly simpler example than @mprobber as it took me a while to figure out what he was doing.

// field definition
{
  name: 'gender',
  type: 'radio',
  label: 'accounts.account.gender',
  rules: 'required',
  default: GENDERS.NONE
}

// example render
<input
  {...form.select('gender').bind({
    value: GENDERS.NONE,
    checked: GENDERS.NONE === gender.value
  })}
/>
<input
  {...form.select('gender').bind({
    value: GENDERS.MALE,
    checked: GENDERS.MALE === gender.value
  })}
/>
<input
  {...form.select('gender').bind({
    value: GENDERS.FEMALE,
    checked: GENDERS.FEMALE === gender.value
  })}
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Radio Buttons in ReactJS | Pluralsight
Radio buttons let a user choose a single option from a list of multiple radio buttons and submit its value. For example, here's...
Read more >
Handling React Forms with Mobx Observables
Learn a single approach to handle React forms with Mobx observables without using an already existing form library.
Read more >
mobx-react-form - Bountysource
Automagically manage React forms state and automatic validation with MobX ... no clear way to handle radio buttons (and their states) with this...
Read more >
Radio button selection to change state - reactjs - Stack Overflow
Your handleRadioButton needs to be informed which button was clicked. I suggest a simple function parameter. i.e.. onChange={() => this.
Read more >
react-form-mobx - npm
import { Input, Checkbox, Radio, Select, TextArea } from 'react-form-mobx';.
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