I'm using React 16.13.0 and material's KeyboardDatePicker component -- https://material-ui-pickers.dev/api/KeyboardDatePicker . I have set it up like so ...
import {
KeyboardDatePicker,
KeyboardTimePicker,
MuiPickersUtilsProvider,
} from "@material-ui/pickers";
...
<KeyboardDatePicker
margin="normal"
id="date-pickUp"
label="Select Date"
format="MM/dd/yyyy"
value={pickUpDateLabel}
onChange={(date) => handleDate(date, "pickUp")}
KeyboardButtonProps={{
"aria-label": "change date",
}}
/>
The thing I'd like to tweak is that when I click on the text field where you can type in the date or click the icon graphic to bring up the date picker, I would like the date picker UI to come up automatically. I'm not sure how to configure things though such that as soon as I click on the text field, the UI for the date picker pops up.
Edit: I'm unable to get a working app up with the code but here's a screen shot of the text field that is rendered by default with the icon at the right ...
Right now you have to click on that icon for the date picker to come up, but I would like to be able to click on the text field and immediately have the date picker appear.
Edit 2: Screen shot in response to answer given ...
To Implement Date Picker on TextField() and TextFormField(): First of all, you need to add intl Flutter package in your dependency to get formatted date output from date picker. Add the following line in your pubspec. yaml file to add intl package to your project.
If the Controls task pane is not visible, click More Controls on the Insert menu, or press ALT+I, C. Under Insert controls, click Date Picker. In the Date Picker Binding dialog box, select the field in which you want to store the date picker data, and then click OK.
Use a calendar date picker to let a user pick a single date from a contextual calendar view. Use it for things like choosing an appointment or departure date. To let a user pick a known date, such as a date of birth, where the context of the calendar is not important, consider using a date picker.
For anyone who uses updated material-ui library (v5), you can use open={bool} attribute to make use of when to open DatePicker.
const [dateOpen,setDateOpen] = useState(false);
const [dueDate,setDueDate] = useState(new Date());
<DatePicker
clearable={true}
open={dateOpen}
onClose={() => setDateOpen(false)}
label="Due Date"
value={dueDate}
minDate={new Date()}
onChange={(newValue) => {
setDueDate(newValue);
}}
renderInput={(params) => (
<TextField
{...params}
onClick={() => setDateOpen(true)}
/>
)}
/>
Multiple issues to handle while solving this:
Input
to control the opening of the DatePicker Popover - has to be a controlled component (which you control the opening of it using a state.open
prop of the KeyboardDatePicker
Input
, and once we have a focus the the Popover will open (not good). We can solve this using the disableRestoreFocus
prop of the Popover. onFocus
of the Input
to open the Popover, but the onClose
of the Popover to actually close it (because we control the open-state of the Popover).onFocus
of the KeyboardButtonProps
.This is the complete code:
const KeyDatePickerContainer = () => {
const [selectedDate, handleDateChange] = useState(null);
const [isOpen, setIsOpen] = useState(false);
return (
<KeyboardDatePicker
variant="inline"
value={selectedDate}
inputVariant="outlined"
label="With keyboard"
format="MM/dd/yyyy"
onChange={newDate => {
handleDateChange(newDate);
}}
KeyboardButtonProps={{
onFocus: e => {
setIsOpen(true);
}
}}
PopoverProps={{
disableRestoreFocus: true,
onClose: () => {
setIsOpen(false);
}
}}
InputProps={{
onFocus: () => {
setIsOpen(true);
}
}}
open={isOpen}
/>
);
};
Here is a link to a working example: https://codesandbox.io/s/material-pickers-open-modal-click-on-text-kjgjk
Update: if you want to also close the DatePicker once the date was selected you can use the onChange
function to not only set the new date, but also close the Popover:
onChange={newDate => {
handleDateChange(newDate);
setIsOpen(false); // Add this
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With