Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Oct 5, 2023

Description

This change updates the vaadin-date-picker to allow disabling arbitrary dates via a new isDateDisabled property. This property is a function

Fixes #1820 but see this platform issue for specific discussion: vaadin/platform#2867

This PR only addresses the UX-frontend portion. This PR does not attempt to address the Java API side.

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/contributing/overview
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.
  • I have not completed some of the steps above and my pull request can be closed immediately.

Additional for Feature type of change

  • Enhancement / new feature was discussed in a corresponding GitHub issue and Acceptance Criteria were created.

@ghost
Copy link
Author

ghost commented Oct 5, 2023

@sissbruecker I have recreated this PR per the comments you made on the closed one. The only deviation from your suggestion is noted in the discussion in the closed PR about passing a DatePickerDate instead of a javascript Date. I opted to stick with a Date object because the primary dateAllowed function in vaadin-date-picker-helper.js receives a date object, not a DatePickerDate object.

@ghost
Copy link
Author

ghost commented Oct 5, 2023

@sissbruecker I have recreated this PR per the comments you made on the closed one. The only deviation from your suggestion is noted in the discussion in the closed PR about passing a DatePickerDate instead of a javascript Date. I opted to stick with a Date object because the primary dateAllowed function in vaadin-date-picker-helper.js receives a date object, not a DatePickerDate object.

The closed PR is: #5252

@ghost ghost changed the title Date Picker isDateDisabled functionality feat(date-picker): Add isDateDisabled functionality Oct 6, 2023
<script type="module">
import '@vaadin/date-picker';
import '@vaadin/tooltip';
const isDateDisabled = (date) => !!(date?.getDay() === 0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a sample implementation in the demo app. We can drop this or add some docs as needed to describe what's going on.

*/
export function dateAllowed(date, min, max) {
return (!min || date >= min) && (!max || date <= max);
export function dateAllowed(date, min, max, isDateDisabled) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the heavy lifting function that does all the work of deciding a date needs to be disabled. So all the components that need to disable a date end up feeding here.

* @protected
*/
_selectDate(dateToSelect) {
if (!this._dateAllowed(dateToSelect)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need to discuss this change. I updated this to be a boolean function so that the keyboard Enter behavior could be prevented for disabled dates. That allows us to hover on disabled dates without allowing selection of those dates.

_focusAllowedDate(dateToFocus, diff, keepMonth) {
if (this._dateAllowed(dateToFocus)) {
// For this check we do consider the isDateDisabled function because disabled dates are allowed to be focused, just not outside min/max
if (this._dateAllowed(dateToFocus, undefined, undefined, () => false)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this one place we want to bypass any custom isDateDisabled function since we are allowed to focus the date, just not select it.

});

expect(invalidChangedSpy.calledOnce).to.be.true;
const selectResult = datePicker._overlayContent._selectDate(new Date('2017-01-01')); // Invalid
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the check for the invalid change since the new logic will prevent actually selecting an invalid date. Checking the boolean result of _selectDate should be sufficient. Same for below tests.

Comment on lines +12 to +19
const isDateDisabled = (date) => {
// Exclude weekends and the 16th day of each month:
const checkDate = new Date(0, 0);
checkDate.setFullYear(date.year);
checkDate.setMonth(date.month);
checkDate.setDate(date.day);
return checkDate.getDay() === 0 || checkDate.getDay() === 6 || checkDate.getDate() === 16;
}
Copy link
Member

@tomivirkki tomivirkki Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently had a brief internal discussion regarding the parameter format for the
isDateDisabled function, considering the diverse range of use cases. Two potential options:

Option 1: DatePickerDate ({year: 2023, month: 10, day: 10}):

  • More structured and explicit, allowing users to access individual components of the date easily.
  • Might be easier for disabling specific weekdays or days of months

Option 2: ISO Formatted Date String ("2023-11-10"):

  • Familiar format, commonly used in various contexts.
  • Same format as the component's value
  • Might be easier for disabling specific dates (stored in DB as local date for example)

If a decision between these two options proves challenging, we could consider offering flexibility by providing both ISO formatted date strings and date objects with individual components. For example, an extended DatePickerDate could include both formats as follows:

{
  year: 2023,
  month: 10,
  day: 10,
  date: "2023-11-10"
}

@rolfsmeds @yuriy-fix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the ISO date be optional or should we expect that the DatePickerDate structure always contains all four fields?

Copy link
Member

@tomivirkki tomivirkki Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying DatePickerDate might be breaking, so better introduce a new type or extend it (if this is the option we want to go with).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now it seems to be working well enough with DatePickerDate. If a developer needs more detailed logic it's easy enough to convert it to a Javascript Date object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rolfsmeds and @yuriy-fix , can you weigh in on this discussion and see if we should alter the DatePickerDate shape per @tomivirkki 's comments or can we proceed with merging this PR as it stands? All other concerns have been addressed save this one.

@ghost ghost requested a review from tomivirkki November 16, 2023 14:28
@ghost ghost requested a review from tomivirkki November 17, 2023 15:02
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@ghost
Copy link
Author

ghost commented Nov 22, 2023

@tomivirkki Is there anything that I need to do regarding the Visual tests failing to run due to Saucelabs proxy connect issues? I didn't see a way to retry those tests.

@tomivirkki
Copy link
Member

Is there anything that I need to do regarding the Visual tests failing to run due to Saucelabs proxy connect issues?

Nope, we'll take care of this before merging

@yuriy-fix
Copy link
Contributor

We will proceed with the PR after branching out for new minor. (~ couple of weeks)

@ghost
Copy link
Author

ghost commented Jan 2, 2024

We will proceed with the PR after branching out for new minor. (~ couple of weeks)

Any word on this? Wondering if this will be in 24.4?

@ghost
Copy link
Author

ghost commented Jan 15, 2024

Closed. Superseded by #7075

@ghost ghost closed this Jan 15, 2024
@dethell
Copy link
Contributor

dethell commented Jan 18, 2024

Sorry for all the "ghost" comments. I merged my GitHub accounts. All the commits moved, but GitHub doesn't re-attach discussions/comments/etc.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[date-picker] Add support for disabled dates

3 participants