Simplify further

Also fix not-i18n-friendly "of" to be "=".
This commit is contained in:
Luke Barnard 2017-11-14 13:06:54 +00:00
parent c3492634bd
commit 3fa1bece0a

View File

@ -59,14 +59,14 @@ module.exports = React.createClass({
}, },
componentWillMount: function() { componentWillMount: function() {
this._initStateFromProps(this.props, true); this._initStateFromProps(this.props);
}, },
componentWillReceiveProps: function(newProps) { componentWillReceiveProps: function(newProps) {
this._initStateFromProps(newProps); this._initStateFromProps(newProps);
}, },
_initStateFromProps: function(newProps, initial) { _initStateFromProps: function(newProps) {
// This needs to be done now because levelRoleMap has translated strings // This needs to be done now because levelRoleMap has translated strings
const levelRoleMap = Roles.levelRoleMap(newProps.usersDefault); const levelRoleMap = Roles.levelRoleMap(newProps.usersDefault);
const options = Object.keys(levelRoleMap).filter((l) => { const options = Object.keys(levelRoleMap).filter((l) => {
@ -76,69 +76,53 @@ module.exports = React.createClass({
this.setState({ this.setState({
levelRoleMap, levelRoleMap,
options, options,
custom: levelRoleMap[newProps.value] === undefined,
}); });
if (initial) {
this.setState({
custom: levelRoleMap[newProps.value] === undefined,
});
}
}, },
onSelectChange: function(event) { onSelectChange: function(event) {
this.setState({ custom: event.target.value === "SELECT_VALUE_CUSTOM" }); this.setState({ custom: event.target.value === "SELECT_VALUE_CUSTOM" });
if (event.target.value !== "SELECT_VALUE_CUSTOM") { if (event.target.value !== "SELECT_VALUE_CUSTOM") {
this.props.onChange(this.getValue()); this.props.onChange(event.target.value);
} }
}, },
onCustomBlur: function(event) { onCustomBlur: function(event) {
this.props.onChange(this.getValue()); this.props.onChange(parseInt(this.refs.custom.value));
}, },
onCustomKeyDown: function(event) { onCustomKeyDown: function(event) {
if (event.key == "Enter") { if (event.key == "Enter") {
this.props.onChange(this.getValue()); this.props.onChange(parseInt(this.refs.custom.value));
} }
}, },
getValue: function() {
if (this.refs.custom) {
return parseInt(this.refs.custom.value);
}
if (this.refs.select) {
return this.refs.select.value;
}
return undefined;
},
render: function() { render: function() {
let customPicker; let customPicker;
if (this.state.custom) { if (this.state.custom) {
let input;
if (this.props.disabled) { if (this.props.disabled) {
input = <span>{ _t( customPicker = <span>{ _t(
"Custom of %(powerLevel)s", "Custom of %(powerLevel)s",
{ powerLevel: this.props.value }, { powerLevel: this.props.value },
) }</span>; ) }</span>;
} else { } else {
input = <input customPicker = <span> = <input
ref="custom" ref="custom"
type="text" type="text"
size="3" size="3"
defaultValue={this.props.value} defaultValue={this.props.value}
onBlur={this.onCustomBlur} onBlur={this.onCustomBlur}
onKeyDown={this.onCustomKeyDown} onKeyDown={this.onCustomKeyDown}
/>; />
</span>;
} }
customPicker = <span> of { input }</span>;
} }
let selectValue; let selectValue;
if (this.state.custom) { if (this.state.custom) {
selectValue = "SELECT_VALUE_CUSTOM"; selectValue = "SELECT_VALUE_CUSTOM";
} else { } else {
selectValue = this.state.levelRoleMap[selectValue] ? selectValue = this.state.levelRoleMap[this.props.value] ?
this.props.value : "SELECT_VALUE_CUSTOM"; this.props.value : "SELECT_VALUE_CUSTOM";
} }
let select; let select;