In a code like
html
<FormField>
<Textfield id="firstname" label="First Name" />
</FormField>
the generated label tag gets "4px" as its padding-left style, which leads to an ugly look.
@fterradev, RMWC doesn't contain any of its own styles, it simply wraps the original material components provided by Google https://github.com/material-components/material-components-web/.
You are not required to wrap Textfields in the FormField component. According to the MDC docs, it's a helper for layout and RTL support. https://material.io/components/web/catalog/input-controls/form-fields/
Thank you.
I was trying to migrate from pure vanilla HTML/JS/CSS components to rmwc.
So I've followed this example from https://github.com/material-components/material-components-web/blob/master/docs/getting-started.md
And was trying to convert it to rmwc, which I thought could be:
````jsx
import React, { Component } from 'react';
import { Button, Typography, FormField, Textfield } from 'rmwc';
class App extends Component {
render() {
return (
<form action="#" id="greeting-form">
<div>
<FormField>
<Textfield id="firstname" label="First Name" />
</FormField>
<FormField>
<Textfield id="lastname" label="Last Name" />
</FormField>
</div>
<Button raised>Print Greeting</Button>
</form>
<Typography use="headline" tag="p"></Typography>
</main>
);
}
}
export default App;
````
But this way it shifts the line under the text field by 4px to the right. It can be seen as the green band below, to the left.

But I expected it to look like the vanilla version:

If I don't wrap the Textfields into FormFields, they get joined:

A between them will do it. But I think it would be very nice if my React code can match my vanilla code as much as possible.
Furthermore without the FormField as wrapper the font gets bigger.
It's also worth mentioning that rmwc generates the textfield as something like:
html
<label class="mdc-textfield mdc-textfield--upgraded"><input type="text" class="mdc-textfield__input" id="firstname"><span class="mdc-textfield__label">First Name</span></label>
And it seems like I'm not able to make it generate as:
````html
</div>
````
So the very root element is not what a user could need. The root element is label, even if the label attribute is undefined.
Thank you for your attention.
Thank you for more detail. MDC has multiple recommended ways to build the textfield markup. Let me see if using the other one fixes your issue.
@fterradev dug into this for you.
Turns out, the TextField component should not be placed inside of FormField per MDCs documentation. The FormField component only exists to provide some functionality when wrapping generic input label combos.
Textfield to TextFieldJust a heads up, I had a botched deploy with RC2 and missed some classnames. Make sure you upgrade to RC3.
Man, bad day for RMWC. That breaking name changed really screwed me up. 3 releases later they should be working as billed... Use RC4.
Seems to me like the FormField should work somehow with MDC input elements like TextField by reading documentation at
https://github.com/material-components/material-components-web/tree/master/packages/mdc-form-field
on MDCFormField.input, where it's assigned an MDCRadio instance.
Not sure though.
There documentation isn't always the most up to date. Even though they say it should work (and it really should), I found the reason why.

The selector they use is looking for a direct child. The TextField component has a div wrapper. I'll file a but with Material.
Ok.
Hey, rmwc now gives an error when wrapping TextField with FormField.
Code:
jsx
<FormField>
<TextField id="firstname" label="First Name" />
</FormField>
Error:
脳
TypeError: Cannot read property 'addEventListener' of null
registerInteractionHandler
C:/.../node_modules/@material/form-field/dist/mdc.formField.js:1007
1004 |
1005 | return new __WEBPACK_IMPORTED_MODULE_1__foundation__["a" /* default */]({
1006 | registerInteractionHandler: function registerInteractionHandler(type, handler) {
> 1007 | return _this2.label_.addEventListener(type, handler);
1008 | },
1009 | deregisterInteractionHandler: function deregisterInteractionHandler(type, handler) {
1010 | return _this2.label_.removeEventListener(type, handler);
View compiled
MDCFormFieldFoundation.init
C:/.../node_modules/@material/form-field/dist/mdc.formField.js:1131
1128 | _createClass(MDCFormFieldFoundation, [{
1129 | key: 'init',
1130 | value: function init() {
> 1131 | this.adapter_.registerInteractionHandler('click', this.clickHandler_);
1132 | }
1133 | }, {
1134 | key: 'destroy',
View compiled
MDCFormField.MDCComponent
C:/.../node_modules/@material/form-field/dist/mdc.formField.js:260
257 | // this.root_ is defined and can be used within the foundation class.
258 | /** \@protected {!F} */
259 | this.foundation_ = foundation === undefined ? this.getDefaultFoundation() : foundation;
> 260 | this.foundation_.init();
261 | this.initialSyncWithDOM();
262 | }
263 |
I know. Did you see the bug I just filed with MDC? This isn鈥檛 an issue on my end.
Ok.
Yes, I saw it, but I still thought it was something with rmwc. I mean, I thought rmwc would just not be able to render it accordingly due to that issue, but I didn't thought it would cause such error.
And now I've just noticed that in fact FormField is giving error whenever it has no direct child label tag.
BTW, remember to replace the for attribute by an htmlFor one in https://jamesmfriedman.github.io/rmwc/form-fields.
Cool, will do.
The reason it causes the error is because all of RMWC uses the Foundation javascript classes, and the error is with the actual MDC FormField constructor.
If you're just trying to layout with the form-field class, you can do the following:
<div className="mdc-form-field">
<TextField .../>
</div>
I could also just catch any errors caused by the MDC constructors to insulate against them. To my knowledge this is the only situation where one has broken.
@fterradev
I added a try catch around the MDC constructor. The bug still exists inside of MDC, but now at least RMWC will fail gracefully. Currently, it will put a warning in the console but your component will still render, it just won't have any of the MDC foundation class functionality.