I want to create a date component on a child component and show on the screen (on the parent component) the selected date. Moreover I have two problems:
- I am not managing to make the components talk with each other. I can print on the console the answer from the child component but I cannot pass to the parent component
- When I select date "custom" on my dropdown I want to select a start and end date but I can't do it, when I select one of the dates (start or the end) the other updates itself automatically.
Any help is welcome :)
Here is my codesandbox: https://codesandbox.io/s/date-picker-forked-h1u9s?file=/src/App.tsx
This is how I call the date component
export default function App() {
return (
<div>
<DateComponent />
<div>Your chosen date was: </div>
</div>
);
}
This is the date component:
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<div className="App">
<div>
<ReactSelect
value={selectedOption as ValueType<OptionType>}
onChange={(option) => handleChange(option)}
isMulti={false}
options={options}
/>
{selectedOption === DateValueEnum.Custom ? (
<div style={{ display: "flex" }}>
<div style={{ width: "50%", float: "left", paddingRight: "5px" }}>
<DatePicker
fullWidth
margin="normal"
required={true}
error={false}
invalidLabel={"Several values..."}
value={selectedDate}
onChange={(newDate) => setSelectedDate(newDate)}
format="MM/dd/yyyy"
/>
</div>
<DatePicker
fullWidth
margin="normal"
required={true}
error={false}
invalidLabel={"Several values..."}
value={selectedDate}
onChange={(newDate) => setSelectedDate(newDate)}
format="MM/dd/yyyy"
/>
</div>
) : null}
</div>
</div>
</MuiPickersUtilsProvider>
);
};