0

因此,我终于让 MDC 使用 React 并使用 Google 文档,我发现当用户单击输入字段时,我需要 reactjs 来运行以下 JS - 基本上标签应该移动到输入上方,以便用户可以键入- 就像我的纯 HTML / JS 演示https://codepen.io/russellharrower/pen/oeReEN

但是使用 reactjs 我注意到我无法将下面的代码放在渲染区域中,所以我想知道我将它放在哪里以及如何放置?

(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);
        }
      })();

完整的 reactjs 代码已更新

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;

最新更新 我已经添加了这两个函数但是出于某种原因 (function(){})(); 脚本,虽然它会发出警报等,但它不允许文本标签像 codepen.io 演示那样移动

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);
};
4

1 回答 1

2

即使您的方法最终以某种方式起作用。它看起来确实不是一种非常愉快的继续开发方式,并且您的代码将成为其他人(或您自己)以后阅读的噩梦。它实际上可以更容易地完成。我通常为 MDC 所需的组件编写 react.js 包装器。这是文本字段的示例:

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

您可以在此处查看 react.js 的官方 mdc 演示(https://github.com/material-components/material-components-web/tree/master/framework-examples/react)。

对于样式,我将 mdc sccs 文件包含到我的 scss 文件中并更改我需要的值。例如:

$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";
于 2017-10-11T08:24:12.150 回答