Skip to content

Instantly share code, notes, and snippets.

@mhaagens
Last active May 27, 2017 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhaagens/1d49f42c7e396f4649c8faa29d05c4eb to your computer and use it in GitHub Desktop.
Save mhaagens/1d49f42c7e396f4649c8faa29d05c4eb to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
class AutoComplete extends Component {
constructor(props) {
super(props)
this.state = {
query: ""
}
}
_handleChange({target: {value}}) {
this.setState({
query: value
})
// Do api-call etc...
}
_getData = () => this.state.query
render() {
return <input
name="query"
placeholder="Autocomplete search..."
onChange={(e) => this._handleChange(e)}
value={this.state.query}
/>
}
}
class AnotherInput extends Component {
constructor(props) {
super(props)
this.state = {
query: ""
}
}
_handleChange({target: {value}}) {
this.setState({
query: value
})
// Do api-call etc...
}
_getData = () => this.state.query
render() {
return <input
name="query"
placeholder="Elasticsearch query..."
onChange={(e) => this._handleChange(e)}
value={this.state.query}
/>
}
}
class Form extends Component {
_getData() {
const children = ["autocomplete", "anotherinput"];
const data = [];
for (let child of children) {
let obj = {
value: this[child]._getData();
}
data[child] = obj;
}
console.log(data);
}
render() {
return <form style={{display: "flex", flexFlow: "column"}}>
<AutoComplete ref={e => this.autocomplete = e} />
<AnotherInput ref={e => this.anotherinput = e} />
<button onClick={(e) => {
e.preventDefault();
this._getData();
}}>Get data</button>
</form>
}
}
export default Form;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment