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

defaultDate prop in MonthPickerInput and YearPickerInput is ignored (tested fix enclosed) #7108

Closed
1 of 2 tasks
mkf62 opened this issue Nov 10, 2024 · 1 comment
Closed
1 of 2 tasks
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@mkf62
Copy link

mkf62 commented Nov 10, 2024

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?

7.13.2

What package has an issue?

@mantine/dates

What framework do you use?

Next.js

In which browsers you can reproduce the issue?

None

Describe the bug

Both MonthPickerInput and YearPickerInput have the getDefaultClampedDate function called when setting the defaultDate if _value or _value[0] is null. When using these pickers in forms, where Mantine docs suggest using uncontrolled components for best performance of forms, the defaultDate prop becomes important. The getDefaultClampedDate function doesn't resolve how I would expect it to though because it derives the defaultDate from the minDate and maxDate props instead of using the developer-supplied defaultDate:

defaultDate={
Array.isArray(_value)
? _value[0] || getDefaultClampedDate({ maxDate, minDate })
: _value || getDefaultClampedDate({ maxDate, minDate })
}

defaultDate={
Array.isArray(_value)
? _value[0] ||
getDefaultClampedDate({ maxDate, minDate, timezone: ctx.getTimezone() })
: _value || getDefaultClampedDate({ maxDate, minDate, timezone: ctx.getTimezone() })
}

export function getDefaultClampedDate({ minDate, maxDate, timezone }: GetDefaultClampedDate) {
const today = shiftTimezone('add', new Date(), timezone);
if (!minDate && !maxDate) {
return today;
}
if (minDate && dayjs(today).isBefore(minDate)) {
return minDate;
}
if (maxDate && dayjs(today).isAfter(maxDate)) {
return maxDate;
}
return today;
}

I think the MonthPickerInput and the YearPickerInput should operate the same way as the DatePickerInput when setting the defaultDate, otherwise the defaultDate prop on Month/Year pickers serves no purpose since it will always be derived from the minDate or maxDate props.

const _defaultDate = Array.isArray(_value) ? _value[0] || defaultDate : _value || defaultDate;

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

https://codesandbox.io/p/sandbox/mantine-react-template-forked-zxhyqr

Possible fix

I tested changing the code in the MonthPickerInput to this and it performed as I expected it to:

 defaultDate={ 
   Array.isArray(_value) 
     ? _value[0] || calendarProps.defaultDate || getDefaultDate({minDate, maxDate, timezone: ctx.getTimezone() })
     : _value || calendarProps.defaultDate || getDefaultDate({minDate, maxDate, timezone: ctx.getTimezone() })
 } 

I tested changing the code in the YearPickerInput to the same code above and it performed as I expected it to also.

Self-service

  • I would be willing to implement a fix for this issue
rtivital added a commit that referenced this issue Nov 19, 2024
@rtivital rtivital added the Fixed patch Completed issues that will be published with next patch (1.0.X) label Nov 19, 2024
@rtivital
Copy link
Member

Fixed in 7.14.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

2 participants