Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
フォームに入力した要素をテーブルに追加できるようになる
Browse files Browse the repository at this point in the history
  • Loading branch information
kadoyau committed Sep 5, 2018
1 parent 6314cc2 commit e8aeebc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
27 changes: 9 additions & 18 deletions my-app/src/App.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import React, {Component} from 'react';
import Table from './Table';
import Form from "./Form";

class App extends Component {
state = {
characters: [
{
'name': 'Charlie',
'job': 'Janitor'
},
{
'name': 'Mac',
'job': 'Bounce'
},
{
'name': 'Dee',
'job': 'Aspiring actress'
},
{
'name': 'Dennis',
'job': 'Bartender'
}
]
characters: []
};

removeCharacter = index => {
Expand All @@ -33,12 +17,19 @@ class App extends Component {
})
};

handleSubmit = character => {
this.setState({
characters: [...this.state.characters, character]
});
}

render() {
return (
<div className="container">
<Table characterData={this.state.characters}
removeCharacter={this.removeCharacter}
/>
<Form handleSubmit={this.handleSubmit}/>
</div>
);
}
Expand Down
58 changes: 58 additions & 0 deletions my-app/src/Form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, {Component} from 'react';

class Form extends Component {
constructor(props) {
super(props);

this.initialState = {
name: '',
job: ''
};

this.state = this.initialState;
}

handleChange = event => {
const {name, value} = event.target;

this.setState({
[name]: value
})
// nameにしてしまうとオブジェクトリテラルのキーがname固定になってしまう。配列リテラルにするとnameが評価される
};

submitForm = () => {
this.props.handleSubmit(this.state);
this.setState(this.initialState);
};

render() {
const {name, job} = this.state;

return (
<form>
<label>Name</label>
<input
type="text"
name="name"
value={name}
onChange={this.handleChange}
/>
<label>Job</label>
<input
type="text"
name="job"
value={job}
onChange={this.handleChange}
/>
<input
type="button"
value="Sumbit"
onClick={this.submitForm}
/>
</form>
);
}
}

export default Form;
3 changes: 1 addition & 2 deletions my-app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {Component} from 'react';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';


ReactDOM.render(<App/>, document.getElementById('root'));

0 comments on commit e8aeebc

Please sign in to comment.