1. 程式人生 > 實用技巧 >react實現的一個待辦事項管理小程式

react實現的一個待辦事項管理小程式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>任務管理</title>
    <style>
    #root{
        width: 
100%; position: absolute; left: 0; top: 0; } </style> </head> <body> <div id="root"></div> <script src="https://cdn.bootcss.com/react/16.8.6/umd/react.development.js" crossorigin></script> <script src="https://cdn.bootcss.com/react-dom/16.8.6/umd/react-dom.development.js" crossorigin></script> <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.js"></script> <script type="text/babel"> /*
CSS start */ const body = { width: '90%', marginLeft: '5%' }; const left = { display: 'inline-block', width: '70%', textAlign: 'left', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace:
'nowrap' }; const right = { width: '30%', textAlign: 'right' }; /* CSS end */ let storage = globalThis.localStorage; if(!storage.getItem('id')){ storage.setItem('id', '1'); storage.setItem('c', '[]'); storage.setItem('u', '[]'); } let ID = parseInt(storage.getItem('id')); class AddUntreated extends React.Component{ constructor(props){ super(props); this.state = { value: '' }; this.add = this.add.bind(this); this._add = this._add.bind(this); this.valueChange = this.valueChange.bind(this); } shouldComponentUpdate(nextProps, nextState){ if(this.props.addu == nextProps.addu && this.state.value == nextState.value){ return false; } return true; } add(){ if(this.state.value == ''){ alert('請輸入待辦事項'); return void 0; } this.props.addu({ id: ID, text: this.state.value }); ++ID; storage.setItem('id', ID + ''); this.setState({ value: '' }); } _add(e){ if(e.keyCode === 13){ this.add(); } } valueChange(event){ this.setState({ value: event.target.value }); } render(){ return ( <div style={body}> <h3>新增待辦事項</h3> <input type="text" onKeyUp={this._add} value={this.state.value} onChange={this.valueChange} /> <button onClick={this.add}>新增</button> </div> ); } } class Untreated extends React.Component { constructor(props){ super(props); this.complete = this.complete.bind(this); } shouldComponentUpdate(nextProps, nextState){ if(this.props.complete == nextProps.complete && this.props.list == nextProps.list){ return false; } return true; } complete(index){ this.props.complete(index); } render(){ return ( <div style={body}> <h3>待辦事項列表</h3> <ul> {this.props.list.map( (item, index) => { return ( <li key={item.id} title={item.text}> <span style={left}>{item.text}</span> <span style={right}> <button onClick={this.complete.bind(this, index)}>完成</button> </span> </li> ); } )} </ul> </div> ); } } class Complete extends React.Component { constructor(props){ super(props); this.deleteC = this.deleteC.bind(this); } shouldComponentUpdate(nextProps, nextState){ if(this.props.deleteC == nextProps.deleteC && this.props.list == nextProps.list){ return false; } return true; } deleteC(index){ this.props.deleteC(index); } render(){ return ( <div style={body}> <h3>已完成事項列表</h3> <ul> {this.props.list.map( (item, index) => { return ( <li key={item.id} title={item.text}> <span style={left}>{item.text}</span> <span style={right}> <button onClick={this.deleteC.bind(this, index)}>刪除</button> </span> </li> ); } )} </ul> </div> ); } } class Matter extends React.Component { constructor(props){ super(props); this.state = { uList: JSON.parse(storage.getItem('u')), //待辦事項列表 cList: JSON.parse(storage.getItem('c')) //已完成事項列表 }; this.empty = this.empty.bind(this); this.adduListItem = this.adduListItem.bind(this); this.complete = this.complete.bind(this); this.deleteC = this.deleteC.bind(this); } shouldComponentUpdate(nextProps, nextState){ if(this.state.uList == nextState.uList && this.state.cList == nextState.cList){ return false; } return true; } empty(){ //清空 ID = 1; this.setState({ uList: [], cList: [] }); storage.setItem('id', '1'); storage.setItem('c', '[]'); storage.setItem('u', '[]'); } adduListItem(item){ //新增事項加入待辦事項列表uList this.setState(function(state, props){ return { uList: [...state.uList, item] }; }, function(){storage.setItem('u', JSON.stringify(this.state.uList));}); } complete(index){ //把完成的這項事項從 待辦事項列表uList中刪除,加入已完成事項列表cList this.setState(function(state, props){ let tempC = [...state.cList, {id: state.uList[index].id, text: state.uList[index].text}], tempU = [...state.uList]; tempU.splice(index, 1); storage.setItem('c', JSON.stringify(tempC)); storage.setItem('u', JSON.stringify(tempU)); return { cList: tempC, uList: tempU }; }); } deleteC(index){ //把刪除的這項事項從 已完成事項列表cList中移除 this.setState(function(state, props){ let temp = [...state.cList]; temp.splice(index, 1); storage.setItem('c', JSON.stringify(temp)); return { cList: temp }; }); } render(){ return ( <React.Fragment> <br /><br /> <div style={body}><button onClick={this.empty}>清空</button></div> <AddUntreated addu={this.adduListItem} /> <Untreated list={this.state.uList} complete={this.complete} /> <Complete list={this.state.cList} deleteC={this.deleteC} /> </React.Fragment> ); } } ReactDOM.render( <Matter />, document.getElementById('root') ); </script> </body> </html>
歡迎react使用經驗豐富的朋友為我指出程式的不足之處