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

fix(sbb-datepicker): remove deprecated methods and properties #3247

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/elements/core/datetime/date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export const YEARS_PER_PAGE: number = 24;
export const FORMAT_DATE =
/(^0?[1-9]?|[12]?[0-9]?|3?[01]?)[.,\\/\-\s](0?[1-9]?|1?[0-2]?)?[.,\\/\-\s](\d{1,4}$)?/;

// TODO(breaking-change): Change undefined return types to null.

/**
* Abstract date functionality.
*
Expand Down Expand Up @@ -139,11 +137,12 @@ export abstract class DateAdapter<T = any> {
* @param value The date in the format DD.MM.YYYY.
* @param now The current date as Date.
*/
public abstract parse(value: string | null | undefined, now: T): T | undefined;
public abstract parse(value: string | null | undefined, now: T): T | null;

/**
* Format the given date as string.
* @param date The date to format.
* @param options options object with weekdayStyle as property
*/
public format(
date: T | null | undefined,
Expand Down
6 changes: 3 additions & 3 deletions src/elements/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ describe('NativeDateAdapter', () => {

it('parseDate should return the correct value', function () {
const now = new Date(2023, 8, 15, 0, 0, 0, 0);
expect(nativeDateAdapter.parse(null, now)).to.be.undefined;
expect(nativeDateAdapter.parse('Test', now)).to.be.undefined;
expect(nativeDateAdapter.parse('1.1', now)).to.be.undefined;
expect(nativeDateAdapter.parse(null, now)).to.be.null;
expect(nativeDateAdapter.parse('Test', now)).to.be.null;
expect(nativeDateAdapter.parse('1.1', now)).to.be.null;
let formattedDate = nativeDateAdapter.parse('1/1/2000', now)!;
expect(formattedDate.getFullYear()).to.be.equal(2000);
expect(formattedDate.getMonth()).to.be.equal(0);
Expand Down
6 changes: 3 additions & 3 deletions src/elements/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ export class NativeDateAdapter extends DateAdapter<Date> {
}

/** Returns the right format for the `valueAsDate` property. */
public parse(value: string | null | undefined, now: Date): Date | undefined {
public parse(value: string | null | undefined, now: Date): Date | null {
if (!value) {
return undefined;
return null;
}

const strippedValue = value.replace(/\D/g, ' ').trim();
Expand All @@ -188,7 +188,7 @@ export class NativeDateAdapter extends DateAdapter<Date> {
match.some((e) => e === undefined) ||
!this.isValid(this.createDate(+match[3], +match[2], +match[1]))
) {
return undefined;
return null;
}

let year = +match[3];
Expand Down
39 changes: 11 additions & 28 deletions src/elements/datepicker/common/datepicker-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
protected abstract iconName: string;
protected abstract i18nOffBoundaryDay: Record<string, string>;
protected abstract i18nSelectOffBoundaryDay: (_currentDate: string) => Record<string, string>;
protected abstract findAvailableDate: (
_date: T,
_dateFilter: ((date: T) => boolean) | null,
_dateAdapter: DateAdapter<T>,
_boundary: string | number | null,
) => T;
protected abstract onInputUpdated(event: CustomEvent<SbbInputUpdateEvent>): void;
protected abstract findAvailableDate(_date: T): T;

public override connectedCallback(): void {
super.connectedCallback();
Expand All @@ -71,21 +65,16 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
this._datePickerController?.abort();
}

protected setDisabledState(datepicker: SbbDatepickerElement<T> | null | undefined): void {
const pickerValueAsDate = datepicker?.valueAsDate;
private _setDisabledState(): void {
const pickerValueAsDate = this.datePickerElement?.valueAsDate;

if (!pickerValueAsDate) {
this._disabled = true;
this._setDisabledRenderAttributes(true);
return;
}

const availableDate: T = this.findAvailableDate(
pickerValueAsDate,
datepicker?.dateFilter || null,
this._dateAdapter,
this.boundary,
);
const availableDate: T = this.findAvailableDate(pickerValueAsDate);
this._disabled = this._dateAdapter.compareDate(availableDate, pickerValueAsDate) === 0;
this._setDisabledRenderAttributes();
}
Expand All @@ -95,12 +84,7 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
return;
}
const startingDate: T = this.datePickerElement.valueAsDate ?? this.datePickerElement.now;
const date: T = this.findAvailableDate(
startingDate,
this.datePickerElement.dateFilter,
this._dateAdapter,
this.boundary,
);
const date: T = this.findAvailableDate(startingDate);
if (this._dateAdapter.compareDate(date, startingDate) !== 0) {
this.datePickerElement.valueAsDate = date;
}
Expand All @@ -125,7 +109,7 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
this._datePickerController?.abort();
this._datePickerController = new AbortController();
this.datePickerElement = getDatePicker(this, picker);
this.setDisabledState(this.datePickerElement);
this._setDisabledState();
if (!this.datePickerElement) {
// If the component is attached to the DOM before the datepicker, it has to listen for the datepicker init,
// assuming that the two components share the same parent element.
Expand All @@ -140,16 +124,16 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb

this.datePickerElement.addEventListener(
'change',
(event: Event) => {
this.setDisabledState(event.target as SbbDatepickerElement<T>);
() => {
this._setDisabledState();
this._setAriaLabel();
},
{ signal: this._datePickerController.signal },
);
this.datePickerElement.addEventListener(
'datePickerUpdated',
(event: Event) => {
this.setDisabledState(event.target as SbbDatepickerElement<T>);
() => {
this._setDisabledState();
this._setAriaLabel();
},
{ signal: this._datePickerController.signal },
Expand All @@ -160,7 +144,7 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
this._inputDisabled = !!(event.detail.disabled || event.detail.readonly);
this._setDisabledRenderAttributes();
this._setAriaLabel();
this.onInputUpdated(event);
this._setDisabledState();
},
{ signal: this._datePickerController.signal },
);
Expand All @@ -176,7 +160,6 @@ export abstract class SbbDatepickerButton<T = Date> extends SbbNegativeMixin(Sbb
return;
}

// TODO: use toIsoString() instead of toDateString()
const currentDateString =
this.datePickerElement &&
this._dateAdapter.compareDate(this.datePickerElement.now, currentDate) === 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { customElement } from 'lit/decorators.js';
import { hostAttributes } from '../../core/decorators.js';
import { i18nNextDay, i18nSelectNextDay } from '../../core/i18n.js';
import { SbbDatepickerButton } from '../common.js';
import { findNextAvailableDate, type SbbInputUpdateEvent } from '../datepicker.js';

import '../../icon.js';
import style from './datepicker-next-day.scss?lit&inline';

/**
Expand All @@ -23,13 +21,10 @@ class SbbDatepickerNextDayElement<T = Date> extends SbbDatepickerButton<T> {
protected iconName: string = 'chevron-small-right-small';
protected i18nOffBoundaryDay: Record<string, string> = i18nNextDay;
protected i18nSelectOffBoundaryDay = i18nSelectNextDay;
protected findAvailableDate = findNextAvailableDate;

protected onInputUpdated(event: CustomEvent<SbbInputUpdateEvent>): void {
if (this.boundary !== event.detail.max) {
this.boundary = event.detail.max!;
this.setDisabledState(this.datePickerElement!);
}
protected findAvailableDate(date: T): T {
// When calling findAvailableDate, datepickerElement is always defined.
return this.datePickerElement!.findNextAvailableDate(date);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { customElement } from 'lit/decorators.js';
import { hostAttributes } from '../../core/decorators.js';
import { i18nPreviousDay, i18nSelectPreviousDay } from '../../core/i18n.js';
import { SbbDatepickerButton } from '../common.js';
import { findPreviousAvailableDate, type SbbInputUpdateEvent } from '../datepicker.js';
import '../../icon.js';

import style from './datepicker-previous-day.scss?lit&inline';

Expand All @@ -23,13 +21,10 @@ class SbbDatepickerPreviousDayElement<T = Date> extends SbbDatepickerButton<T> {
protected iconName: string = 'chevron-small-left-small';
protected i18nOffBoundaryDay: Record<string, string> = i18nPreviousDay;
protected i18nSelectOffBoundaryDay = i18nSelectPreviousDay;
protected findAvailableDate = findPreviousAvailableDate;

protected onInputUpdated(event: CustomEvent<SbbInputUpdateEvent>): void {
if (this.boundary !== event.detail.min) {
this.boundary = event.detail.min!;
this.setDisabledState(this.datePickerElement!);
}
protected findAvailableDate(date: T): T {
// When calling findAvailableDate, datepickerElement is always defined.
return this.datePickerElement!.findPreviousAvailableDate(date);
}
}

Expand Down
Loading