Merge pull request #5 from RandellDawson/refactor/input-react-component

Implemented ref forwarding for Input component. Minor clean up.
This commit is contained in:
Randell Dawson
2018-11-20 12:53:47 -08:00
committed by mrugesh mohapatra
parent 1211c2b758
commit 3c5203e97d
2 changed files with 12 additions and 18 deletions

View File

@ -24,7 +24,7 @@ class App extends Component {
handleInputChange = (event) => { handleInputChange = (event) => {
const { value } = event.target; const { value } = event.target;
console.log(Number(value))
if (Number(value) || value === '') { if (Number(value) || value === '') {
this.setState((prevState) => ({ number: value, foundPRs: [] })); this.setState((prevState) => ({ number: value, foundPRs: [] }));
} }
@ -38,7 +38,7 @@ class App extends Component {
.then(({ ok, foundPRs }) => { .then(({ ok, foundPRs }) => {
if (ok) { if (ok) {
if (!foundPRs.length) { if (!foundPRs.length) {
foundPRs.push({number: 'No PRs with matching files', filenames: []}); foundPRs.push({ number: 'No PRs with matching files', filenames: [] });
} }
this.setState((prevState) => ({ foundPRs })); this.setState((prevState) => ({ foundPRs }));
} }
@ -46,25 +46,19 @@ class App extends Component {
this.inputRef.current.focus(); this.inputRef.current.focus();
} }
}) })
.catch((err) => { .catch(() => {
//console.log(err)
this.setState((prevState) => ({ number: '', foundPRs: [] })); this.setState((prevState) => ({ number: '', foundPRs: [] }));
}); });
} }
render() { render() {
const { number, foundPRs } = this.state; const { handleButtonClick, handleInputChange, inputRef, state } = this;
const { number, foundPRs } = state;
return ( return (
<Container> <Container>
<input <Input ref={inputRef} value={number} onInputChange={handleInputChange} />
type="text" <button onClick={handleButtonClick}>Search</button>
placeholder="PR #"
onChange={this.handleInputChange}
value={number}
ref={this.inputRef}
/>
<button onClick={this.handleButtonClick}>Search</button>
<Results foundPRs={foundPRs} /> <Results foundPRs={foundPRs} />
</Container> </Container>
); );

View File

@ -1,13 +1,13 @@
import React from 'react'; import React from 'react';
const Input = ({ onInputChange, value, test }) => ( const Input = React.forwardRef((props, ref) => (
<input <input
type="text" type="text"
placeholder="PR #" placeholder="PR #"
onChange={onInputChange} onChange={props.onInputChange}
value={value} value={props.value}
ref={test} ref={ref}
/> />
); ));
export default Input; export default Input;