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.

bug(select): floating label does not float if value is not string

See original GitHub issue

If <Select> value prop is not a string mdc-web will not add the mdc-floating-label--float-above class and the label will sit atop the text [0]. Select expects its value to be either a string | string[] | number. I believe it should only be string to conform to HTMLSelectElement[1]. This differs from the react select implementation which expands the interface to be string | string[] | number [2].

Specifically this occurs in the foundation handleChange method [3]. To determine whether it should call adapter.floatLabel it calls

const optionHasValue = value.length > 0;

which if it was a number value.length would return undefined and would evaluate to false. Additionally string[] would be misleading. First because the Select component does not support multiple values. Second, because e.target.value will only ever return the first selected value.

Why not file an mdc-web issue?

Mainly because this is the proper behavior of the HTMLSelectElement. the html5 spec indicates that the value of select should only be a string[1].

Proposed solution

Change value to only accept string. Additionally, I believe HTMLSelectElement only allows value on the DOMElement[4] and not the ContentAttributes[5]. React explicitly defines value [2]… Therefore, assuming I understand it correctly, the type signature for value should also be defined in the Props interface for both <Select> and <NativeControl>.


I spent too much time I don’t have going down this rabbit hole. I don’t regret it 😃

[0] https://imgur.com/DLCJQFu [1] https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element [2] https://github.com/DefinitelyTyped/DefinitelyTyped/blob/bdb87d49e550e600cb30658a9542bfe83337eaf9/types/react/index.d.ts#L2077-L2088 [3] https://github.com/material-components/material-components-web/blob/3cc4c6aa48ae8c1a307df542911e28a7808de41f/packages/mdc-select/foundation.js#L141-L162 [4] https://html.spec.whatwg.org/multipage/dom.html#concept-element-dom [5] https://html.spec.whatwg.org/multipage/dom.html#concept-element-attributes

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mgr34commented, Mar 7, 2019

Great–I’m glad I do not need to explain further. 😃 I was having a hard time putting it into words as I was rushing out the door yesterday evening.

I will prepare a PR shortly.

1reaction
moog16commented, Mar 7, 2019

@mgr34 I understand the differences between DomInterface & Attributes. They have the same syntax and look the same, but the browser and other frameworks treat them differently.

Since MDC Web has value strictly as string, I’m fine with changing it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mat-form-field floatLabel="never" not working - Stack Overflow
Managed to come up with the following scss to imitate the label not floating:
Read more >
Input label does not move up when value or placeholder is sp
I am trying to use mdbootstrap in my meteor app but am running into issues with forms: the label does not move above...
Read more >
Floating labels · Bootstrap v5.0
Create beautifully simple form labels that float over your input fields.
Read more >
<input type="number"> - HTML: HyperText Markup Language
A string value of any means that no stepping is implied, and any value is allowed (barring other constraints, such as min and...
Read more >
Input | Quasar Framework
Fills string with specified characters (or underscore if value is not string) to ... error. : Boolean. Description. Does field have validation errors?...
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