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.

Material UI SelectField onChange not working

See original GitHub issue

Hi, I have implemented mobx-react-form successfully in my project but now I’m trying to get a SelectField working.

This is what I have.

// MaterialSelectFieldComponent.js

import React from 'react';
import {observer} from 'mobx-react';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';

export default observer(({field}) => (
  <div>
    <SelectField {...field.bind()} >
      {field.options.map(({value,label}) => {
        return (<MenuItem key={value} value={value} primaryText={label} />)
      })}
    </SelectField>
  </div>
));
// bindings
  MaterialSelectField: {
    id: 'id',
    name: 'name',
    value: 'value',
    label: 'floatingLabelText',
    disabled: 'disabled',
    error: 'errorText',
    onChange: 'onChange',
  }
// fields
    a_dummy_field: {
      label: 'Nice Field',
      rules: 'required',
      bindings: 'MaterialSelectField',
      options: [{value:1, label: 'Yes'}]
    }

and <MaterialSelectField field={form.$('a_dummy_field')} />

With this the form, the field and its options are rendered, but selecting one option does not set the value in the select field. Am I missing something? Thanks in advance.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:39 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
foxhound87commented, Aug 21, 2017

React Native does not have an event object passed to the onChange, as you can see in the official doc, you need to use onChangeText, this should work without bindings:

const handleOnChange = (field) => (text) => {
  field.set(text);
};

<TextInput 
  {...field.bind({ onChange: () => {} })}
  onChangeText: {handleOnChange(field)}
/>
1reaction
ricbermocommented, Feb 23, 2017

I’m so sorry @foxhound87 I tried again, just updated the package version to 1.20.0 and now this is working

const onChange = field => (e, k, payload) => {
  field.set(payload);
};

where payload is the actual value returned from SelectField. I was my mistake.

Thank you so much

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material ui select onchange is not bubbling - Stack Overflow
The event that triggers the change does bubble, but it is not a "change" event. The onChange function that you provide gets called...
Read more >
on change for Material UI Select component not triggered #322
What you did: I am trying to fire the onChange method of the Material UI Select. ... fireEvent.change not working on input elements...
Read more >
React Select component - Material UI - MUI
It's enabled with the multiple prop. Like with the single selection, you can pull out the new value by accessing event.target.value in the...
Read more >
firing change event for material-ui select - CodeSandbox
firing change event for material-ui select. 1. Embed Fork Create Sandbox Sign in. Sandbox Info. firing change event for material-ui select.
Read more >
How to use Material UI Select in React - Refine Dev
We'll discover the Material UI (MUI) select component with examples. ... It does not, however, inherit the Select variant value.
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