Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If I click outside of the DateTimePicker popup onAccept is fired #15661

Closed
2 tasks done
drmax24 opened this issue Nov 28, 2024 · 1 comment
Closed
2 tasks done

If I click outside of the DateTimePicker popup onAccept is fired #15661

drmax24 opened this issue Nov 28, 2024 · 1 comment
Labels
component: pickers This is the name of the generic UI component, not the React module! duplicate This issue or pull request already exists

Comments

@drmax24
Copy link

drmax24 commented Nov 28, 2024

If I click outside of the DateTimePicker popup
onAccept is fired

I want onAccept to be fired only if I press OK. As far as I understand this should be an expected behavior.
If I add a cancel button though and if I click cancel then onAccept is not fired which is good.

import dayjs from "dayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { DateTimePicker as BaseDateTimePicker } from "@mui/x-date-pickers/DateTimePicker";
import "dayjs/locale/de";
import { useState } from "react";
import { DateTimePickerSlotsComponentsProps } from "@mui/x-date-pickers/DateTimePicker/DateTimePicker.types";

export const DateTimePicker = ({
    label,
    value,
    onChange,
    onAccept,
    minDateTime,
    disablePast = true,
    slotProps: slotProps,
}: {
    label: string;
    value: string | undefined;
    onChange?: (value: dayjs.Dayjs | null) => void;
    onAccept?: (value: dayjs.Dayjs | null) => void;
    required?: boolean;
    minDateTime?: string | null;
    disablePast?: boolean;
    slotProps?: DateTimePickerSlotsComponentsProps<dayjs.Dayjs>;
}) => {
    const [tempDate, setTempDate] = useState(value ? dayjs(value) : null);

    const mergedSlotProps = {
        ...{
            toolbar: {
                hidden: true,
            },
            textField: {
                sx: {
                    "& .MuiInputBase-input": {
                        padding: "8px",
                    },
                },
            },
        },
        ...slotProps,
    };

    return (
        <LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale="de">
            <BaseDateTimePicker
                label={label}
                defaultValue={tempDate}
                onChange={onChange}
                onAccept={(newValue) => {
                    if (newValue) {
                        setTempDate(newValue);
                        onAccept?.(newValue);
                    }
                }}
                ampm={false}
                disablePast={disablePast}
                minDateTime={minDateTime ? dayjs(minDateTime) : undefined}
                slotProps={mergedSlotProps}
            />
        </LocalizationProvider>
    );
};
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Expected Behavior 🤔

Your Environment 🌎

| Tech | Version |
"@mui/x-date-pickers@^6":
version "6.20.2"
"@mui/[email protected]":
version "5.16.7"
next@^13.1.3:
version "13.1.6"

Search keywords:

@oliviertassinari oliviertassinari transferred this issue from mui/material-ui-pickers Nov 28, 2024
@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 28, 2024
@oliviertassinari oliviertassinari added the component: pickers This is the name of the generic UI component, not the React module! label Nov 28, 2024
@michelengelen
Copy link
Member

michelengelen commented Nov 28, 2024

Duplicate of #15646

@michelengelen michelengelen marked this as a duplicate of #15656 Nov 28, 2024
@github-actions github-actions bot added duplicate This issue or pull request already exists and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 28, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2024
@michelengelen michelengelen marked this as a duplicate of #15646 Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: pickers This is the name of the generic UI component, not the React module! duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants