-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathAddPlayerInput.js
More file actions
executable file
·52 lines (45 loc) · 1.26 KB
/
AddPlayerInput.js
File metadata and controls
executable file
·52 lines (45 loc) · 1.26 KB
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
import React, { Component } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import styles from './AddPlayerInput.css';
class AddPlayerInput extends Component {
render() {
return (
<input
type="text"
autoFocus={true}
className={classnames('form-control', styles.addPlayerInput)}
placeholder="Type the name of a player"
value={this.state.name}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)}
/>
);
}
constructor(props, context) {
super(props, context);
this.state = {
name: this.props.name || '',
};
}
handleChange(e) {
this.setState({ name: e.target.value });
}
handleSubmit(e) {
const name = e.target.value.trim();
const {playersByIdList} =this.props
if (e.which === 13) {
// 应为通过名称来匹配删除,所以名称不可重复
if(playersByIdList.some((item,index)=>item.name===name)){
return alert('名称不可重复')
}
this.props.addPlayer(name);
this.setState({ name: '' });
}
}
}
AddPlayerInput.propTypes = {
addPlayer: PropTypes.func.isRequired,
playersByIdList: PropTypes.array.isRequired,
};
export default AddPlayerInput;