So i have MDC finially working with React and using the Google docs I have worked out that I some how need reactjs to run the following JS when a user clicks on an input field - basiclly the label should move above the input so the user can type - like my pure HTML / JS demo https://codepen.io/russellharrower/pen/oeReEN
However with reactjs I noticed I can't put the code below in the render area, so i am wondering where would I place it and how?
(function() {
var tfs = document.querySelectorAll(
'.mdc-textfield:not([data-demo-no-auto-js])'
);
for (var i = 0, tf; tf = tfs[i]; i++) {
mdc.textfield.MDCTextfield.attachTo(tf);
}
})();
full reactjs code updated
import React, { Component } from 'react';
import './App.css';
import * as mdc from 'material-components-web';
class App extends Component {
hrefjs(l){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = l; // use this for linked script
document.body.appendChild(script);
}
componentWillMount() {
this.hrefjs("//unpkg.com/material-components-web@latest/dist/material-components-web.min.js");
var texst ="(function(){var tfs = document.querySelectorAll('.mdc-textfield:not([data-demo-no-auto-js])'); for (var i = 0, tf; tf = tfs[i]; i++) {mdc.textfield.MDCTextfield.attachTo(tf); mdc.autoInit();}})();"
const script = document.createElement("script");
script.text=texst;
document.body.appendChild(script);
};
render() {
return (
<div className="App">
<header className="mdc-toolbar mdc-toolbar--fixed mdc-toolbar--waterfall">
<div className="mdc-toolbar__row">
<section className="mdc-toolbar__section mdc-toolbar__section--align-start">
<span className="mdc-toolbar__title">Title</span>
</section>
</div>
</header>
<main className="demo-main mdc-toolbar-fixed-adjust">
<div className="mdc-form-field">
<div className="mdc-checkbox">
<input type="checkbox"
id="my-checkbox"
className="mdc-checkbox__native-control"/>
<div className="mdc-checkbox__background">
<svg className="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path className="mdc-checkbox__checkmark__path"
fill="none"
stroke="white"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div className="mdc-checkbox__mixedmark"></div>
</div>
</div>
<label htmlhtmlhtmlFor="my-checkbox" id="my-checkbox-label">This is my checkbox</label>
</div>
<section className="example">
<div className="mdc-textfield">
<input type="text" id="username" className="mdc-textfield__input" aria-controls="username-helptext"/>
<label htmlFor="username" className="mdc-textfield__label">Username</label>
</div>
<p id="username-helptext" className="mdc-textfield-helptext" aria-hidden="true">
This will be displayed on your public profile
</p>
</section>
</main>
<script>
mdc.toolbar.MDCToolbar.attachTo(document.querySelector('.mdc-toolbar'));
//mdc.textfield.MDCToolbar.attachTo(document.querySelector('.mdc-textfield'));
//mdc.autoInit();
</script>
</div>
);
}
}
export default App;
Latest update I have added these two functions in however for some reason the (function(){})(); script while it will do an alert etc. it will not allow the text label to move like the codepen.io demo
hrefjs(l){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = l; // use this for linked script
document.body.appendChild(script);
}
componentWillMount() {
this.hrefjs("//unpkg.com/material-components-web@latest/dist/material-components-web.min.js");
var texst ="(function(){var tfs = document.querySelectorAll('.mdc-textfield:not([data-demo-no-auto-js])'); for (var i = 0, tf; tf = tfs[i]; i++) {mdc.textfield.MDCTextfield.attachTo(tf); mdc.autoInit();}})();"
const script = document.createElement("script");
script.text=texst;
document.body.appendChild(script);
};
Even if your approach works in the end somehow. It does really not look like a very pleasant way to continue developing and your code will be a nightmare for others to read later on (or yourself). It can actually be done much easier. I am usually writing react.js wrappers for the components I need from MDC. Here is an example for the textfield:
You can check out the official mdc demo for react.js here (https://github.com/material-components/material-components-web/tree/master/framework-examples/react).
For styling, I am including the mdc sccs files into my scss files and change the values I need. e.g.: