var RecordField = React.createClass({
render: function() {
return (
{this.props.label}
{this.props.children}
);
}
});
var RecordFlag = React.createClass({
render: function() {
return (
{this.props.text}
);
}
});
var RecordInputField = React.createClass({
getInitialState: function() {
return {
autocomplete: { options: [], current: -1 }
}
},
render: function() {
return (
{this.renderAutocomplete()}
);
},
renderAutocomplete: function() {
if (this.state.autocomplete.options.length > 0) {
return (
{this.renderAutocompleteOptions()}
)
}
},
renderAutocompleteOptions: function() {
var result = [];
for (var ix = 0; ix < this.state.autocomplete.options.length; ++ix) {
if (ix == this.state.autocomplete.current) {
result.push({this.state.autocomplete.options[ix]} )
} else {
result.push({this.state.autocomplete.options[ix]} )
}
}
return result;
},
autoLiStyle: {
lineHeight: "1.8em",
whiteSpace: "nowrap",
cursor: "pointer"
},
onAutocomplete: function(text, event) {
this.props.onChange({ target: { value: text }}); // Almost an event. ;)
this.setState({ autocomplete: {
options: [],
current: -1
}
});
},
onChange: function(event) {
if (this.props.autocomplete) {
this.state.autocomplete.options = event.target.value.autocomplete(this.props.autocomplete);
this.state.autocomplete.current = -1;
}
this.props.onChange(event);
},
onKeyDown: function(event) {
if (event.keyCode == 40) {
this.state.autocomplete.current += 1;
this.setState({ autocomplete: this.state.autocomplete })
} else if (event.keyCode == 38) {
this.state.autocomplete.current -= 1;
this.setState({ autocomplete: this.state.autocomplete })
} else if (event.keyCode == 13 && this.state.autocomplete.current >= 0 && this.state.autocomplete.current < this.state.autocomplete.options.length) {
this.onAutocomplete(this.state.autocomplete.options[this.state.autocomplete.current]);
}
// event.keyCode
// 40 - ArrowDown
// 38 - ArrowUp
// 13 - Enter
console.debug("RecordInputField.onKeydown()", event.keyCode, event.key);
},
onBlur: function() {
setTimeout(function() {
this.setState({ autocomplete: {
options: [],
current: -1
}
});
}.bind(this), 500)
}
});
var RecordTextField = React.createClass({
render: function() {
return (
{this.props.label}
);
}
});
var RecordForm = React.createClass({
render: function() {
return (
{this.emptyOption()}
Övriga
Volvo
Scania
Mercedes
{this.emptyOption()}
Övriga
Baklastare
Sidlastare
Brandbil
{this.renderTextField()}
)
},
renderTextField: function() {
if (this.props.text == "true") {
return (
);
}
},
emptyOption: function() {
if (this.props.emptyOptions == "true") {
return (
);
}
},
updateRecord: function(field, event) {
this.props.record[field] = event.target.value;
this.props.onUpdate(this.props.record);
},
hasFlag: function(flag) {
if (this.props.record.Flags) {
for (var ix = 0; ix < this.props.record.Flags.length; ++ix) {
if (this.props.record.Flags[ix] == flag) {
return true;
}
}
}
return false;
},
setFlag: function(flag, event) {
if (!this.props.record.Flags) {
this.props.record.Flags = [];
}
if (event.target.checked) {
this.props.record.Flags.push(flag);
} else {
this.props.record.Flags.splice(this.props.record.Flags.indexOf(flag), 1);
}
this.props.onUpdate(this.props.record);
},
});