Merge pull request #6 from RandellDawson/feature/search-with-enter-keypress

Added the ability to search by pressing Enter. Associated refactoring.
This commit is contained in:
Randell Dawson
2018-11-20 12:56:07 -08:00
committed by mrugesh mohapatra
parent 3c5203e97d
commit 6b63642d3d
2 changed files with 17 additions and 7 deletions

View File

@ -22,17 +22,26 @@ class App extends Component {
inputRef = React.createRef(); inputRef = React.createRef();
handleInputChange = (event) => { handleInputEvent = (event) => {
const { value } = event.target; const { type, key, target: { value } } = event;
if (Number(value) || value === '') { if (type === 'change') {
this.setState((prevState) => ({ number: value, foundPRs: [] })); if (Number(value) || value === '') {
this.setState((prevState) => ({ number: value, foundPRs: [] }));
}
}
else if (type === 'keypress' && key === 'Enter') {
this.searchPRs(value);
} }
} }
handleButtonClick = () => { handleButtonClick = () => {
const { number } = this.state; const { number } = this.state;
this.searchPRs(number);
}
searchPRs = (number) => {
fetch(`https://pr-relations.glitch.me/pr/${number}`) fetch(`https://pr-relations.glitch.me/pr/${number}`)
.then((response) => response.json()) .then((response) => response.json())
.then(({ ok, foundPRs }) => { .then(({ ok, foundPRs }) => {
@ -52,12 +61,12 @@ class App extends Component {
} }
render() { render() {
const { handleButtonClick, handleInputChange, inputRef, state } = this; const { handleButtonClick, handleInputEvent, inputRef, state } = this;
const { number, foundPRs } = state; const { number, foundPRs } = state;
return ( return (
<Container> <Container>
<Input ref={inputRef} value={number} onInputChange={handleInputChange} /> <Input ref={inputRef} value={number} onInputEvent={handleInputEvent} />
<button onClick={handleButtonClick}>Search</button> <button onClick={handleButtonClick}>Search</button>
<Results foundPRs={foundPRs} /> <Results foundPRs={foundPRs} />
</Container> </Container>

View File

@ -4,7 +4,8 @@ const Input = React.forwardRef((props, ref) => (
<input <input
type="text" type="text"
placeholder="PR #" placeholder="PR #"
onChange={props.onInputChange} onChange={props.onInputEvent}
onKeyPress={props.onInputEvent}
value={props.value} value={props.value}
ref={ref} ref={ref}
/> />