I have a component that is responsible for a date time picker. I wish that another parent component call this child date picker component so I can have a component of "wrapper/container" and one just for the data picker. Moreover I am not having success on doing this on typescript.
Here is my code sandbox: https://codesandbox.io/s/date-picker-ubosl?file=/src/App.tsx
This is my code, The function handleChangeDateType
do a console.log() in the end with the results that should be passed to the Parent component.
import React, { useState } from "react";
import { DatePicker, MuiPickersUtilsProvider } from "@material-ui/pickers";
import ReactSelect, { ValueType } from "react-select";
import DateFnsUtils from "@date-io/date-fns";
export enum DateValueEnum {
Today = "TODAY",
Yesterday = "YESTERDAY",
Custom = "CUSTOM"
}
type OptionType = {
key: string;
label: string;
};
const options: OptionType[] = [
{ key: DateValueEnum.Today, label: "Today" },
{ key: DateValueEnum.Yesterday, label: "Yesterday" },
{ key: DateValueEnum.Custom, label: "Custom" }
];
export default function App() {
const [selectedDate, setSelectedDate] = React.useState<Date | null>(
new Date()
);
const [selectedOption, setSelectedOption] = useState<ValueType<OptionType>>();
const handleChangeDateType = (value: string) => {
let startDate: Date | undefined = undefined;
let endDate: Date | undefined = undefined;
const startToday = new Date();
switch (value) {
case DateValueEnum.Today: {
startDate = startToday;
endDate = new Date(startDate.getTime());
break;
}
case DateValueEnum.Yesterday: {
startDate = new Date(startToday.getTime());
startDate.setDate(startDate.getDate() - 1);
endDate = new Date(startDate.getTime());
break;
}
default: /** nothing */
}
console.log(startDate);
console.log(endDate);
};
const handleChange = (option: ValueType<OptionType>) => {
setSelectedOption(option.key);
handleChangeDateType(option.key);
};
return (
<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" }}>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<DatePicker
fullWidth
margin="normal"
required={true}
error={false}
invalidLabel={"Several values..."}
value={selectedDate}
onChange={(newDate) => setSelectedDate(newDate)}
format="MM/dd/yyyy"
/>
</MuiPickersUtilsProvider>
</div>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<DatePicker
fullWidth
margin="normal"
required={true}
error={false}
invalidLabel={"Several values..."}
value={selectedDate}
onChange={(newDate) => setSelectedDate(newDate)}
format="MM/dd/yyyy"
/>
</MuiPickersUtilsProvider>
</div>
) : null}
</div>
</div>
);
}
Edit: To answer the original question, what I would do is to extract the states out of the DateComponent and have them in the parent, so you can pass the dates and the onChange function from the parent to the date component.
After that in your DateComponent you need to do this:
Here's a forked sandbox if you want to take a look: https://codesandbox.io/s/date-picker-forked-fyt7t