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

Use native input for mobile on basic datepickers (#12268)

Oliver Güntherca06cae13fa0
diff --git a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.html b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.html
index e78947ec4a9..0c8eb6048c2 100644
--- a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.html
+++ b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.html
@@ -1,4 +1,5 @@
<input
+ *ngIf="!mobile"
#input
type="text"
class="spot-input op-basic-range-datepicker--input"
@@ -7,10 +8,44 @@
[attr.data-value]="value"
[id]="id"
[name]="name"
- [attr.name]="name"
[required]="required"
[disabled]="disabled"
[ngModel]="stringValue"
(ngModelChange)="changeValueFromInputDebounced($event)"
(focus)="showDatePicker()"
/>
+<ng-container *ngIf="mobile">
+ <input
+ type="date"
+ class="spot-input"
+ data-qa-selector="op-basic-range-date-picker-start"
+ [ngClass]="inputClassNames"
+ id="{{ id }}-start"
+ name="{{ name }}-start"
+ [required]="required"
+ [disabled]="disabled"
+ [ngModel]="value[0] || ''"
+ (ngModelChange)="changeValueFromInputDebounced([$event, value[1] || ''])"
+ />
+ <span class="op-basic-range-datepicker--spacer" [textContent]="text.spacer"></span>
+ <input
+ type="date"
+ class="spot-input"
+ data-qa-selector="op-basic-range-date-picker-end"
+ [ngClass]="inputClassNames"
+ id="{{ id }}-end"
+ name="{{ name }}-end"
+ [required]="required"
+ [disabled]="disabled"
+ [ngModel]="value[1] || ''"
+ (ngModelChange)="changeValueFromInputDebounced([value[0] || '', $event])"
+ />
+ <input
+ type="hidden"
+ data-qa-selector="op-basic-range-date-picker--value"
+ [attr.data-value]="stringValue"
+ [id]="id"
+ [name]="name"
+ [ngModel]="stringValue"
+ />
+</ng-container>
diff --git a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.sass b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.sass
index f8b5777b749..3dc817c9a2a 100644
--- a/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.sass
+++ b/frontend/src/app/shared/components/datepicker/basic-range-date-picker/basic-range-date-picker.component.sass
@@ -1,3 +1,9 @@
.op-basic-range-datepicker
+ &_mobile
+ display: flex
+ flex-direction: column
+ align-items: center
&--input
min-width: 12rem
+ &--spacer
+ padding: 0 0.5rem
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 af87b7db579..0affd91ea3d 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
@@ -37,6 +37,7 @@ import {
HostBinding,
Injector,
Input,
+ OnInit,
Output,
ViewChild,
ViewEncapsulation,
@@ -46,14 +47,17 @@ import {
ControlValueAccessor,
NG_VALUE_ACCESSOR,
} from '@angular/forms';
-import { onDayCreate } from 'core-app/shared/components/datepicker/helpers/date-modal.helpers';
+import {
+ onDayCreate,
+ validDate,
+} from 'core-app/shared/components/datepicker/helpers/date-modal.helpers';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
import { DatePicker } from '../datepicker';
import flatpickr from 'flatpickr';
import { DayElement } from 'flatpickr/dist/types/instance';
import { populateInputsFromDataset } from '../../dataset-inputs';
import { debounce } from 'lodash';
-import { SpotDropModalTeleportationService } from 'core-app/spot/components/drop-modal/drop-modal-teleportation.service';
+import { DeviceService } from 'core-app/core/browser/device.service';
export const rangeSeparator = '-';
@@ -76,12 +80,12 @@ export const opBasicRangeDatePickerSelector = 'op-basic-range-date-picker';
},
],
})
-export class OpBasicRangeDatePickerComponent implements ControlValueAccessor, AfterViewInit {
+export class OpBasicRangeDatePickerComponent implements OnInit, ControlValueAccessor, AfterViewInit {
@HostBinding('class.op-basic-range-datepicker') className = true;
- @Output('valueChange') valueChange = new EventEmitter();
+ @HostBinding('class.op-basic-range-datepicker_mobile') mobile = false;
- public stringValue = '';
+ @Output() valueChange = new EventEmitter();
private _value:string[] = [];
@@ -108,37 +112,52 @@ export class OpBasicRangeDatePickerComponent implements ControlValueAccessor, Af
@ViewChild('input') input:ElementRef;
+ stringValue = '';
+
+ datePickerInstance:DatePicker;
+
text = {
date: this.I18n.t('js.work_packages.properties.date'),
placeholder: this.I18n.t('js.placeholders.default'),
+ spacer: this.I18n.t('js.filter.value_spacer'),
};
- public datePickerInstance:DatePicker;
-
constructor(
readonly I18n:I18nService,
readonly timezoneService:TimezoneService,
readonly injector:Injector,
readonly cdRef:ChangeDetectorRef,
readonly elementRef:ElementRef,
- readonly spotDropModalTeleportationService:SpotDropModalTeleportationService,
+ readonly deviceService:DeviceService,
) {
populateInputsFromDataset(this);
}
+ ngOnInit() {
+ this.mobile = this.deviceService.isMobile;
+ }
+
ngAfterViewInit():void {
- this.initializeDatePicker();
+ if (!this.mobile) {
+ this.initializeDatePicker();
+ }
}
changeValueFromInputDebounced = debounce(this.changeValueFromInput.bind(this), 16);
- changeValueFromInput(value:string) {
- const newDates = this.resolveDateStringToArray(value);
- this.valueChange.emit(newDates);
+ changeValueFromInput(value:string|string[]) {
+ const newDates = (typeof value === 'string') ? this.resolveDateStringToArray(value) : value;
+
this.onChange(newDates);
this.onTouched(newDates);
this.writeValue(newDates);
this.cdRef.detectChanges();
+
+ if (newDates.find((el) => !validDate(el))) {
+ return;
+ }
+
+ this.valueChange.emit(newDates);
}
showDatePicker():void {
diff --git a/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.html b/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.html
index ae47f287a0a..ec3c52710db 100644
--- a/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.html
+++ b/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.html
@@ -1,4 +1,5 @@
<input
+ *ngIf="!mobile"
#input
type="text"
class="spot-input"
@@ -9,7 +10,22 @@
[attr.name]="name"
[required]="required"
[disabled]="disabled"
- (input)="changeValueFromInput($event)"
+ (input)="changeValueFromInput($event.target.value)"
(focus)="showDatePicker()"
[attr.data-remote-field-key]="remoteFieldKey"
/>
+
+<ng-container *ngIf="mobile">
+ <input
+ type="date"
+ class="spot-input"
+ [ngClass]="inputClassNames"
+ [attr.data-value]="value"
+ [id]="id"
+ [name]="name"
+ [required]="required"
+ [disabled]="disabled"
+ [ngModel]="value"
+ (ngModelChange)="changeValueFromInput($event)"
+ />
+</ng-container>
diff --git a/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.ts b/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.ts
index 74a19ee6657..604e3b560e3 100644
--- a/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.ts
+++ b/frontend/src/app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component.ts
@@ -37,6 +37,7 @@ import {
Injector,
Input,
OnDestroy,
+ OnInit,
Output,
ViewChild,
ViewEncapsulation,
@@ -55,7 +56,7 @@ import { DatePicker } from '../datepicker';
import flatpickr from 'flatpickr';
import { DayElement } from 'flatpickr/dist/types/instance';
import { populateInputsFromDataset } from '../../dataset-inputs';
-import { SpotDropModalTeleportationService } from 'core-app/spot/components/drop-modal/drop-modal-teleportation.service';
+import { DeviceService } from 'core-app/core/browser/device.service';
export const opBasicSingleDatePickerSelector = 'op-basic-single-date-picker';
@@ -73,7 +74,7 @@ export const opBasicSingleDatePickerSelector = 'op-basic-single-date-picker';
},
],
})
-export class OpBasicSingleDatePickerComponent implements ControlValueAccessor, AfterViewInit, OnDestroy {
+export class OpBasicSingleDatePickerComponent implements ControlValueAccessor, OnInit, AfterViewInit, OnDestroy {
@Output() valueChange = new EventEmitter();
@Output() picked = new EventEmitter();
@@ -104,6 +105,8 @@ export class OpBasicSingleDatePickerComponent implements ControlValueAccessor, A
@ViewChild('input') input:ElementRef;
+ mobile = false;
+
text = {
date: this.I18n.t('js.work_packages.properties.date'),
placeholder: this.I18n.t('js.placeholders.default'),
@@ -117,32 +120,32 @@ export class OpBasicSingleDatePickerComponent implements ControlValueAccessor, A
readonly injector:Injector,
readonly cdRef:ChangeDetectorRef,
readonly elementRef:ElementRef,
- readonly spotDropModalTeleportationService:SpotDropModalTeleportationService,
+ readonly deviceService:DeviceService,
) {
populateInputsFromDataset(this);
}
+ ngOnInit() {
+ this.mobile = this.deviceService.isMobile;
+ }
+
ngAfterViewInit():void {
- this.initializeDatePicker();
+ if (!this.mobile) {
+ this.initializeDatePicker();
+ }
}
ngOnDestroy():void {
this.datePickerInstance?.destroy();
}
- changeValueFromInput($event:KeyboardEvent) {
- const value = ($event.target as HTMLInputElement).value;
+ changeValueFromInput(value:string) {
if (validDate(value)) {
- const dateString = this.timezoneService.formattedISODate(value);
- this.datePickerInstance.setDates(dateString);
- this.valueChange.emit(dateString);
- this.onTouched(dateString);
- this.onChange(dateString);
- this.writeValue(dateString);
- } else if (value === '') {
- this.datePickerInstance.setDates('');
- this.onTouched('');
- this.onChange('');
+ this.onTouched(value);
+ this.onChange(value);
+ this.writeValue(value);
+ this.datePickerInstance?.setDates(value);
+ this.valueChange.emit(value);
}
}
diff --git a/spec/features/work_packages/display_representations/switch_display_representations_spec.rb b/spec/features/work_packages/display_representations/switch_display_representations_spec.rb
index e1dff76e05d..2844ca69b59 100644
--- a/spec/features/work_packages/display_representations/switch_display_representations_spec.rb
+++ b/spec/features/work_packages/display_representations/switch_display_representations_spec.rb
@@ -111,24 +111,9 @@ describe 'Switching work package view',
end
context 'switching to mobile card view' do
- let!(:height_before) do
- page.driver.browser.manage.window.size.height
- end
- let!(:width_before) do
- page.driver.browser.manage.window.size.width
- end
-
- after do
- page.driver.browser.manage.window.resize_to(width_before, height_before)
- end
+ include_context 'with mobile screen size'
it 'can switch the representation automatically on mobile after a refresh' do
- # Change browser size to mobile
- page.driver.browser.manage.window.resize_to(679, 1080)
-
- # Expect the representation to switch to card on mobile
- page.driver.browser.navigate.refresh
-
# It shows the elements as cards
cards.expect_work_package_listed wp_1, wp_2
diff --git a/spec/features/work_packages/table/queries/mobile_date_filter_spec.rb b/spec/features/work_packages/table/queries/mobile_date_filter_spec.rb
new file mode 100644
index 00000000000..7c6973d2482
--- /dev/null
+++ b/spec/features/work_packages/table/queries/mobile_date_filter_spec.rb
@@ -0,0 +1,100 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe 'mobile date filter work packages', js: true do
+ shared_let(:user) { create(:admin) }
+ shared_let(:project) { create(:project) }
+ shared_let(:wp_table) { Pages::WorkPackagesTable.new(project) }
+ shared_let(:wp_cards) { Pages::WorkPackageCards.new(project) }
+ shared_let(:filters) { Components::WorkPackages::Filters.new }
+ shared_let(:work_package_with_due_date) { create(:work_package, project:, due_date: Date.current) }
+ shared_let(:work_package_without_due_date) { create(:work_package, project:, due_date: 5.days.from_now) }
+
+ current_user { user }
+
+ include_context 'with mobile screen size'
+
+ before do
+ wp_table.visit!
+ end
+
+ context 'when filtering between finish date' do
+ it 'allows filtering, saving and retrieving and altering the saved filter' do
+ filters.open
+ filters.add_filter('Finish date')
+ filters.set_operator('Finish date', 'between', 'dueDate')
+
+ start_field = find('[data-qa-selector="op-basic-range-date-picker-start"]')
+ end_field = find('[data-qa-selector="op-basic-range-date-picker-end"]')
+
+ start_field.native.clear
+ end_field.native.clear
+
+ start_field.set 1.day.ago.strftime('%m/%d/%Y')
+ end_field.set Date.current.strftime('%m/%d/%Y')
+
+ loading_indicator_saveguard
+ wp_cards.expect_work_package_listed work_package_with_due_date
+ wp_cards.expect_work_package_not_listed work_package_without_due_date
+
+ wp_table.save_as('Some query name')
+ wp_table.expect_and_dismiss_toaster(message: 'Successful creation.')
+
+ last_query = Query.last
+ date_filter = last_query.filters.last
+ expect(date_filter.values).to eq [1.day.ago.to_date.iso8601, Date.current.iso8601]
+ end
+ end
+
+ context 'when filtering on finish date' do
+ it 'allows filtering, saving and retrieving and altering the saved filter' do
+ filters.open
+ filters.add_filter('Finish date')
+ filters.set_operator('Finish date', 'on', 'dueDate')
+
+ date_field = find_field 'values-dueDate'
+ expect(date_field['type']).to eq 'date'
+
+ date_field.native.clear
+ date_field.set Date.current.strftime('%m/%d/%Y')
+
+ loading_indicator_saveguard
+ wp_cards.expect_work_package_listed work_package_with_due_date
+ wp_cards.expect_work_package_not_listed work_package_without_due_date
+
+ wp_table.save_as('Some query name')
+ wp_table.expect_and_dismiss_toaster(message: 'Successful creation.')
+
+ last_query = Query.last
+ date_filter = last_query.filters.last
+ expect(date_filter.values).to eq [Date.current.iso8601]
+ end
+ end
+end
diff --git a/spec/support/shared/with_mobile_screen.rb b/spec/support/shared/with_mobile_screen.rb
new file mode 100644
index 00000000000..1ff58ae4c70
--- /dev/null
+++ b/spec/support/shared/with_mobile_screen.rb
@@ -0,0 +1,46 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+
+shared_context 'with mobile screen size' do |width, height|
+ let!(:height_before) do
+ page.driver.browser.manage.window.size.height
+ end
+ let!(:width_before) do
+ page.driver.browser.manage.window.size.width
+ end
+
+ before do
+ # Change browser size
+ page.driver.browser.manage.window.resize_to(width || 500, height || 1000)
+
+ # Refresh the page
+ page.driver.browser.navigate.refresh
+ end
+
+ after do
+ page.driver.browser.manage.window.resize_to(width_before, height_before)
+ end
+end