boardgame.io
Version:
library for turn-based games
71 lines (62 loc) • 1.58 kB
JavaScript
/*
* Copyright 2018 The boardgame.io Authors.
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import React from 'react';
import PropTypes from 'prop-types';
class LobbyLoginForm extends React.Component {
static propTypes = {
playerName: PropTypes.string,
onEnter: PropTypes.func.isRequired,
};
static defaultProps = {
playerName: '',
};
state = {
playerName: this.props.playerName,
nameErrorMsg: '',
};
render() {
return (
<div>
<p className="phase-title">Choose a player name:</p>
<input
type="text"
value={this.state.playerName}
onChange={this.onChangePlayerName}
onKeyPress={this.onKeyPress}
/>
<span className="buttons">
<button className="buttons" onClick={this.onClickEnter}>
Enter
</button>
</span>
<br />
<span className="error-msg">
{this.state.nameErrorMsg}
<br />
</span>
</div>
);
}
onClickEnter = () => {
if (this.state.playerName === '') return;
this.props.onEnter(this.state.playerName);
};
onKeyPress = event => {
if (event.key === 'Enter') {
this.onClickEnter();
}
};
onChangePlayerName = event => {
const name = event.target.value.trim();
this.setState({
playerName: name,
nameErrorMsg: name.length > 0 ? '' : 'empty player name',
});
};
}
export default LobbyLoginForm;