[React]簡易留言板
效果如圖:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
<script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
<script src="https://cdn.bootcss.com/babel-core/5.8.24/browser.js"></script>
<link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="./css/css.css">
</head>
<body>
<div id="test" class="container"></div>
<script type="text/babel" src="./js/app.js"></script>
</body>
</html>
/**
* @author: wilcohuang
* @date: 2017/12/11
* @time: 9:28
*/
/*
ReactDOM.render(
<div>
<button className="btn btn-default">Add</button>
<ul className="list-group">
<li className="list-group-item">Cras justo odio <a className="right"
clsssName="glyphicon glyphicon-edit" href=""></a>
<a className="right glyphicon glyphicon-remove-circle" href="#"></a></li>
<li className="list-group-item">
<input className="item-edit" type="text"/> <a className="glyphicon glyphicon-share"
href="#"></a>
<a className="right glyphicon glyphicon-remove-circle" href="#"></a>
</li>
</ul>
</div>, document.getElementById('test')
);*/
//Item
const Item = React.createClass({
remove: function () {
this.props.onRemove(this.props.id);
},
edit: function() {
this.props.onEdit(this.props.id, this.props.value);
},
render: function () {
return <li className="list-group-item">{this.props.value}<a className="right glyphicon glyphicon-edit" href="#" onClick={this.edit}></a>
<a className="right glyphicon glyphicon-remove-circle" href="#" onClick={this.remove}></a></li>;
},
});
//ItemEditor
const ItemEditor = React.createClass({
getInitialState: function () {
return {
value: this.props.value,
}
},
changeHandle: function (event) {
// this.state.value = event.target.value;
// this.forceUpdate();
this.setState({value: event.target.value});
},
remove: function () {
this.props.onRemove(this.props.id);
},
save: function () {
this.props.onSave(this.props.id, this.state.value);
},
render: function () {
return <li className="list-group-item">
<input className="item-edit" type="text" value={this.state.value} onChange={this.changeHandle}/> <a
className="glyphicon glyphicon-share" onClick={this.save}
href="#"></a>
<a className="glyphicon glyphicon-remove-circle" href="#" onClick={this.remove}></a>
</li>;
}
});
//List
const List = React.createClass({
getInitialState: function () {
return {
key: 0,
list: new Map(),
editList: new Map(),
};
},
add: function () {
this.state.editList.set(++this.state.key, '');
this.forceUpdate();
},
removeItem: function (id) {
this.state.list.delete(id);
this.forceUpdate();
},
removeItemEditor: function (id) {
this.state.editList.delete(id);
this.forceUpdate();
},
save: function (id, value) {
this.state.editList.delete(id);
this.state.list.set(id, value);
this.forceUpdate();
},
edit: function(id, value) {
this.state.list.delete(id) ;
this.state.editList.set(id, value);
this.forceUpdate();
},
render: function () {
const listDOM = [];
const editListDOM = [];
for (let entity of this.state.list) {
listDOM.push(<Item id={entity[0]} key={entity[0]} value={entity[1]}
onRemove={this.removeItem} onEdit={this.edit}></Item>);
}
for (let entity of this.state.editList) {
editListDOM.push(<ItemEditor id={entity[0]}
key={entity[0]} value={entity[1]}
onRemove={this.removeItemEditor} onSave={this.save}></ItemEditor>)
}
return <div>
<button onClick={this.add} className="btn btn-default">Add</button>
<ul className="list-group">
{listDOM}
{editListDOM}
</ul>
</div>;
}
});
ReactDOM.render(<List></List>, document.getElementById('test'));
.right {
float: right;
}
.list-group-item a {
margin-left: 10px;
}
.item-edit {
outline: none;
border-width: 0 0 1px 0;
}
相關文章
- JS簡易留言板JS
- JavaScript簡易留言板效果JavaScript
- Flask學習之旅--簡易留言板Flask
- JSP語言做簡易留言板JS
- React簡易版老虎機React
- 手寫簡易版React框架React框架
- JavaScript簡單留言板效果JavaScript
- 基於React搭建一個簡易版豆瓣React
- 基於react的hash路由簡易實現React路由
- react筆記--手動實現一個react-router(簡易版)React筆記
- react/vue中dom-diff簡易版實現ReactVue
- web前端技術分享:使用react實現簡易路由Web前端React路由
- React 全家桶實現一個簡易備忘錄React
- 留言板
- 用React實現一個簡易的markdown部落格應用React
- 使用react-router0-config時的簡易守衛React
- 基於React跑一個簡易版九宮格抽獎React
- 簡易ApiAPI
- 簡易方程
- CodeIgniter編寫的簡單留言板的DEMO
- React-Redux 100行程式碼簡易版探究原理。(面試熱點,React Hook + TypeScript實現)ReactRedux行程面試HookTypeScript
- [React Native]使用App Center CLI釋出CodePush更新--iOS簡易版React NativeAPPiOS
- flutter簡易教程Flutter
- nginx簡易教程Nginx
- FastDFS簡易概括AST
- 簡易JSONJSON
- 簡易操作statspack
- 簡易版管道模式模式
- Flutter 動畫簡易教程Flutter動畫
- namedtuple簡易實現
- 手寫簡易PromisePromise
- 手寫簡易webpackWeb
- Solidity 簡易教程Solid
- CMake簡易入門
- HBase簡易遷移
- c#簡易 logC#
- c#簡易logC#
- gdb 簡易入門