Here I'm providing my code where I want to disable spacebar in textbox? I'm setting an alert message when entering spacebar in textbox, till here all is going well. But I want that after clicking 'OK' of an alert message the screen should be same like as it was before entering spacebar. I mean it(screen) should get loaded with all the data it was holding initially with asking a user to input something in textbox.
What my code is providing a screen with a cursor(next to spacebar) in textbox asking to enter the next input. I think it's taking a spacebar and most importantly screen is not getting roll back to its original state after clicking on 'OK' of alert message. Please help me to fix the issue.
CODE
import React from "react";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
term: "",
names: [
{ name: "Roony", about: "He is a student" },
{ name: "Rocky", about: "He is a player" },
{ name: "Ronny", about: "He is a singer" }
],
filteredData: [{}]
};
}
render() {
let terms = "";
if (this.state.term) {
terms = this.state.term.toLowerCase();
}
return (
<div className="App">
<label>Search Person: </label>
<input
type="text"
value={this.state.name}
id="searchEmp"
placeholder="Enter Person's Name"
onChange={(event) => {
if (event.target.value.indexOf(" ") > -1) {
alert("space not allowed.");
this.setState({ term: "" });
return;
}
this.setState({ term: event.target.value });
}}
/>
<br />
<br />
<hr />
{this.state.names &&
this.state.names
.filter((x) => x.name.toLowerCase().startsWith(terms))
.map((item) => {
return (
<div className="data-body">
<div>{item.name}</div>
<div>{item.about}</div>
</div>
);
})}
</div>
);
}
}
export default App;
The same I want to do when I change my data from 'array of objects' to an 'array' as names: ["Elon Musk","Bill Gates","Tim Cook","Richard Branson","Jeff Bezos","Warren Buffet","The Zuck","Carlos Slim","Bill Gates","Larry Page","Harold Finch","Sergey Brin","Jack Ma","Steve Ballmer","Phil Knight","Paul Allen","Woz"]. What changes are required to make and Where when using the same code as above? Thanks in advance to all helpers.