forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LevelSelector.jsx
61 lines (51 loc) · 1.54 KB
/
LevelSelector.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
import PropTypes from 'prop-types';
import React from 'react';
import { selectionList } from './style/SettingsPane.scss';
const MAX_LEVEL = 7;
export default class LevelSelector extends React.Component {
static propTypes = {
inline: PropTypes.bool.isRequired,
text: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = { textValue: this.props.value };
}
render() {
const Wrapper = this.props.inline ? 'span' : 'ul';
const Label = this.props.inline ? 'label' : 'li';
const onChange = (event) => {
if (event.target.value == '') {
this.setState({ textValue: event.target.value });
return;
}
// We take the value mod 10, so that we can make the latest entered digit override
// the field value
let value = parseInt(event.target.value) % 10;
if (Number.isNaN(value) || value < 0) {
value = 0;
} else if (value > MAX_LEVEL) {
value = MAX_LEVEL;
}
this.setState({ textValue: value });
this.props.onChange(value);
};
const handleFocus = event => event.target.select();
return (
<Wrapper className={selectionList}>
<Label>{this.props.text}</Label>
<input
name="scenario_level"
type="text"
pattern="[0-9]*"
inputMode="numeric"
value={this.state.textValue}
onChange={onChange}
onFocus={handleFocus}
/>
</Wrapper>
);
}
}