Skip to content

DateInput cannot correctly display input value when it is controlled #8556

@yoadev22

Description

@yoadev22

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

8.3.10

What package has an issue?

@mantine/dates

What framework do you use?

Vite

In which browsers you can reproduce the issue?

Chrome

Describe the bug

It appears that the DateInput component, when controlled, cannot correctly display the value format correctly (up to HH:mm). The controlled value is still updated correctly. The uncontrolled version interestingly does not have this issue. This is my reproduction of the issue:

import { DateInput, DateInputProps } from "@mantine/dates";
import dayjs from "dayjs";
import { useState } from "react";

const DAYJS_FMT = "YYYY-MM-DD HH:mm";

const dateParser: DateInputProps["dateParser"] = (input) => {
    if (!input) return null;
    const parsed = dayjs(input, DAYJS_FMT, true);
    return parsed.isValid() ? parsed.toDate() : null;
};

export function DateInputBug() {
    const [value, setValue] = useState<string | null>(null);

    return (
        <div className="px-10 py-4 flex flex-col gap-y-4">
            {/* state is updated correctly (UTC string) */}
            <div>{JSON.stringify({ value })}</div>

            {/* input cannot display correctly on HH:mm , it always reverts back to 00:00 */}
            <DateInput
                description="controlled"
                clearable
                valueFormat={DAYJS_FMT}
                dateParser={dateParser}
                value={value}
                onChange={setValue}
            />

            {/* uncontrolled: input displays correctly up to HH:mm */}
            <DateInput
                description="uncontrolled"
                clearable
                valueFormat={DAYJS_FMT}
                dateParser={dateParser}
            />
        </div>
    );
}

Note: I already extend dayjs plugin as per the guideline in my root application file(main.tsx):

import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(timezone);

Please also see the gif here that captures the issue: Click Here

This issue does not appear in older versions, namely v6 (most of our existing projects are still using v6)

If possible, include a link to a codesandbox with a minimal reproduction

No response

Possible fix

It looks like it has something to do with useUncontrolledDates in the source code, which hardcodes the withTime parameter to be false and thus _value will be always translated in the form of YYYY-MM-DD before passing onto valueFormatter . I am not sure about the exact fix as it may involve many moving parts of the component.

Self-service

  • I would be willing to implement a fix for this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeferredPR that is not planned to be merged until next major version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions