所以我要MDC finially與之反應工作,並使用谷歌文檔我曾指出,我一些如何需要reactjs運行以下JS時,在輸入欄上,用戶點擊 - basiclly標籤應該移動輸入以上,使用戶可以鍵入 - 就像我的純HTML/JS演示https://codepen.io/russellharrower/pen/oeReEN材質設計組件及reactjs輸入字段
與reactjs我發現我不能把下面的代碼在Render區域不過,所以我想知道在那裏我會放置如何?
(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/[email protected]/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;
最新更新 我在然而加入這兩個函數出於某種原因(函數(){})();腳本的時候,它會做一個警告等它不會允許的文本標籤移動像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/[email protected]/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);
};
您可以將javascript代碼添加到您的componentDidMount中。它會在組件呈現 – bennygenel
後運行,謝謝我在腳本運行時添加了componentWillMount部件,似乎不喜歡我們正在追加腳本的事實。 是他們爲了讓它加載第一? – RussellHarrower
我能夠使它成爲onClick函數,但是,我確定這不是谷歌的想法? – RussellHarrower