I am developing a React Js website. Where I have a form with 4 select fields Make, Model, Min price and Max price. I have a javascript file which contains make and models of cars. File contains multiple Car Makes. File data is available below which represent Car Make and its available models in array.
export const veh_data = [{"alfa-romeo": ["145", "90", "Alfa 6", "Alfasud"]},
{"aston-martin": ["15", "2-Litre", "AM Vantage", "Atom", "Cygnet", "DB2"]},
{"audi": ["100", "200", "A1", "A2", "A3", "A4", "A5", "A6", "A7" ]}
];
I want to get that data (Make name (e.g audi) and its Models) from this file and display that data as options of Select fields in the form.
Here is my React JS component code:
import React, { Component } from 'react';
import { Form, FormGroup, Input } from 'reactstrap';
import { veh_data } from '../shared/vehicle_make_and_models'
class ImgAndForm extends Component {
constructor(props) {
super(props);
this.handleSearch = this.handleSearch.bind(this);
this.state = {
veh_data: veh_data,
};
}
handleSearch(event) {
alert("Search button clicked");
event.preventDefault();
}
render() {
const veh_make = this.state.veh_data.map((veh) => {
return (
<div>
<option></option>
</div>
);
});
return (
<div>
<header className="headerbg d-flex">
<div className="container my-auto">
<div className="row">
<div className="offset-1 col-10 offset-lg-0 col-lg-4">
<div id="search-form-div" className="container">
<div className="row">
<div className="col-12 my-4">
<h3>Search</h3>
<Form onSubmit={this.handleSearch}>
<FormGroup>
<Input type="select" name="select1" id="select1">
<option value="">Make</option>
{veh_make}
</Input>
</FormGroup>
<FormGroup>
<Input type="select" name="select2" id="select2">
<option value="">Model</option>
{veh_make}
</Input>
</FormGroup>
<FormGroup>
<Input type="select" name="select3" id="select3">
<option value="">Min Price</option>
<option value="500">500</option>
</Input>
</FormGroup>
<FormGroup>
<Input type="select" name="select4" id="select4">
<option value="">Max Price</option>
<option value="2000">2000</option>
</Input>
</FormGroup>
<FormGroup>
<Input type="submit" name="search" id="search" className="btn btn-primary" value="Search" />
</FormGroup>
</Form>
</div>
</div>
</div>
</div>
</div>
</div>
</header>
</div>
);
}
}
export default ImgAndForm;
console.log(veh_data). Does it give you what you expect? Go from there.