From 6f9b5c9f1945eb0f3d1f43d8f9961bc206e078aa Mon Sep 17 00:00:00 2001 From: Davide Mininni Date: Fri, 17 May 2024 08:28:29 +0200 Subject: [PATCH 01/18] feat: add now property instead of data-now --- src/components/calendar/calendar.e2e.ts | 4 ++-- src/components/calendar/calendar.spec.ts | 6 ++--- src/components/calendar/calendar.stories.ts | 4 ++-- src/components/calendar/calendar.ts | 9 ++++--- src/components/calendar/readme.md | 9 ++++--- src/components/clock/clock.spec.ts | 2 +- src/components/clock/clock.stories.ts | 10 ++++---- src/components/clock/clock.ts | 21 +++++++--------- src/components/clock/readme.md | 14 ++++++++--- src/components/core/datetime.ts | 1 - src/components/core/datetime/data-now.ts | 8 ------- .../datepicker-next-day.stories.ts | 2 +- .../datepicker-previous-day.stories.ts | 2 +- .../datepicker-toggle.stories.ts | 4 ++-- .../datepicker-toggle/datepicker-toggle.ts | 7 +++--- .../datepicker/datepicker.stories.ts | 21 ++++++---------- .../datepicker/datepicker/datepicker.ts | 13 +++++----- .../datepicker/datepicker/readme.md | 23 +++++++++--------- src/components/footer/footer.stories.ts | 2 +- .../journey-summary.spec.snap.js | 8 +++---- .../journey-summary/journey-summary.spec.ts | 12 +++++----- .../journey-summary.stories.ts | 4 ++-- .../journey-summary/journey-summary.ts | 9 +++---- src/components/journey-summary/readme.md | 4 ++++ .../pearl-chain-time/pearl-chain-time.spec.ts | 24 +++++++++---------- .../pearl-chain-time.stories.ts | 8 +++---- .../pearl-chain-time/pearl-chain-time.ts | 10 ++++---- src/components/pearl-chain-time/readme.md | 5 ++-- .../pearl-chain/pearl-chain.stories.ts | 18 +++++++------- src/components/pearl-chain/pearl-chain.ts | 8 ++++--- src/components/pearl-chain/readme.md | 5 ++-- src/components/timetable-row/readme.md | 5 ++-- .../timetable-row/timetable-row.spec.ts | 16 ++++++------- .../timetable-row/timetable-row.stories.ts | 22 ++++++++--------- src/components/timetable-row/timetable-row.ts | 10 ++++---- .../pages/home/home--logged-in.stories.ts | 4 ++-- src/storybook/pages/home/home.common.ts | 4 +--- 37 files changed, 166 insertions(+), 172 deletions(-) delete mode 100644 src/components/core/datetime/data-now.ts diff --git a/src/components/calendar/calendar.e2e.ts b/src/components/calendar/calendar.e2e.ts index cacfb4a273..ab6b1cd9ce 100644 --- a/src/components/calendar/calendar.e2e.ts +++ b/src/components/calendar/calendar.e2e.ts @@ -18,7 +18,7 @@ describe(`sbb-calendar with ${fixture.name}`, () => { beforeEach(async () => { element = await fixture( - html``, + html``, { modules: ['./calendar.ts'] }, ); }); @@ -326,7 +326,7 @@ describe(`sbb-calendar with ${fixture.name}`, () => { describe('navigation for year view', () => { beforeEach(async () => { element = await fixture( - html``, + html``, { modules: ['./calendar.ts'] }, ); diff --git a/src/components/calendar/calendar.spec.ts b/src/components/calendar/calendar.spec.ts index 845c48e729..da736dc4d7 100644 --- a/src/components/calendar/calendar.spec.ts +++ b/src/components/calendar/calendar.spec.ts @@ -8,11 +8,11 @@ import './calendar.js'; describe(`sbb-calendar`, () => { it('renders', async () => { const root = await fixture( - html``, + html``, ); expect(root).dom.to.be.equal( - ``, + ``, ); await expect(root).shadowDom.to.be.equalSnapshot(); }); @@ -54,7 +54,7 @@ describe(`sbb-calendar`, () => { }); testA11yTreeSnapshot( - html``, + html``, undefined, { safari: true }, // We skip safari because it has an inconsistent behavior on ci environment ); diff --git a/src/components/calendar/calendar.stories.ts b/src/components/calendar/calendar.stories.ts index 611d4992c6..dd5ec8f1f4 100644 --- a/src/components/calendar/calendar.stories.ts +++ b/src/components/calendar/calendar.stories.ts @@ -84,7 +84,7 @@ const max: InputType = { }, }; -const dataNow: InputType = { +const now: InputType = { control: { type: 'date', }, @@ -124,7 +124,7 @@ const defaultArgTypes: ArgTypes = { min, max, dateFilter, - 'data-now': dataNow, + now, }; const today = new Date(); diff --git a/src/components/calendar/calendar.ts b/src/components/calendar/calendar.ts index 1195bbfe88..3028e8c2c7 100644 --- a/src/components/calendar/calendar.ts +++ b/src/components/calendar/calendar.ts @@ -12,7 +12,7 @@ import { classMap } from 'lit/directives/class-map.js'; import { isArrowKeyOrPageKeysPressed, sbbInputModalityDetector } from '../core/a11y.js'; import { SbbConnectedAbortController, SbbLanguageController } from '../core/controllers.js'; -import { type DateAdapter, readDataNow } from '../core/datetime.js'; +import { type DateAdapter } from '../core/datetime.js'; import { DAYS_PER_ROW, defaultDateAdapter, @@ -141,6 +141,9 @@ export class SbbCalendarElement extends LitElement { /** A function used to filter out dates. */ @property({ attribute: 'date-filter' }) public dateFilter?: (date: T | null) => boolean; + /** A specific date for the current datetime (timestamp in milliseconds). */ + @property({ attribute: 'now' }) public dataNow?: number; + private _dateAdapter: DateAdapter = defaultDateAdapter as unknown as DateAdapter; /** Event emitted on date selection. */ @@ -796,8 +799,8 @@ export class SbbCalendarElement extends LitElement { } private _now(): T { - if (this.hasAttribute('data-now')) { - const today = new Date(readDataNow(this)); + if (this.dataNow) { + const today = new Date(+this.dataNow); if (defaultDateAdapter.isValid(today)) { return this._dateAdapter.createDate( today.getFullYear(), diff --git a/src/components/calendar/readme.md b/src/components/calendar/readme.md index cb5b981359..a4aa8e3b7e 100644 --- a/src/components/calendar/readme.md +++ b/src/components/calendar/readme.md @@ -20,6 +20,9 @@ It's recommended to set the time to 00:00:00. ``` +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). +This is helpful if you need a specific state of the component. + ## Style The component displays one month by default; two months can be displayed setting the `wide` property to `true`. @@ -55,17 +58,13 @@ It's possible to move within the component using the keyboard. For accessibility purposes, the component is rendered as a native table element and each day is a button. -## Testing - -To specify a specific date for the current datetime, you can use the `data-now` attribute (timestamp in milliseconds). -This is helpful if you need a specific state of the component. - ## Properties | Name | Attribute | Privacy | Type | Default | Description | | ------------ | ------------- | ------- | ------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------ | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | | `dateFilter` | `date-filter` | public | `(date: T \| null) => boolean \| undefined` | | A function used to filter out dates. | | `max` | `max` | public | `T \| null` | | The maximum valid date. Takes T Object, ISOString, and Unix Timestamp (number of seconds since Jan 1, 1970). | | `min` | `min` | public | `T \| null` | | The minimum valid date. Takes T Object, ISOString, and Unix Timestamp (number of seconds since Jan 1, 1970). | diff --git a/src/components/clock/clock.spec.ts b/src/components/clock/clock.spec.ts index efe7cf63bc..9263610b04 100644 --- a/src/components/clock/clock.spec.ts +++ b/src/components/clock/clock.spec.ts @@ -18,7 +18,7 @@ describe(`sbb-clock`, () => { }); it('renders with a fixed time', async () => { - element = await fixture(html``); + element = await fixture(html``); assert.instanceOf(element, SbbClockElement); expect(element).to.have.attribute('data-initialized'); diff --git a/src/components/clock/clock.stories.ts b/src/components/clock/clock.stories.ts index 70df338b8b..54fc460d59 100644 --- a/src/components/clock/clock.stories.ts +++ b/src/components/clock/clock.stories.ts @@ -21,14 +21,14 @@ const Template = (args: Args): TemplateResult => html` { if (document.visibilityState === 'hidden') { this._stopClock(); - } else if (!this._hasDataNow()) { + } else if (!this.dataNow) { await this._startClock(); } } @@ -218,7 +219,7 @@ export class SbbClockElement extends LitElement { private _stopClock(): void { clearInterval(this._handMovement); - if (this._hasDataNow()) { + if (this.dataNow) { this._setHandsStartingPosition(); this._clockHandSeconds?.classList.add('sbb-clock__hand-seconds--initial-minute'); this._clockHandHours?.classList.add('sbb-clock__hand-hours--initial-hour'); @@ -253,13 +254,9 @@ export class SbbClockElement extends LitElement { ); } - private _hasDataNow(): boolean { - return this.hasAttribute('data-now'); - } - private _now(): Date { - if (this._hasDataNow()) { - return new Date(readDataNow(this)); + if (this.dataNow) { + return new Date(+this.dataNow); } return new Date(); } @@ -269,7 +266,7 @@ export class SbbClockElement extends LitElement { this._addEventListeners(); - if (this._hasDataNow()) { + if (this.dataNow) { this._stopClock(); } else { await this._startClock(); diff --git a/src/components/clock/readme.md b/src/components/clock/readme.md index 4c5ce98433..bb2d33b9c1 100644 --- a/src/components/clock/readme.md +++ b/src/components/clock/readme.md @@ -7,9 +7,17 @@ then it briefly pauses at the clock top before starting a new rotation. ``` -## Testing - -To specify a specific date for the current datetime, you can use the `data-now` attribute (timestamp in milliseconds). +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). This is helpful if you need a specific state of the component. +```html + +``` + + +## Properties + +| Name | Attribute | Privacy | Type | Default | Description | +| --------- | --------- | ------- | --------------------- | ------- | --------------------------------------------------------------------- | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | diff --git a/src/components/core/datetime.ts b/src/components/core/datetime.ts index 37e061731b..e3c9b8f40a 100644 --- a/src/components/core/datetime.ts +++ b/src/components/core/datetime.ts @@ -1,4 +1,3 @@ -export * from './datetime/data-now.js'; export * from './datetime/date-adapter.js'; export * from './datetime/date-helper.js'; export * from './datetime/native-date-adapter.js'; diff --git a/src/components/core/datetime/data-now.ts b/src/components/core/datetime/data-now.ts deleted file mode 100644 index d5e8acac16..0000000000 --- a/src/components/core/datetime/data-now.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Reads the value of the data-now attribute and converts it to a number. - * `undefined` results in NaN. - * @param element - */ -export const readDataNow = (element: HTMLElement): number => { - return +(element.getAttribute('data-now') ?? undefined)!; -}; diff --git a/src/components/datepicker/datepicker-next-day/datepicker-next-day.stories.ts b/src/components/datepicker/datepicker-next-day/datepicker-next-day.stories.ts index 905057091e..bbc454ae8b 100644 --- a/src/components/datepicker/datepicker-next-day/datepicker-next-day.stories.ts +++ b/src/components/datepicker/datepicker-next-day/datepicker-next-day.stories.ts @@ -44,7 +44,7 @@ const PickerAndButtonTemplate = (args: Args): TemplateResult => html` ${BaseTemplate(args, 'datepicker')} diff --git a/src/components/datepicker/datepicker-previous-day/datepicker-previous-day.stories.ts b/src/components/datepicker/datepicker-previous-day/datepicker-previous-day.stories.ts index 6fcbc06c1a..dceb325845 100644 --- a/src/components/datepicker/datepicker-previous-day/datepicker-previous-day.stories.ts +++ b/src/components/datepicker/datepicker-previous-day/datepicker-previous-day.stories.ts @@ -49,7 +49,7 @@ const PickerAndButtonTemplate = (args: Args): TemplateResult => html` `; diff --git a/src/components/datepicker/datepicker-toggle/datepicker-toggle.stories.ts b/src/components/datepicker/datepicker-toggle/datepicker-toggle.stories.ts index f70cb24db4..5b0a6d5e73 100644 --- a/src/components/datepicker/datepicker-toggle/datepicker-toggle.stories.ts +++ b/src/components/datepicker/datepicker-toggle/datepicker-toggle.stories.ts @@ -69,7 +69,7 @@ const PickerAndButtonTemplate = (args: Args): TemplateResult => html` @@ -79,7 +79,7 @@ const FormFieldTemplate = ({ negative, ...args }: Args): TemplateResult => html` ${StandaloneTemplate(args)} diff --git a/src/components/datepicker/datepicker-toggle/datepicker-toggle.ts b/src/components/datepicker/datepicker-toggle/datepicker-toggle.ts index 8d6c6e7309..fd4f5e530c 100644 --- a/src/components/datepicker/datepicker-toggle/datepicker-toggle.ts +++ b/src/components/datepicker/datepicker-toggle/datepicker-toggle.ts @@ -6,7 +6,6 @@ import { ref } from 'lit/directives/ref.js'; import type { SbbCalendarElement } from '../../calendar.js'; import { sbbInputModalityDetector } from '../../core/a11y.js'; import { SbbLanguageController } from '../../core/controllers.js'; -import { readDataNow } from '../../core/datetime.js'; import { hostAttributes } from '../../core/decorators.js'; import { i18nShowCalendar } from '../../core/i18n.js'; import { SbbNegativeMixin } from '../../core/mixins.js'; @@ -157,8 +156,8 @@ export class SbbDatepickerToggleElement extends SbbNegativeMixin(LitElement) { } private _now(): Date | undefined { - if (this._datePickerElement?.hasAttribute('data-now')) { - const today = new Date(readDataNow(this._datePickerElement)); + if (this._datePickerElement?.dataNow) { + const today = new Date(+this._datePickerElement?.dataNow); today.setHours(0, 0, 0, 0); return today; } @@ -192,7 +191,7 @@ export class SbbDatepickerToggleElement extends SbbNegativeMixin(LitElement) { ${ref((el?: Element) => (this._popoverElement = el as SbbPopoverElement))} > => { document.getElementById('container-value')?.append(div); }; -const Template = ({ - min, - max, - wide, - dateFilter, - 'data-now': dataNow, - ...args -}: Args): TemplateResult => { +const Template = ({ min, max, wide, dateFilter, now, ...args }: Args): TemplateResult => { return html`
@@ -330,7 +323,7 @@ const Template = ({ .dateFilter=${dateFilter} ?wide=${wide} @change=${(event: Event) => changeEventHandler(event)} - data-now=${dataNow} + now=${now} >
@@ -354,7 +347,7 @@ const TemplateFormField = ({ wide, dateFilter, dateHandling, - 'data-now': dataNow, + now, ...args }: Args): TemplateResult => { return html` @@ -376,7 +369,7 @@ const TemplateFormField = ({ .format=${dateHandling.format} ?wide=${wide} @change=${(event: Event) => changeEventHandler(event)} - data-now=${dataNow} + now=${now} >
## Properties -| Name | Attribute | Privacy | Type | Default | Description | -| ------------ | ------------- | ------- | --------------------------------------------------- | ------- | ----------------------------------------------------------------- | -| `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. | -| `wide` | `wide` | public | `boolean` | `false` | If set to true, two months are displayed. | +| Name | Attribute | Privacy | Type | Default | Description | +| ------------ | ------------- | ------- | --------------------------------------------------- | ------- | --------------------------------------------------------------------- | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | +| `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. | +| `wide` | `wide` | public | `boolean` | `false` | If set to true, two months are displayed. | ## Methods diff --git a/src/components/footer/footer.stories.ts b/src/components/footer/footer.stories.ts index 3c3d391a30..0e230c4767 100644 --- a/src/components/footer/footer.stories.ts +++ b/src/components/footer/footer.stories.ts @@ -190,7 +190,7 @@ const TemplateClockColumns = ({ ...args }): TemplateResult => html`
diff --git a/src/components/journey-summary/__snapshots__/journey-summary.spec.snap.js b/src/components/journey-summary/__snapshots__/journey-summary.spec.snap.js index 6fa41950fc..c76c84ae77 100644 --- a/src/components/journey-summary/__snapshots__/journey-summary.spec.snap.js +++ b/src/components/journey-summary/__snapshots__/journey-summary.spec.snap.js @@ -31,7 +31,7 @@ snapshots["sbb-journey-summary renders"] = - + @@ -55,7 +55,7 @@ snapshots["sbb-journey-summary renders without vias"] = - + @@ -79,7 +79,7 @@ snapshots["sbb-journey-summary renders with second journey"] = - +
@@ -118,7 +118,7 @@ snapshots["sbb-journey-summary renders with second journey"] =
- + diff --git a/src/components/journey-summary/journey-summary.spec.ts b/src/components/journey-summary/journey-summary.spec.ts index 93162f8863..d4f9c88c32 100644 --- a/src/components/journey-summary/journey-summary.spec.ts +++ b/src/components/journey-summary/journey-summary.spec.ts @@ -40,35 +40,35 @@ const dataWithoutVia: InterfaceSbbJourneySummaryAttributes = { describe(`sbb-journey-summary`, () => { it('renders', async () => { const root = (await fixture( - html` `, + html` `, )) as SbbJourneySummaryElement; root.trip = data; await waitForLitRender(root); expect(root).dom.to.be.equal(` - + `); await expect(root).shadowDom.to.be.equalSnapshot(); }); it('renders without vias', async () => { const root = (await fixture( - html` `, + html` `, )) as SbbJourneySummaryElement; root.trip = dataWithoutVia; await waitForLitRender(root); expect(root).dom.to.be.equal(` - + `); await expect(root).shadowDom.to.be.equalSnapshot(); }); it('renders with second journey', async () => { const root = (await fixture( - html` `, + html` `, )) as SbbJourneySummaryElement; root.trip = dataWithoutVia; root.tripBack = data; @@ -77,7 +77,7 @@ describe(`sbb-journey-summary`, () => { await waitForLitRender(root); expect(root).dom.to.be.equal(` - + `); await expect(root).shadowDom.to.be.equalSnapshot(); }); diff --git a/src/components/journey-summary/journey-summary.stories.ts b/src/components/journey-summary/journey-summary.stories.ts index 46087dd575..f0951cc987 100644 --- a/src/components/journey-summary/journey-summary.stories.ts +++ b/src/components/journey-summary/journey-summary.stories.ts @@ -67,18 +67,18 @@ const tripBack: InputType = { const defaultArgTypes: ArgTypes = { 'disable-animation': disableAnimation, - 'data-now': now, 'round-trip': roundTrip, 'header-level': headerLevel, + now, trip, tripBack, }; const defaultArgs: Args = { 'disable-animation': isChromatic(), - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), 'round-trip': false, 'header-level': headerLevel.options![2], + now: new Date('2022-12-05T12:11:00').valueOf(), trip: undefined, tripBack: undefined, }; diff --git a/src/components/journey-summary/journey-summary.ts b/src/components/journey-summary/journey-summary.ts index 643f648592..883096acd3 100644 --- a/src/components/journey-summary/journey-summary.ts +++ b/src/components/journey-summary/journey-summary.ts @@ -8,7 +8,6 @@ import { defaultDateAdapter, durationToTime, removeTimezoneFromISOTimeString, - readDataNow, } from '../core/datetime.js'; import { i18nTripDuration } from '../core/i18n.js'; import type { Leg } from '../core/timetable.js'; @@ -63,6 +62,9 @@ export class SbbJourneySummaryElement extends LitElement { */ @property({ attribute: 'disable-animation', type: Boolean }) public disableAnimation?: boolean; + /** A specific date for the current datetime (timestamp in milliseconds). */ + @property({ attribute: 'now' }) public dataNow?: number; + private _hasContentSlot: boolean = false; private _language = new SbbLanguageController(this); @@ -72,8 +74,7 @@ export class SbbJourneySummaryElement extends LitElement { } private _now(): number { - const dataNow = readDataNow(this); - return isNaN(dataNow) ? Date.now() : dataNow; + return this.dataNow ?? Date.now(); } /** renders the date of the journey or if it is the current or next day */ @@ -136,7 +137,7 @@ export class SbbJourneySummaryElement extends LitElement { .arrivalWalk=${arrivalWalk} .legs=${legs} .disableAnimation=${this.disableAnimation} - data-now=${this._now()} + now=${this._now()} > `; diff --git a/src/components/journey-summary/readme.md b/src/components/journey-summary/readme.md index 471cf86d84..81e88f4bd4 100644 --- a/src/components/journey-summary/readme.md +++ b/src/components/journey-summary/readme.md @@ -16,12 +16,16 @@ If the tripBack prop is passed to the component a second journey-summary, withou ``` +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). +This is helpful if you need a specific state of the component. + ## Properties | Name | Attribute | Privacy | Type | Default | Description | | ------------------ | ------------------- | ------- | --------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------- | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | | `disableAnimation` | `disable-animation` | public | `boolean \| undefined` | | Per default, the current location has a pulsating animation. You can disable the animation with this property. | | `headerLevel` | `header-level` | public | `SbbTitleLevel` | `'3'` | Heading level of the journey header element (e.g. h1-h6). | | `roundTrip` | `round-trip` | public | `boolean \| undefined` | | The RoundTrip prop. This prop controls if one or two arrows are displayed in the header. | diff --git a/src/components/pearl-chain-time/pearl-chain-time.spec.ts b/src/components/pearl-chain-time/pearl-chain-time.spec.ts index 55c8e19f52..41762f2838 100644 --- a/src/components/pearl-chain-time/pearl-chain-time.spec.ts +++ b/src/components/pearl-chain-time/pearl-chain-time.spec.ts @@ -17,7 +17,7 @@ describe(`sbb-pearl-chain-time`, () => { `); @@ -28,7 +28,7 @@ describe(`sbb-pearl-chain-time`, () => { ]; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); expect(element).shadowDom.to.be.equal(` @@ -39,7 +39,7 @@ describe(`sbb-pearl-chain-time`, () => { 12:00 - + `); @@ -67,7 +67,7 @@ describe(`sbb-pearl-chain-time`, () => { ]; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); expect(element).shadowDom.to.be.equal(` @@ -95,7 +95,7 @@ describe(`sbb-pearl-chain-time`, () => { 12:00 - + `); @@ -123,7 +123,7 @@ describe(`sbb-pearl-chain-time`, () => { ]; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); expect(element).shadowDom.to.be.equal(` @@ -134,7 +134,7 @@ describe(`sbb-pearl-chain-time`, () => { 12:00 - + `); @@ -180,7 +180,7 @@ describe(`sbb-pearl-chain-time`, () => { ]; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); expect(element).shadowDom.to.be.equal(` @@ -208,7 +208,7 @@ describe(`sbb-pearl-chain-time`, () => { 12:00 - + ``` -## Testing - -To specify a specific date for the current datetime, you can use the `data-now` attribute (timestamp in milliseconds). +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). This is helpful if you need a specific state of the component. @@ -58,6 +56,7 @@ This is helpful if you need a specific state of the component. | ------------------ | ------------------- | ------- | ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `arrivalTime` | `arrival-time` | public | `string \| undefined` | | Prop to render the arrival time - will be formatted as "H:mm" | | `arrivalWalk` | `arrival-walk` | public | `number \| undefined` | | Optional prop to render the walk time (in minutes) after arrival | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | | `departureTime` | `departure-time` | public | `string \| undefined` | | Prop to render the departure time - will be formatted as "H:mm" | | `departureWalk` | `departure-walk` | public | `number \| undefined` | | Optional prop to render the walk time (in minutes) before departure | | `disableAnimation` | `disable-animation` | public | `boolean \| undefined` | | Per default, the current location has a pulsating animation. You can disable the animation with this property. | diff --git a/src/components/pearl-chain/pearl-chain.stories.ts b/src/components/pearl-chain/pearl-chain.stories.ts index 8e41921982..ab3bceef95 100644 --- a/src/components/pearl-chain/pearl-chain.stories.ts +++ b/src/components/pearl-chain/pearl-chain.stories.ts @@ -32,12 +32,12 @@ const now: InputType = { const defaultArgTypes: ArgTypes = { 'disable-animation': disableAnimation, - 'data-now': now, + now, }; const defaultArgs: Args = { 'disable-animation': isChromatic(), - 'data-now': new Date('2022-12-01T12:11:00').valueOf(), + now: new Date('2022-12-01T12:11:00').valueOf(), }; const Template = ({ legs, ...args }: Args): TemplateResult => { @@ -86,7 +86,7 @@ export const withPosition: StoryObj = { args: { ...defaultArgs, legs: [progressLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; @@ -96,7 +96,7 @@ export const Past: StoryObj = { args: { ...defaultArgs, legs: [pastLeg, pastLeg], - 'data-now': new Date('2023-11-01T12:11:00').valueOf(), + now: new Date('2023-11-01T12:11:00').valueOf(), }, }; @@ -106,7 +106,7 @@ export const DepartureStopSkipped: StoryObj = { args: { ...defaultArgs, legs: [pastLeg, progressLeg, longFutureLeg, redirectedOnDepartureLeg, futureLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; @@ -116,7 +116,7 @@ export const ArrivalStopSkipped: StoryObj = { args: { ...defaultArgs, legs: [pastLeg, progressLeg, longFutureLeg, redirectedOnArrivalLeg, futureLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; @@ -126,7 +126,7 @@ export const FirstStopSkipped: StoryObj = { args: { ...defaultArgs, legs: [redirectedOnDepartureLeg, futureLeg, longFutureLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; @@ -136,7 +136,7 @@ export const LastStopSkipped: StoryObj = { args: { ...defaultArgs, legs: [futureLeg, longFutureLeg, redirectedOnArrivalLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; @@ -146,7 +146,7 @@ export const Mixed: StoryObj = { args: { ...defaultArgs, legs: [pastLeg, progressLeg, longFutureLeg, cancelledLeg, futureLeg], - 'data-now': new Date('2022-12-05T12:11:00').valueOf(), + now: new Date('2022-12-05T12:11:00').valueOf(), }, }; diff --git a/src/components/pearl-chain/pearl-chain.ts b/src/components/pearl-chain/pearl-chain.ts index 46c5a00606..d8168103bb 100644 --- a/src/components/pearl-chain/pearl-chain.ts +++ b/src/components/pearl-chain/pearl-chain.ts @@ -4,7 +4,7 @@ import { html, LitElement, nothing } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { styleMap } from 'lit/directives/style-map.js'; -import { readDataNow, removeTimezoneFromISOTimeString } from '../core/datetime.js'; +import { removeTimezoneFromISOTimeString } from '../core/datetime.js'; import type { Leg, PtRideLeg } from '../core/timetable.js'; import { isRideLeg } from '../core/timetable.js'; @@ -35,9 +35,11 @@ export class SbbPearlChainElement extends LitElement { */ @property({ attribute: 'disable-animation', type: Boolean }) public disableAnimation?: boolean; + /** A specific date for the current datetime (timestamp in milliseconds). */ + @property({ attribute: 'now' }) public dataNow?: number; + private _now(): number { - const dataNow = readDataNow(this); - return isNaN(dataNow) ? Date.now() : dataNow; + return this.dataNow ?? Date.now(); } private _getAllDuration(legs: PtRideLeg[]): number { diff --git a/src/components/pearl-chain/readme.md b/src/components/pearl-chain/readme.md index 4b9532c8ae..26ace5f998 100644 --- a/src/components/pearl-chain/readme.md +++ b/src/components/pearl-chain/readme.md @@ -46,9 +46,7 @@ The `legs` property is mandatory. ``` -## Testing - -To specify a specific date for the current datetime, you can use the `data-now` attribute (timestamp in milliseconds). +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). This is helpful if you need a specific state of the component. @@ -57,5 +55,6 @@ This is helpful if you need a specific state of the component. | Name | Attribute | Privacy | Type | Default | Description | | ------------------ | ------------------- | ------- | ----------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | | `disableAnimation` | `disable-animation` | public | `boolean \| undefined` | | Per default, the current location has a pulsating animation. You can disable the animation with this property. | | `legs` | `legs` | public | `(Leg \| PtRideLeg)[] \| undefined` | | Define the legs of the pearl-chain. Format: `{"legs": \[{"duration": 25}, ...]}` `duration` in minutes. Duration of the leg is relative to the total travel time. Example: departure 16:30, change at 16:40, arrival at 17:00. So the change should have a duration of 33.33%. | diff --git a/src/components/timetable-row/readme.md b/src/components/timetable-row/readme.md index dc8c2d4404..4e5f98f84d 100644 --- a/src/components/timetable-row/readme.md +++ b/src/components/timetable-row/readme.md @@ -39,9 +39,7 @@ _`tripProp` property_ ``` -## Testing - -To specify a specific date for the current datetime, you can use the `data-now` attribute (timestamp in milliseconds). +To specify a specific date for the current datetime, you can use the `now` property (timestamp in milliseconds). This is helpful if you need a specific state of the component. @@ -54,6 +52,7 @@ This is helpful if you need a specific state of the component. | `active` | `active` | public | `boolean \| undefined` | | When this prop is true the sbb-card will be in the active state. | | `boarding` | `boarding` | public | `Boarding \| undefined` | | This will be forwarded to the notices section | | `cardActionLabel` | `card-action-label` | public | `string \| undefined` | | Hidden label for the card action. It overrides the automatically generated accessibility text for the component. Use this prop to provide custom accessibility information for the component. | +| `dataNow` | `now` | public | `number \| undefined` | | A specific date for the current datetime (timestamp in milliseconds). | | `disableAnimation` | `disable-animation` | public | `boolean \| undefined` | | This will be forwarded to the sbb-pearl-chain component - if true the position won't be animated. | | `loadingPrice` | `loading-price` | public | `boolean` | `false` | The loading state - when this is true it will be render skeleton with an idling animation | | `loadingTrip` | `loading-trip` | public | `boolean` | `false` | The loading state - when this is true it will be render skeleton with an idling animation | diff --git a/src/components/timetable-row/timetable-row.spec.ts b/src/components/timetable-row/timetable-row.spec.ts index 8fff9830e8..5b176c559b 100644 --- a/src/components/timetable-row/timetable-row.spec.ts +++ b/src/components/timetable-row/timetable-row.spec.ts @@ -21,13 +21,13 @@ describe(`sbb-timetable-row`, () => { describe('sbb-timetable-row with defaultTrip', () => { it('renders component with config', async () => { - element = await fixture(html``); + element = await fixture(html``); element.trip = defaultTrip as ITripItem; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); @@ -87,7 +87,7 @@ describe(`sbb-timetable-row`, () => {

@@ -112,13 +112,13 @@ describe(`sbb-timetable-row`, () => { describe('sbb-timetable-row with BusTrip', () => { it('renders component with config', async () => { - element = await fixture(html``); + element = await fixture(html``); element.trip = busTrip as ITripItem; await waitForLitRender(element); expect(element).dom.to.be.equal(` - + `); @@ -144,7 +144,7 @@ describe(`sbb-timetable-row`, () => { Direction Spiegel, Blinzern

- +