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

Select's required prop doesn't work #63

Closed
fterradev opened this issue Dec 20, 2017 · 5 comments
Closed

Select's required prop doesn't work #63

fterradev opened this issue Dec 20, 2017 · 5 comments
Labels

Comments

@fterradev
Copy link

What RMWC Version are you using?

v0.0.1-rc12

What are the steps to reproduce the bug?

Use the Select tag as defined below inside a form and try to submit it without selecting any option.

<Select
  cssOnly
  required
  name="food"
  placeholder="-- cssOnly --"
  options={['Cookies', 'Pizza', 'Icecream']}
/>

What is the expected behavior?

The form should prevent the submition for this required field has not been filled.

What is the actual behavior?

The form gets inadvertently submitted, with food value of '_placeholder'.

Any other information you believe would be useful?

I guess this regards to cssOnly Select tags only.
I guess the placeholder option should be set with some null value instead of '_placeholder', probably an empty string. And/or maybe let the user set its value.

@jamesmfriedman
Copy link
Collaborator

You are absolutely right. In the meantime, can you try making your own placeholder as one of the select options with a blank value and verify it works the way you think it should?

The required prop should be passed along, so it sounds like it’s just the value.

@fterradev
Copy link
Author

fterradev commented Dec 20, 2017

Fortunately, if the options is a map with non-numerical keys, this can achieved with something like:

<Select
  cssOnly
  name="food"
  required
  options={{
    '': '-- cssOnly --',
    ...{
      'Cookies': 'Cookies',
      'Pizza': 'Pizza',
      'Icecream': 'Icecream'
    }
  }}
/>

Note: if the keys are numerical (and even if they are numerical strings), they appear before our empty string, which makes our placeholder to be the last option, which is undesired.

We can also get the same result with some "automatic" transformation on our initial array with reduce:

  options={{
    '': '-- cssOnly --',
    ...['Cookies', 'Pizza', 'Icecream'].reduce((items, item) => 
      {
        items[item] = item;
        return items;
      }, {}
    )
  }}

Or by building the list with option tags:

<Select
  cssOnly
  name="food"
  required
>
  <option>-- cssOnly --</option>
  {
    ['Cookies', 'Pizza', 'Icecream'].map(item =>
      <option key={item} value={item}>{item}</option>
    )
  }
</Select>

@jamesmfriedman
Copy link
Collaborator

You might just want to use the formatted options from the docs, it’s the first example. The object map is there for convenience but you can’t guarantee ordering with it.

I’ll fix the placeholder in the next release, until then you can use your blank option workaround.

@jamesmfriedman
Copy link
Collaborator

@fterradev check out that commit and verify that it will fix this issue.

@jamesmfriedman
Copy link
Collaborator

This should be fixed by V1

fterradev added a commit to fterradev/reactnd-project-readable-starter that referenced this issue Jan 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants