I have written functional based component i am checking the type in the switch case ,depending on the type i am creating the form here
const component = (props) => {
let renderObject = null
let obj;
const renderdata = () =>{
console.log(props);
obj = Object.keys(props.todoprop).map((todoproprties) =>{
console.log("here we are looking at the data" , props.todoprop[todoproprties])
let toDoProprties = props.todoprop[todoproprties]
switch(props.todoprop[todoproprties].type){
case 'text':
return <input type="text" key="1" value={toDoProprties.value} name = {toDoProprties.name} onChange={props.handleChange} />
case 'date':
return <input type="date" key="2" value={toDoProprties.value} onChange={props.handleChange}/>
default:
return <textarea rows="10" key="3" cols="30" rows="10" cols="30" value={toDoProprties.value} onChange={props.handleChange}/>
}
})
}
return (
<div>
<h1>Hello from component </h1>
{renderdata()}
<div className="card">
<div className="card-body">
<div>
{obj}
<button type="submit" onClick={props.handleSubmit}>Submit</button>
</div>
</div>
</div>
{console.log(props)}
</div>
);
}
export default component;
Below is the container in which am rendering my component
class container extends Component {
state = {
// how i want my data to look with a heading with date proprtey and a description
heading: '',
date: '',
description: '',
Todo: {
heading: { type: "text", value: "", placeholder: 'plese enter heading', name:"heading"},
date: { type: "date", value: "", placeholder: "plese enter date", name: "date" },
description: { value: "PLESE DESIGN A GOOD TODO", placeholder: 'plese enter description', name: "description"}
}
}
onChangeHandler = (event) =>{
console.log(event.value)
this.setState({
[event.target.name] : event.target.value
})
// this.setState({Todo.heading.value: event.target.value })
}
onSubmitHandler = () =>{
console.log("you have succesfully clicked the handler")
console.log("you are seeing the state" ,this.state.heading);
console.log("you are seeing the description", this.state.description)
}
render() {
var dataPassingHandler = () => {
return (<div>
<Dynamiccomponent todoprop={this.state.Todo} handleChange={this.onChangeHandler} handleSubmit={this.onSubmitHandler}/>
</div>
)
}
return (
<div>
{dataPassingHandler()}
</div>
)
}
}
export default container;
1) In my container i am rendering the component and i am passing the type to the container and i am checking in the switch case i am creating the form
2) I am stuck here with two problems First one is how to pass the onChange ,
3) Second how to handle the onclick and create a new object in state of the container