Reactで編集機能を作りたいです。
this.stateにediting=falseを設定して、クリックするとediting=trueに切り替わりselect,inputにクリックした内容が表示されるという動きにしたいのですがどうしたらよいでしょうか。
Javascript
1import React from 'react'; 2import Loop from './Loop'; 3import './list.css' 4import EditForm from './EditForm' 5 6const fixed= ["月曜日","火曜日","水曜日","木曜日","金曜日"] 7 8class App extends React.Component{ 9 constructor(props) { 10 super(props); 11 this.state = { 12 lists:[], 13 input:"", 14 value:"", 15 days:"", 16 editing:false 17 } 18 this.handleChange = this.handleChange.bind(this); 19 this.handleSubmit = this.handleSubmit.bind(this); 20 this.handleChangeDays = this.handleChangeDays.bind(this); 21 this.handleDelete = this.handleDelete.bind(this); 22 this.handleChangeEditing = this.handleChangeEditing.bind(this); 23 } 24 25 render () { 26 const {editing}=this.state 27 return ( 28 <div> 29 <h1>時間割</h1> 30 <form> 31 <select 32 defaultValue="---" 33 value={this.state.days} 34 onChange={this.handleChangeDays}> 35 <option>---</option> 36 <option value="月曜日">月曜日</option> 37 <option value="火曜日">火曜日</option> 38 <option value="水曜日">水曜日</option> 39 <option value="木曜日">木曜日</option> 40 <option value="金曜日">金曜日</option> 41 </select> 42 43 <select 44 defaultValue="---" 45 value={this.state.value} 46 onChange={this.handleChangeOrders}> 47 <option>---</option> 48 <option value="1限:">1限</option> 49 <option value="2限:">2限</option> 50 <option value="3限:">3限</option> 51 <option value="4限:">4限</option> 52 <option value="5限:">5限</option> 53 </select> 54 55 {editing ? ( 56 <EditForm 57 text={this.state.input}/> 58 ):( 59 <div> 60 <input 61 onChange={this.handleChange} 62 type="text" 63 value={this.state.input} 64 /> 65 66 <button onClick={this.handleSubmit}>保存</button> 67 <button onClick={this.handleDelete}>削除</button> 68 </div> 69 )} 70 </form> 71 72 73 <div className="p-list"> 74 {fixed.map((l) =>( 75 <div className="list" key={l.toString()}> 76 <p>{l}</p> 77 <p> 78 <Loop 79 onClick={this.handleChangeEditing} 80 lists={this.state.lists.filter(item => item.days === l)} 81 /> 82 </p> 83 </div> 84 ))} 85 </div> 86 87 </div> 88 ); 89 } 90 91 handleChangeEditing () { 92 this.setState({ 93 editing: !this.state.editing 94 }) 95 } 96 97 handleDelete () { 98 this.setState({ 99 input: this.state.input, 100 value: this.state.value, 101 days: this.state.days}) 102 } 103 104 handleChange = (event) => { 105 this.setState({input:event.target.value}) 106 } 107 handleChangeDays = (event) => { 108 this.setState({days:event.target.value}) 109 } 110 handleChangeOrders = (event) => { 111 this.setState({value:event.target.value}) 112 } 113 114 handleSubmit (e) { 115 e.preventDefault(); 116 if (!this.state.input) return; 117 this.setState( 118 { 119 lists: [ 120 ...this.state.lists, 121 { 122 input: this.state.input, 123 value: this.state.value, 124 days: this.state.days 125 }], 126 input:"", 127 days:"", 128 value:"" 129 }); 130 }; 131 132} 133 134export default App;
Javascript
1import React from 'react'; 2 3class Loop extends React.Component { 4 render () { 5 return ( 6 <div> 7 {this.props.lists.map((l) =>( 8 <div key={l.days}> 9 <div onClick={this.props.onClick}>{l.value}{l.input}</div> 10 </div> 11 ))} 12 </div> 13 ); 14 } 15 16} 17 18export default Loop;
Javascript
1import React from 'react'; 2 3class EditForm extends React.Component { 4 constructor (props) { 5 super(props); 6 this.state = { 7 text: props.text 8 }; 9 10 this.handleChange = this.handleChange.bind(this); 11 this.handleSubmit = this.handleSubmit.bind(this); 12 this.handleChangeDays = this.handleChangeDays.bind(this); 13 this.handleDelete = this.handleDelete.bind(this); 14 this.handleChangeEditing = this.handleChangeEditing.bind(this); 15 } 16 render(){ 17 return ( 18 <div> 19 <input 20 onChange={this.handleChange} 21 type="text" 22 value={this.state.text} 23 /> 24 25 <button onClick={this.handleSubmit}>保存</button> 26 <button onClick={this.handleDelete}>削除</button> 27 </div> 28 ); 29 } 30 31 handleDelete () { 32 this.setState({ 33 input: this.state.input, 34 value: this.state.value, 35 days: this.state.days}) 36 } 37 38 handleChange = (event) => { 39 this.setState({text:event.target.value}) 40 } 41 handleChangeDays = (event) => { 42 this.setState({days:event.target.value}) 43 } 44 handleChangeOrders = (event) => { 45 this.setState({value:event.target.value}) 46 } 47 48 handleSubmit (e) { 49 e.preventDefault(); 50 if (!this.state.text) return; 51 this.setState( 52 { 53 lists: [ 54 ...this.state.lists, 55 { 56 input: this.state.input, 57 value: this.state.value, 58 days: this.state.days 59 }], 60 input:"", 61 days:"", 62 value:"" 63 }); 64 }; 65} 66export default EditForm;