Skip to content

Commit

Permalink
feat: first impl
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideMininni-Fincons committed Nov 27, 2023
1 parent 35928e3 commit dbb06d1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export class SbbDatepickerNextDay extends LitElement {
if (!this._datePickerElement || isValidAttribute(this, 'data-disabled')) {
return;
}
const startingDate: Date = this._datePickerElement.getValueAsDate() ?? this._now();
const startingDate: Date = this._datePickerElement.valueAsDate ?? this._now();
const date: Date = findNextAvailableDate(
startingDate,
this._datePickerElement.dateFilter,
this._dateAdapter,
this._max,
);
if (this._dateAdapter.compareDate(date, startingDate) !== 0) {
this._datePickerElement.setValueAsDate(date);
this._datePickerElement.valueAsDate = date;
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ export class SbbDatepickerNextDay extends LitElement {
}

private _setDisabledState(datepicker: SbbDatepicker): void {
const pickerValueAsDate: Date = datepicker?.getValueAsDate();
const pickerValueAsDate: Date = datepicker?.valueAsDate;

if (!pickerValueAsDate) {
this._disabled = true;
Expand Down Expand Up @@ -207,7 +207,7 @@ export class SbbDatepickerNextDay extends LitElement {
}

private _setAriaLabel(): void {
const currentDate = this._datePickerElement.getValueAsDate();
const currentDate = this._datePickerElement.valueAsDate;

if (!currentDate || !this._dateAdapter.isValid(currentDate)) {
this.setAttribute('aria-label', i18nNextDay[this._currentLanguage]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export class SbbDatepickerPreviousDay extends LitElement {
if (!this._datePickerElement || isValidAttribute(this, 'data-disabled')) {
return;
}
const startingDate: Date = this._datePickerElement.getValueAsDate() ?? this._now();
const startingDate: Date = this._datePickerElement.valueAsDate ?? this._now();
const date: Date = findPreviousAvailableDate(
startingDate,
this._datePickerElement.dateFilter,
this._dateAdapter,
this._min,
);
if (this._dateAdapter.compareDate(date, startingDate) !== 0) {
this._datePickerElement.setValueAsDate(date);
this._datePickerElement.valueAsDate = date;
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ export class SbbDatepickerPreviousDay extends LitElement {
}

private _setDisabledState(datepicker: SbbDatepicker): void {
const pickerValueAsDate: Date = datepicker?.getValueAsDate();
const pickerValueAsDate: Date = datepicker?.valueAsDate;

if (!pickerValueAsDate) {
this._disabled = true;
Expand Down Expand Up @@ -207,7 +207,7 @@ export class SbbDatepickerPreviousDay extends LitElement {
}

private _setAriaLabel(): void {
const currentDate = this._datePickerElement.getValueAsDate();
const currentDate = this._datePickerElement.valueAsDate;

if (!currentDate || !this._dateAdapter.isValid(currentDate)) {
this.setAttribute('aria-label', i18nPreviousDay[this._currentLanguage]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class SbbDatepickerToggle extends LitElement {

private _datePickerChanged(event: Event): void {
this._datePickerElement = event.target as SbbDatepicker;
this._calendarElement.selectedDate = this._datePickerElement.getValueAsDate();
this._calendarElement.selectedDate = this._datePickerElement.valueAsDate;
}

private _assignCalendar(calendar: SbbCalendar): void {
Expand All @@ -158,12 +158,12 @@ export class SbbDatepickerToggle extends LitElement {
this._calendarElement = calendar;
if (
!this._datePickerElement ||
!this._datePickerElement.getValueAsDate ||
!this._datePickerElement.valueAsDate ||
!this._calendarElement?.resetPosition
) {
return;
}
this._calendarElement.selectedDate = this._datePickerElement.getValueAsDate();
this._calendarElement.selectedDate = this._datePickerElement.valueAsDate;
this._configureCalendar(this._calendarElement, this._datePickerElement);
this._calendarElement.resetPosition();
}
Expand Down Expand Up @@ -221,7 +221,7 @@ export class SbbDatepickerToggle extends LitElement {
@dateSelected=${(d: CustomEvent<Date>) => {
const newDate = new Date(d.detail);
this._calendarElement.selectedDate = newDate;
this._datePickerElement.setValueAsDate(newDate);
this._datePickerElement.valueAsDate = newDate;
}}
></sbb-calendar>
</sbb-tooltip>
Expand Down
9 changes: 4 additions & 5 deletions src/components/datepicker/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class SbbDatepicker extends LitElement {
return this._inputElementState;
}

private set _inputElement(value) {
private set _inputElement(value: HTMLInputElement) {
const oldValue = this._inputElementState;
this._inputElementState = value;
this._registerInputElement(this._inputElementState, oldValue);
Expand Down Expand Up @@ -298,12 +298,12 @@ export class SbbDatepicker extends LitElement {
}

/** Gets the input value with the correct date format. */
public getValueAsDate(): Date {
public get valueAsDate(): Date | undefined {
return this._parse(this._inputElement?.value);
}

/** Set the input value to the correctly formatted value. */
public setValueAsDate(date: SbbDateLike): void {
public set valueAsDate(date: SbbDateLike) {
const parsedDate = date instanceof Date ? date : new Date(date);
this._formatAndUpdateValue(this._inputElement.value, parsedDate);
/* Emit blur event when value is changed programmatically to notify
Expand Down Expand Up @@ -339,8 +339,7 @@ export class SbbDatepicker extends LitElement {
this._currentLanguage = l;
if (this._inputElement) {
this._inputElement.placeholder = i18nDatePickerPlaceholder[this._currentLanguage];
const valueAsDate = this.getValueAsDate();
this._inputElement.value = this._format(valueAsDate);
this._inputElement.value = this._format(this.valueAsDate);
}
}),
);
Expand Down
22 changes: 8 additions & 14 deletions src/components/datepicker/datepicker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,14 @@ This is helpful if you need a specific state of the component.

## Properties

| Name | Attribute | Privacy | Type | Default | Description |
| ------------ | ------------- | ------- | --------------------------------------------------- | ------- | ----------------------------------------------------------------- |
| `wide` | `wide` | public | `boolean` | `false` | If set to true, two months are displayed. |
| `dateFilter` | `date-filter` | public | `(date: Date \| null) => boolean` | | A function used to filter out dates. |
| `dateParser` | `date-parser` | public | `(value: string) => Date \| undefined \| undefined` | | A function used to parse string value into dates. |
| `format` | `format` | public | `(date: Date) => string \| undefined` | | A function used to format dates into the preferred string format. |
| `input` | `input` | public | `string \| HTMLElement \| undefined` | | Reference of the native input connected to the datepicker. |

## Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ---------------- | ------- | ----------------------------------------------------- | ------------------- | ------ | -------------- |
| `getValueAsDate` | public | Gets the input value with the correct date format. | | `Date` | |
| `setValueAsDate` | public | Set the input value to the correctly formatted value. | `date: SbbDateLike` | `void` | |
| Name | Attribute | Privacy | Type | Default | Description |
| ------------- | --------------- | ------- | --------------------------------------------------- | ------- | ----------------------------------------------------------------- |
| `wide` | `wide` | public | `boolean` | `false` | If set to true, two months are displayed. |
| `dateFilter` | `date-filter` | public | `(date: Date \| null) => boolean` | | A function used to filter out dates. |
| `dateParser` | `date-parser` | public | `(value: string) => Date \| undefined \| undefined` | | A function used to parse string value into dates. |
| `format` | `format` | public | `(date: Date) => string \| undefined` | | A function used to format dates into the preferred string format. |
| `input` | `input` | public | `string \| HTMLElement \| undefined` | | Reference of the native input connected to the datepicker. |
| `valueAsDate` | `value-as-date` | public | `Date \| undefined` | | Set the input value to the correctly formatted value. |

## Events

Expand Down
14 changes: 4 additions & 10 deletions src/components/time-input/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ Whenever the validation state changes (e.g., a valid value becomes invalid or vi

## Properties

| Name | Attribute | Privacy | Type | Default | Description |
| ------- | --------- | ------- | ----------------------- | ------- | ---------------------------------------------------------- |
| `input` | `input` | public | `string \| HTMLElement` | | Reference of the native input connected to the datepicker. |

## Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ---------------- | ------- | ----------------------------------------------------- | ------------------- | -------------- | -------------- |
| `getValueAsDate` | public | Gets the input value with the correct date format. | | `Date \| null` | |
| `setValueAsDate` | public | Set the input value to the correctly formatted value. | `date: SbbDateLike` | `void` | |
| Name | Attribute | Privacy | Type | Default | Description |
| ------------- | --------------- | ------- | ----------------------- | ------- | ---------------------------------------------------------- |
| `input` | `input` | public | `string \| HTMLElement` | | Reference of the native input connected to the datepicker. |
| `valueAsDate` | `value-as-date` | public | `Date \| null` | | |

## Events

Expand Down
14 changes: 7 additions & 7 deletions src/components/time-input/time-input.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,25 @@ describe('sbb-time-input', () => {
const blurSpy = new EventSpy('blur', input);
const date = new Date('2023-01-01T15:00:00');

element.setValueAsDate(date);
element.valueAsDate = date;
await waitForLitRender(element);

expect(input.value).to.be.equal('15:00');
expect(blurSpy.count).to.be.equal(1);

const dateCalculated = element.getValueAsDate().getTime();
const dateCalculated = element.valueAsDate.getTime();
expect(new Date(dateCalculated).getHours()).to.be.equal(date.getHours());
expect(new Date(dateCalculated).getMinutes()).to.be.equal(date.getMinutes());
});

it('should set and get value as a date (string)', async () => {
const date = new Date('2023-01-01T15:00:00');

element.setValueAsDate(date.toISOString());
element.valueAsDate = date.toISOString();
await waitForLitRender(element);
expect(input.value).to.be.equal('15:00');

const dateCalculated = element.getValueAsDate().getTime();
const dateCalculated = element.valueAsDate.getTime();
expect(new Date(dateCalculated).getHours()).to.be.equal(date.getHours());
expect(new Date(dateCalculated).getMinutes()).to.be.equal(date.getMinutes());
});
Expand All @@ -234,7 +234,7 @@ describe('sbb-time-input', () => {
element = root.querySelector('sbb-time-input');
input = root.querySelector('input');

element.setValueAsDate('2023-01-01T15:00:00');
element.valueAsDate = '2023-01-01T15:00:00';
await waitForLitRender(element);
expect(input.value).to.be.equal('15:00');
});
Expand All @@ -249,7 +249,7 @@ describe('sbb-time-input', () => {
element = root.querySelector('sbb-time-input');
input = root.querySelector('input');
element.input = input;
element.setValueAsDate('2023-01-01T15:00:00');
element.valueAsDate = '2023-01-01T15:00:00';
await waitForLitRender(element);

expect(input.value).to.be.equal('15:00');
Expand All @@ -266,7 +266,7 @@ describe('sbb-time-input', () => {
input = root.querySelector('input');

element.input = 'input-2';
element.setValueAsDate('2023-01-01T15:00:00');
element.valueAsDate = '2023-01-01T15:00:00';
await waitForLitRender(element);

expect(input.value).to.be.equal('15:00');
Expand Down
8 changes: 2 additions & 6 deletions src/components/time-input/time-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ export class SbbTimeInput extends LitElement {
this._abortController?.abort();
this._handlerRepository.disconnect();
}

/** Gets the input value with the correct date format. */
// TODO: refactor this to be a get/set
public getValueAsDate(): Date | null {
public get valueAsDate(): Date | null {
return this._formatValueAsDate(this._parseInput(this._inputElement?.value));
}

/** Set the input value to the correctly formatted value. */
public setValueAsDate(date: SbbDateLike): void {
public set valueAsDate(date: SbbDateLike) {
if (!date || !this._inputElement) {
return;
}
Expand Down

0 comments on commit dbb06d1

Please sign in to comment.