forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScenarioSelector.jsx
56 lines (46 loc) · 1.34 KB
/
ScenarioSelector.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
import PropTypes from 'prop-types';
import React from 'react';
import { SCENARIO_DEFINITIONS } from './scenarios';
import { selectionList } from './style/SettingsPane.scss';
export default class ScenarioSelector extends React.Component {
static propTypes = {
text: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = { textValue: this.props.value };
}
render() {
const onChange = (event) => {
if (event.target.value == '') {
this.setState({ textValue: event.target.value });
return;
}
let value = parseInt(event.target.value);
if (Number.isNaN(value) || value < 0) {
value = 0;
} else if (value >= SCENARIO_DEFINITIONS.length) {
value = SCENARIO_DEFINITIONS.length;
}
this.setState({ textValue: value });
this.props.onChange(value);
};
const handleFocus = event => event.target.select();
return (
<ul className={selectionList}>
<li>{this.props.text}</li>
<input
name="scenario_number"
type="text"
pattern="[0-9]*"
inputMode="numeric"
value={this.state.textValue}
onChange={onChange}
onFocus={handleFocus}
/>
</ul>
);
}
}