Skip to content
Snippets Groups Projects
Commit c9c02efd authored by Martin Wighus Holtmon's avatar Martin Wighus Holtmon
Browse files

Modified states to the one displayed in class

parent fa2a2318
No related branches found
No related tags found
No related merge requests found
...@@ -9,50 +9,34 @@ import './States.css'; ...@@ -9,50 +9,34 @@ import './States.css';
class States extends React.Component { class States extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
console.log('window.cs142models.statesModel()', window.cs142models.statesModel()); this.state = {
states: window.cs142models.statesModel().sort(),
displayedStates: window.cs142models.statesModel().sort(),
searchValue: ''
};
//console.log('window.cs142models.statesModel()', this.state.states);
} }
findStates(event) { onInputChange = (event) => {
function matchStates(value, statesModel) { this.setState({
let results = []; searchValue: event.target.value,
statesModel.forEach(e => { displayedStates: this.state.states.filter((usStates) => usStates.toLowerCase().includes(event.target.value.toLowerCase()))
if (e.toLowerCase().includes(value)) results.push(e)
}); });
return results;
}
let out = document.getElementById("content"); //Get table to edit
out.innerHTML = ""; //Clear table
// Exit
if (event.target.value == "") {
return "";
}
// Get matching states
let results = matchStates(event.target.value, window.cs142models.statesModel());
// Update dive
if (results.length == 0) {
out.innerHTML = "<tr><td>There are no matching states to this string!</td></tr>";
} else {
out.innerHTML = "<tr><th>States</th></tr>"
results.forEach(e => {
out.innerHTML += "<tr><td>" + e.toString() + "</td></tr>"
})
}
} }
render() { render() {
return ( return (
<div> <div>
<label htmlFor="inName">Name: </label> <input value={this.state.searchValue} onChange={this.onInputChange} />
<input type="text" placeholder="country" id="inName" onChange={this.findStates}/> <div>{this.state.searchValue}</div>
<table> {this.state.displayedStates.length === 0 ?
<tbody id="content"> <div>No matches</div> :
</tbody> <ul>
</table> {this.state.displayedStates.map((usState) =>
<li key={usState}>{usState}</li>
)}
</ul>
}
</div> </div>
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment