-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
74 lines (61 loc) · 1.66 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React from 'react';
import { connect } from 'react-redux';
import RowHero from '../RowHero';
import * as actions from '../../redux/actions';
class ListHeroes extends React.Component {
constructor(props) {
super(props);
this.state = {
stateOfHeroes: {},
input: ''
};
this.isOpen = this.isOpen.bind(this);
this.isChange = this.isChange.bind(this);
this.isClear = this.isClear.bind(this);
}
componentWillReceiveProps(nextProps) {
let stateOfHeroes = {};
nextProps.heroes.forEach(hero => {
stateOfHeroes[hero.name] = false;
});
this.setState({ stateOfHeroes });
}
isOpen(name, value) {
this.setState({ stateOfHeroes: {...this.state.stateOfHeroes, [name]: value} })
this.props.addName(name);
}
isChange(e) {
this.setState({ input: e.target.value })
}
isClear(e) {
this.setState({ input: ''})
}
render() {
const { heroes } = this.props;
const comp = heroes.map((item) => {
return (<RowHero key={item.name} hero={item} state={this.state.isOpen} isOpen={this.isOpen}/>)
});
return (
<div>
<ul>
{ comp }
</ul>
<input onChange={this.isChange} onBlur={this.isClear} value={this.state.input} type='text' />
<p>{this.state.input}</p>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
heroes: state.items.results,
hasErrored: state.itemsHasErrored,
isLoading: state.itemsIsLoading
};
};
const mapDispatchToProps = (dispatch) => {
return {
addName: (data) => dispatch(actions.addName(data))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(ListHeroes);