Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I spawn my date picker UI upon first clicking on the date text field?

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 ...

enter image description here

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 ...

enter image description here

like image 787
Dave Avatar asked Apr 15 '20 20:04

Dave


People also ask

How do I insert a date picker in TextField in flutter?

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.

How do I insert a date picker in Excel?

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.

What is select a date from a calendar picker?

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.


2 Answers

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)}
                    />
                  )}
                />
like image 21
Adam Farmer Avatar answered Sep 23 '22 21:09

Adam Farmer


Multiple issues to handle while solving this:

  1. Since you want to the focus on the 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.
    This state is actually the open prop of the KeyboardDatePicker
  2. Next issue is that once the Popover is closed - the focus is getting back to the 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.
  3. We need to use the 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).
  4. Lastly - the icon is no longer controlling the opening of the Popover. We need to do this, using the 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
}}
like image 154
Dekel Avatar answered Sep 24 '22 21:09

Dekel