Beginner React/Typescript learner here, I am trying to improve this class I have:
import * as React from 'react';
import {Form, IFields, isEmail, required} from "../../components/Form";
import {Field} from "../../components/Field";
const API = '/api/getmonth';
export interface Props {
}
interface State {
data: string[],
isLoading: boolean,
error: any,
}
class monthForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
data: [],
isLoading: false,
error: null,
};
}
componentDidMount() {
this.setState({isLoading: true});
fetch(API)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error getting month list');
}
})
.then(content => this.setState({data: content, isLoading: false}))
.catch(error => this.setState({error, isLoading: false}));
}
render() {
const {data, isLoading, error} = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return (
<p>Loading ...</p>
)
}
const fields: IFields = {
jan: {
id: "jan",
label: "Jan",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
feb: {
id: "feb",
label: "Feb",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
mar: {
id: "mar",
label: "Mar",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
apr: {
id: "apr",
label: "Apr",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
may: {
id: "may",
label: "May",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
jun: {
id: "jun",
label: "Jun",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
jul: {
id: "jul",
label: "Jul",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
aug: {
id: "aug",
label: "Aug",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
sep: {
id: "sep",
label: "Sep",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
oct: {
id: "oct",
label: "Oct",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
nov: {
id: "nov",
label: "Nov",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
dec: {
id: "dec",
label: "Dec",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
};
return (
<Form
action="/react/test/form"
fields={fields}
render={() => (
<React.Fragment>
<div className="alert alert-info" role="alert">
Select Projection for each month
</div>
<div className="container">
<div className="row">
<div className="col-md-3">
<Field {...fields.jan}/>
<Field {...fields.feb}/>
<Field {...fields.mar}/>
<Field {...fields.apr}/>
<Field {...fields.may}/>
<Field {...fields.jun}/>
</div>
<div className="col-md-3">
<Field {...fields.jul}/>
<Field {...fields.aug}/>
<Field {...fields.sep}/>
<Field {...fields.oct}/>
<Field {...fields.nov}/>
<Field {...fields.dec}/>
</div>
</div>
</div>
</React.Fragment>
)}
/>
);
}
}
export default monthForm;
particularly this part:
jan: {
id: "jan",
label: "Jan",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
feb: {
id: "feb",
label: "Feb",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
mar: {
id: "mar",
label: "Mar",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
apr: {
id: "apr",
label: "Apr",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
may: {
id: "may",
label: "May",
editor: "dropdown",
options: data,
value: "hello",
validation: {rule: required}
},
This section seems like it could easily be looped over a const [] with month names. But I can't seem to find any reference on how to achieve it.
Any help or point to a reference example would be greatly appreciated!