淺談React實現輸入框

來源:互聯網
上載者:User

組件要實現的功能:

1、定製label和輸入框的placeholder的內容;

2、輸入的內容不符合Regex要求時,輸入框標紅,符合時標綠;

3、點擊清除按鈕後,按鈕消失並清除輸入框內容;

4、對外開放3個方法:擷取、設定、刪除輸入框的內容(trim)。


inputBox.html:

<!DOCTYPE html><html><head><link rel="stylesheet" href="inputBox.css"><script src="build/react.js"></script><script src="build/react-dom.js"></script><script src="build/browser.min.js"></script></head><body><div id="inputBox"></div><script type="text/babel" src="inputBox.jsx"></script></body></html>


inputBox.jsx:

var grades = [{team: "Team1", name: "Alice", grade: 80},{team: "Team1", name: "Bruce", grade: 90},{team: "Team1", name: "Cindy", grade: 50},{team: "Team2", name: "David", grade: 40},{team: "Team2", name: "Emy", grade: 60},{team: "Team2", name: "Fruid", grade: 70}];var FilterBox = React.createClass({getInitialState: function() {return {inputText: "",showPassingOnly: false};},filter: function(inputText, showPassingOnly) {this.setState({inputText: inputText,showPassingOnly: showPassingOnly});},render: function() {return (<div><SearchBox onInput={this.filter} inputText={this.state.inputText} showPassingOnly={this.state.showPassingOnly} /><GradeList grades={this.props.grades} inputText={this.state.inputText} showPassingOnly={this.state.showPassingOnly} /></div> );}});var SearchBox = React.createClass({filter: function() {this.props.onInput(this.refs.input.value,this.refs.checkbox.checked);},render: function() {return (<form><input id="text" type="text" ref="input" placeholder="Sreaching..." onChange={this.filter} value={this.props.inputText}/><br/><input type="checkbox" ref="checkbox" checked={this.props.showPassingOnly} onChange={this.filter}/>Only show passing grades</form>);}});var GradeList = React.createClass({render: function() {var rows = [];var teams = [];this.props.grades.forEach(function(grade, index) {if (grade.name.toLowerCase().indexOf(this.props.inputText) == -1 || this.props.showPassingOnly && grade.grade < 60)return;if (teams.indexOf(grade.team) == -1) {rows.push(<Team team={grade.team} key={"team" + index}></Team>);teams.push(grade.team);}rows.push(<Grade name={grade.name} grade={grade.grade} key={"grade" + index}></Grade>);}.bind(this));if (rows.length == 0) rows.push(<tr key={"noMatches"}><td colSpan="2">No matches!</td></tr>);return (<table><thead><tr><th>Name</th><th>Grade</th></tr></thead><tbody>{rows}</tbody></table>);}});var Team = React.createClass({render: function() {return (<tr><td colSpan="2">{this.props.team}</td></tr>);}});var Grade = React.createClass({render: function() {return (<tr><td>{this.props.name}</td><td>{this.props.grade}</td></tr>);}});ReactDOM.render(<FilterBox grades={grades} />,document.getElementById('filterBox'));
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.