Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set a default value for SelectField #81

Closed
dealvz opened this issue Jan 3, 2017 · 4 comments
Closed

Can't set a default value for SelectField #81

dealvz opened this issue Jan 3, 2017 · 4 comments

Comments

@dealvz
Copy link

dealvz commented Jan 3, 2017

As the title says, I'm trying to set an initial value for my select field component and it does not show up as selected when rendered.

If working, example should be added to the live demo.

@lube
Copy link

lube commented Jan 3, 2017

Having the same problem as we speak going to try to dig into it.

@lube
Copy link

lube commented Jan 3, 2017

Ok found my issue I was passing immutable values to menuitems and my model had an inmutable value too so they are never the same.

Still don't know which is the clearest way to resolve this, probably something like having a function or prop to determine equality is the cleanest but it involves material-ui

@lube
Copy link

lube commented Jan 3, 2017

Quick hack using ramda, you probably want to wrap select field to avoid boilerplate

    <Field
      name='foo'
      component={SelectField}
      hintText='Select Foo'
      floatingLabelText='Foo'
      format={value => findIndex(propEq('id', value.id))(fooList)}
      normalize={value => fooList[value]}
      fullWidth
    >
      {fooList.map((fooElement, i) => (
        <MenuItem
          value={i}
          primaryText={fooElement.name}
        />
      ))}
    </Field>

Edit: I ended up with something like this

    <Field
      name='foo'
      component={SelectField}
      hintText='Foo'
      floatingLabelText='Foo'
      format={value => value && value.hasOwnProperty('id') ? find(propEq('id', value.id))(fooList) : null}
      fullWidth
    >
      {fooList.map((foo) => (
        <MenuItem
          key={foo.id}
          value={foo}
          primaryText={foo.name}
        />
      ))}
    </Field>

@mihirsoni
Copy link
Collaborator

This library doesn't allow defaultValues.

You can read more about this here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants