因此,我终于让 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);
};