Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Demo custom elements on datepickers and use them in lookbook

Oliver Günther0a0351349135
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index d540fd3a030..64ee9f9a717 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -17,6 +17,7 @@
"@angular/compiler": "^16.1.8",
"@angular/compiler-cli": "^16.1.8",
"@angular/core": "^16.1.8",
+ "@angular/elements": "^16.1.7",
"@angular/forms": "^16.1.8",
"@angular/platform-browser": "^16.1.8",
"@angular/platform-browser-dynamic": "^16.1.8",
@@ -1750,6 +1751,21 @@
"zone.js": "~0.13.0"
}
},
+ "node_modules/@angular/elements": {
+ "version": "16.1.7",
+ "resolved": "https://registry.npmjs.org/@angular/elements/-/elements-16.1.7.tgz",
+ "integrity": "sha512-lAFTVLmLnI/W7wNW4W9JMzptcxZF3btmk9Ybffw7QpqMIstYHkTCq9mqXvJQzywGAd6+NGQEZBRC1gYPcZD3Pw==",
+ "dependencies": {
+ "tslib": "^2.3.0"
+ },
+ "engines": {
+ "node": "^16.14.0 || >=18.10.0"
+ },
+ "peerDependencies": {
+ "@angular/core": "16.1.7",
+ "rxjs": "^6.5.3 || ^7.4.0"
+ }
+ },
"node_modules/@angular/forms": {
"version": "16.1.8",
"resolved": "https://registry.npmjs.org/@angular/forms/-/forms-16.1.8.tgz",
@@ -39138,6 +39154,14 @@
"tslib": "^2.3.0"
}
},
+ "@angular/elements": {
+ "version": "16.1.7",
+ "resolved": "https://registry.npmjs.org/@angular/elements/-/elements-16.1.7.tgz",
+ "integrity": "sha512-lAFTVLmLnI/W7wNW4W9JMzptcxZF3btmk9Ybffw7QpqMIstYHkTCq9mqXvJQzywGAd6+NGQEZBRC1gYPcZD3Pw==",
+ "requires": {
+ "tslib": "^2.3.0"
+ }
+ },
"@angular/forms": {
"version": "16.1.8",
"resolved": "https://registry.npmjs.org/@angular/forms/-/forms-16.1.8.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index 9db0c9fe5b3..b5a9124d350 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -89,6 +89,7 @@
"@angular/compiler": "^16.1.8",
"@angular/compiler-cli": "^16.1.8",
"@angular/core": "^16.1.8",
+ "@angular/elements": "^16.1.7",
"@angular/forms": "^16.1.8",
"@angular/platform-browser": "^16.1.8",
"@angular/platform-browser-dynamic": "^16.1.8",
diff --git a/frontend/src/app/shared/components/datepicker/basic-datepicker.module.ts b/frontend/src/app/shared/components/datepicker/basic-datepicker.module.ts
index 984f9505167..4ae0668af04 100644
--- a/frontend/src/app/shared/components/datepicker/basic-datepicker.module.ts
+++ b/frontend/src/app/shared/components/datepicker/basic-datepicker.module.ts
@@ -1,13 +1,11 @@
-import { NgModule } from '@angular/core';
-import {
- FormsModule,
- ReactiveFormsModule,
-} from '@angular/forms';
+import { Injector, NgModule } from '@angular/core';
+import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { OpBasicRangeDatePickerComponent } from './basic-range-date-picker/basic-range-date-picker.component';
import { OpBasicSingleDatePickerComponent } from './basic-single-date-picker/basic-single-date-picker.component';
+import { registerCustomElement } from 'core-app/shared/helpers/angular/custom-elements.helper';
@NgModule({
imports: [
@@ -30,4 +28,9 @@ import { OpBasicSingleDatePickerComponent } from './basic-single-date-picker/bas
OpBasicSingleDatePickerComponent,
],
})
-export class OpBasicDatePickerModule { }
+export class OpBasicDatePickerModule {
+ constructor(readonly injector:Injector) {
+ registerCustomElement('opce-single-date-picker', OpBasicSingleDatePickerComponent, { injector });
+ registerCustomElement('opce-range-date-picker', OpBasicRangeDatePickerComponent, { injector });
+ }
+}
diff --git a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.ts b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.ts
index 0affd91ea3d..e9fa79f2505 100644
--- a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.ts
+++ b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.ts
@@ -89,9 +89,14 @@ export class OpBasicRangeDatePickerComponent implements OnInit, ControlValueAcce
private _value:string[] = [];
- @Input() set value(newValue:string[]) {
- this._value = newValue;
- this.stringValue = this.resolveDateArrayToString(newValue);
+ @Input() set value(newValue:string|string[]) {
+ if (typeof newValue === 'string') {
+ this._value = newValue.split(/\s-\s/);
+ } else {
+ this._value = newValue;
+ }
+
+ this.stringValue = this.resolveDateArrayToString(this._value);
}
get value() {
diff --git a/frontend/src/app/shared/helpers/angular/custom-elements.helper.ts b/frontend/src/app/shared/helpers/angular/custom-elements.helper.ts
new file mode 100644
index 00000000000..882359357db
--- /dev/null
+++ b/frontend/src/app/shared/helpers/angular/custom-elements.helper.ts
@@ -0,0 +1,14 @@
+import 'reflect-metadata';
+import { Type } from '@angular/core';
+import { debugLog } from 'core-app/shared/helpers/debug_output';
+import { createCustomElement, NgElementConfig } from '@angular/elements';
+
+export function registerCustomElement(name:string, component:Type<unknown>, config:NgElementConfig) {
+ const wrappedElement = createCustomElement(component, config);
+ if (customElements.get(name)) {
+ debugLog(`${name} custom element already registered.`);
+ return;
+ }
+
+ customElements.define(name, wrappedElement);
+}
diff --git a/frontend/src/stories/DatePicker.mdx b/frontend/src/stories/DatePicker.mdx
deleted file mode 100644
index efda10b64c2..00000000000
--- a/frontend/src/stories/DatePicker.mdx
+++ /dev/null
@@ -1,77 +0,0 @@
-import { Canvas, Meta, ArgsTable } from '@storybook/blocks';
-
-import { OpBasicSingleDatePickerComponent } from '../app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component'
-import { OpBasicRangeDatePickerComponent } from '../app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component'
-
-import * as DatePickerStories from './DatePicker.stories';
-
-<Meta of={DatePickerStories} />
-
-# Basic Date Picker
-
-The basic date picker is a key element in OpenProject and is displayed any time the user has to input a date.
-
-Basic date pickers are attached to existing date input fields and is displayed as a drop-down when that date input field is in focus. It consists of only the mini-calendar component.
-
-The basic date picker can also be placed inside modals like the work package date drop modal and the Baseline drop modal.
-
-<Canvas of={DatePickerStories.Single} />
-
-## External dependencies
-
-All date picker are built on the [Flatpickr javascript library](https://flatpickr.js.org/). The library gives us certain functionality out of the box with a fairly high degree of customisation, but also introduces limits (which will be mentioned below when relevant).
-
-Please read the [Flatpickr documentation](https://flatpickr.js.org/instance-methods-properties-elements/) before using or contributing to date pickers.
-
-## Sub-variants
-
-The structure is very basic and consists only of one mini calendar. There are two sub-variants:
-
-### Single
-
-**Single** allows picking just one date (2023-02-09)
-
-<Canvas of={DatePickerStories.SingleWithValue} />
-
-### Range
-
-**Range:** allows inputing a range (2023-02-09 - 2023-02-14), and shows the date picker with two months.
-
-<Canvas of={DatePickerStories.RangeWithValue} />
-
-## Mobile
-
-The basic date picker does not have a mobile version. On mobile devices, Flatpickr will automatically gracefully degrade to the device's native date picker.
-
-For more complex implementations involving date pickers (the work package date field or the baseline modal), please refer to documentation concerning those specific features for notes on mobile-specific rendering.
-
-## Accessibility
-
-The basic date picker itself never receives focus (and thus does not afford keyboard navigation or screen reader compatibility). However, the date input field that the date picker is attached to can receive focus and all users can type in the desired date in the ISO format.
-
-## Usage
-
-The basic date picker is used in:
-
-### Single date picker
-
-- Log time
-- Log unit costs
-- Custom fields (work package and project)
-- Meeting minutes
-- Work package table filters
-- Work package bulk edit form
-- Announcement edit form
-- Version new and edit form and custom fields
-- Project filters
-- Cost report filters
-- Budget forms
-- Dynamic forms
-
-<ArgsTable of={OpBasicSingleDatePickerComponent} />
-
-### Range date picker
-
-- Pause date reminders
-
-<ArgsTable of={OpBasicRangeDatePickerComponent} />
diff --git a/frontend/src/stories/DatePicker.stories.ts b/frontend/src/stories/DatePicker.stories.ts
deleted file mode 100644
index 23291623ee7..00000000000
--- a/frontend/src/stories/DatePicker.stories.ts
+++ /dev/null
@@ -1,106 +0,0 @@
-import type { Meta, StoryObj } from '@storybook/angular';
-import { moduleMetadata } from '@storybook/angular';
-import * as moment from 'moment';
-
-import { TimezoneService } from '../app/core/datetime/timezone.service';
-import { TimezoneServiceStub } from './timezone.service.stub';
-
-import { I18nService } from '../app/core/i18n/i18n.service';
-import { I18nServiceStub } from './i18n.service.stub';
-
-import { WeekdayService } from '../app/core/days/weekday.service';
-import { WeekdayServiceStub } from './weekday.service.stub';
-
-import { DayResourceService } from '../app/core/state/days/day.service';
-import { DayResourceServiceStub } from './day.service.stub';
-
-import { ConfigurationService } from '../app/core/config/configuration.service';
-import { ConfigurationServiceStub } from './configuration.service.stub';
-
-import { States } from '../app/core/states/states.service';
-
-import { OpBasicDatePickerModule } from '../app/shared/components/datepicker/basic-datepicker.module';
-import { OpBasicSingleDatePickerComponent } from '../app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component'
-
-const meta:Meta = {
- title: 'Patterns/DatePicker',
- component: OpBasicSingleDatePickerComponent,
- decorators: [
- moduleMetadata({
- imports: [
- OpBasicDatePickerModule,
- ],
- providers: [
- {
- provide: TimezoneService,
- useValue: TimezoneServiceStub,
- },
- {
- provide: ConfigurationService,
- useValue: ConfigurationServiceStub,
- },
- {
- provide: States,
- useValue: new States(),
- },
- {
- provide: I18nService,
- useValue: I18nServiceStub,
- },
- {
- provide: WeekdayService,
- useFactory: () => new WeekdayServiceStub(),
- },
- {
- provide: DayResourceService,
- useFactory: () => new DayResourceServiceStub(),
- },
- ],
- }),
- ],
-};
-
-export default meta;
-type Story = StoryObj;
-
-export const Single:Story = {
- render: (args) => ({
- props: {
- ...args,
- },
- template: `
- <op-basic-single-date-picker></op-basic-single-date-picker>
- `,
- }),
-};
-
-export const SingleWithValue:Story = {
- render: (args) => ({
- props: {
- ...args,
- date: moment(new Date()).format('YYYY-MM-DD'),
- },
- template: `
- <op-basic-single-date-picker
- [value]="date"
- ></op-basic-single-date-picker>
- `,
- }),
-};
-
-export const RangeWithValue:Story = {
- render: (args) => ({
- props: {
- ...args,
- dates: [
- moment(new Date()).format('YYYY-MM-DD'),
- moment(new Date()).add(4, 'days').format('YYYY-MM-DD'),
- ],
- },
- template: `
- <op-basic-range-date-picker
- [value]="dates"
- ></op-basic-range-date-picker>
- `,
- }),
-};
diff --git a/frontend/src/stories/day.service.stub.ts b/frontend/src/stories/day.service.stub.ts
deleted file mode 100644
index cfcae302fa0..00000000000
--- a/frontend/src/stories/day.service.stub.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { Injectable } from '@angular/core';
-import { Observable, of } from 'rxjs';
-import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
-import {
- ApiV3ListParameters,
-} from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
-import { DayStore } from 'core-app/core/state/days/day.store';
-import { IDay } from 'core-app/core/state/days/day.model';
-import { ResourceStore } from 'core-app/core/state/resource-store.service';
-
-@Injectable()
-export class DayResourceServiceStub {
- protected basePath():string {
- return '';
- }
-
- async isNonWorkingDay$(input:Date):Promise<boolean> {
- const day = input.getDay();
- return day > 5 || day === 0;
- }
-
- requireNonWorkingYear$(_date:Date|string):Observable<IDay[]> {
- return of([]);
- }
-
- requireNonWorkingYears$(_start:Date|string, _end:Date|string):Observable<IDay[]> {
- return of([]);
- }
-
- fetchCollection(_params:ApiV3ListParameters):Observable<IHALCollection<IDay>> {
- return of({
- _type: 'Collection',
- count: 0,
- pageSize: 10,
- offset: 0,
- total: 0,
- _embedded: {
- elements: [],
- },
- });
- }
-
- protected createStore():ResourceStore<IDay> {
- return new DayStore();
- }
-}
diff --git a/spec/components/previews/datepicker_preview.rb b/spec/components/previews/datepicker_preview.rb
new file mode 100644
index 00000000000..40a2c38fdae
--- /dev/null
+++ b/spec/components/previews/datepicker_preview.rb
@@ -0,0 +1,52 @@
+# @logical_path OpenProject
+class DatepickerPreview < Lookbook::Preview
+ ##
+ # **Single date picker**
+ # ---------------------
+ # The basic date picker is a key element in OpenProject and is displayed
+ # any time the user has to input a date.
+ #
+ # Basic date pickers are attached to existing date input fields
+ # and is displayed as a drop-down when that date input field is in focus.
+ # It consists of only the mini-calendar component.
+ #
+ # The basic date picker can also be placed inside modals
+ # like the work package date drop modal and the Baseline drop modal.
+ # All date picker are built on the [Flatpickr javascript library](https://flatpickr.js.org/).
+ #
+ # On mobile devices, the component will automatically degrade to the device's native date picker.
+ #
+ # For more complex implementations involving date pickers (the work package date field or the baseline modal),
+ # please refer to documentation concerning those specific features for notes on mobile-specific rendering.
+ #
+ # The library gives us certain functionality out of the box with a fairly high degree of customisation,
+ # but also introduces limits (which will be mentioned below when relevant).
+ #
+ # Please read the [Flatpickr documentation](https://flatpickr.js.org/instance-methods-properties-elements/)
+ # before using or contributing to date pickers.
+ #
+ # @param value date
+ def single(value: Time.zone.today.iso8601)
+ render_with_template(locals: { value: })
+ end
+
+ ##
+ # **Range date picker**
+ # ---------------------
+ # Range datepicker allow inputing a range (2023-02-09 - 2023-02-14),
+ # and shows the date picker with two months.
+ #
+ # All date picker are built on the [Flatpickr javascript library](https://flatpickr.js.org/).
+ # On mobile devices, the component will automatically degrade to two inputs with the device's native date picker.
+ #
+ # The library gives us certain functionality out of the box with a fairly high degree of customisation,
+ # but also introduces limits (which will be mentioned below when relevant).
+ #
+ # Please read the [Flatpickr documentation](https://flatpickr.js.org/instance-methods-properties-elements/)
+ # before using or contributing to date pickers.
+ #
+ # @param value text
+ def range(value: "#{Time.zone.today.iso8601} - #{Time.zone.today.iso8601}")
+ render_with_template(locals: { value: })
+ end
+end
diff --git a/spec/components/previews/datepicker_preview/range.html.erb b/spec/components/previews/datepicker_preview/range.html.erb
new file mode 100644
index 00000000000..559e35bba55
--- /dev/null
+++ b/spec/components/previews/datepicker_preview/range.html.erb
@@ -0,0 +1 @@
+<%= tag :'opce-range-date-picker', value: %>
diff --git a/spec/components/previews/datepicker_preview/single.html.erb b/spec/components/previews/datepicker_preview/single.html.erb
new file mode 100644
index 00000000000..bd6afbdcdc1
--- /dev/null
+++ b/spec/components/previews/datepicker_preview/single.html.erb
@@ -0,0 +1 @@
+<%= tag :'opce-single-date-picker', value: %>