0

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);
};
3
  • you can add javascript code to your componentDidMount. It will run after the component rendered Commented Sep 6, 2017 at 20:52
  • thanks I have added componentWillMount part any while the script runs it seems not to like the fact we are appending the script. is their away to make it load first? Commented Sep 6, 2017 at 21:59
  • i was able to get it to work by, making it an onClick function but, i am sure that's not what google had in mind? Commented Sep 6, 2017 at 22:19

1 Answer 1

2

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:

import React from 'react';
import { MDCRipple } from '@material/ripple/dist/mdc.ripple';

class MdcButton extends React.Component {
    constructor(props) {
        super(props);
        // Init state
        this.state = {}
    }

    componentDidMount() {
        if (this.props.ripple)
            MDCRipple.attachTo(this.refs.button);
    }

    static defaultProps = {
        ripple: true,
        raised: true,
        disabled: false,
        className: "",
        type: "submit"
    }

    render() {
        let className = "mdc-button " + this.props.className;
        if (this.props.raised)
            className += " mdc-button--raised";

        return (
            <button
                ref="button"
                id={this.props.id}
                className={className}
                type={this.props.type}
                onClick={this.props.onClick}
                disabled={this.props.disabled}
            >
                {this.props.children}
            </button>
        );
    }
}

export default MdcButton

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.:

$mdc-theme-primary: #404040;
$mdc-theme-accent: #a349a3;
$mdc-theme-background: #fff;  

@import "@material/ripple/mdc-ripple";
@import "@material/typography/mdc-typography";
@import "@material/theme/mdc-theme";
@import "@material/button/mdc-button";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.