Skip to content

feat(ui5-duration-picker): improve keyboard handling support #2095

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

Merged
merged 5 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions packages/base/src/Keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ const isBackSpace = event => (event.key ? event.key === "Backspace" : event.keyC

const isDelete = event => (event.key ? event.key === "Delete" : event.keyCode === KeyCodes.DELETE) && !hasModifierKeys(event);

const isPageUp = event => (event.key ? event.key === "PageUp" : event.keyCode === KeyCodes.PAGE_UP) && !hasModifierKeys(event);

const isPageDown = event => (event.key ? event.key === "PageDown" : event.keyCode === KeyCodes.PAGE_DOWN) && !hasModifierKeys(event);

const isPageUpShift = event => (event.key ? event.key === "PageUp" : event.keyCode === KeyCodes.PAGE_UP) && checkModifierKeys(event, false, false, true);

const isPageDownShift = event => (event.key ? event.key === "PageDown" : event.keyCode === KeyCodes.PAGE_DOWN) && checkModifierKeys(event, false, false, true);

const isPageUpShiftCtrl = event => (event.key ? event.key === "PageUp" : event.keyCode === KeyCodes.PAGE_UP) && checkModifierKeys(event, true, false, true);

const isPageDownShiftCtrl = event => (event.key ? event.key === "PageDown" : event.keyCode === KeyCodes.PAGE_DOWN) && checkModifierKeys(event, true, false, true);

const isShow = event => {
if (event.key) {
return isF4(event) || isShowByArrows(event);
Expand Down Expand Up @@ -167,4 +179,10 @@ export {
isDelete,
isShow,
isF4,
isPageUp,
isPageDown,
isPageUpShift,
isPageDownShift,
isPageUpShiftCtrl,
isPageDownShiftCtrl,
};
51 changes: 50 additions & 1 deletion packages/main/src/DurationPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import { isShow } from "@ui5/webcomponents-base/dist/Keys.js";
import {
isShow,
isPageUp,
isPageDown,
isPageUpShift,
isPageDownShift,
isPageUpShiftCtrl,
isPageDownShiftCtrl,
} from "@ui5/webcomponents-base/dist/Keys.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import DurationPickerTemplate from "./generated/templates/DurationPickerTemplate.lit.js";
Expand Down Expand Up @@ -408,8 +416,49 @@ class DurationPicker extends UI5Element {
if (isShow(event)) {
this.togglePicker();
}

if (isPageUpShiftCtrl(event)) {
event.preventDefault();
this._incrementValue(true, false, false, true);
} else if (isPageUpShift(event)) {
event.preventDefault();
this._incrementValue(true, false, true, false);
} else if (isPageUp(event)) {
event.preventDefault();
this._incrementValue(true, true, false, false);
}

if (isPageDownShiftCtrl(event)) {
event.preventDefault();
this._incrementValue(false, false, false, true);
} else if (isPageDownShift(event)) {
event.preventDefault();
this._incrementValue(false, false, true, false);
} else if (isPageDown(event)) {
event.preventDefault();
this._incrementValue(false, true, false, false);
}
}

_incrementValue(increment, hours, minutes, seconds) {
const values = this.readFormattedValue(this.value);
const incrementStep = increment ? 1 : -1;

if (hours && !this.hideHours) {
values[0] = Number(values[0]) + incrementStep;
} else if (minutes && !this.hideMinutes) {
values[1] = Number(values[1]) + incrementStep;
} else if (seconds && !this.hideSeconds) {
values[2] = Number(values[2]) + incrementStep;
} else {
return;
}

this.value = `${!this.hideHours ? values[0] : ""}${!this.hideHours && !this.hideMinutes ? ":" : ""}${!this.hideMinutes ? values[1] : ""}${!this.hideSeconds ? `:${values[2]}` : ""}`;
this.fireEvent("change", { value: this.value });
}


generateTimeItemsArray(arrayLength, step = 1) {
const resultArray = [];
for (let i = 0; i < arrayLength; i++) {
Expand Down
49 changes: 49 additions & 0 deletions packages/main/test/specs/DurationPicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,53 @@ describe("Duration Picker general interaction", () => {
// close picker
duratationPickerIcon.click();
});

it("Tests Keyboard handling", () => {
const durationPicker = browser.$("#duration-default")

// act
durationPicker.click();
durationPicker.keys(['Shift', 'PageUp']);
durationPicker.keys('Shift');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "00:01:00", "The value of minutes is +1");
// act
durationPicker.click();
durationPicker.keys(['Shift', 'PageDown']);
durationPicker.keys('Shift');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "00:00:00", "The value of minutes is -1");

// act
durationPicker.click();
durationPicker.keys('PageUp');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "01:00:00", "The value of hours is +1");
// act
durationPicker.click();
durationPicker.keys('PageDown');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "00:00:00", "The value of hours is -1");

// act
durationPicker.click();
durationPicker.keys(['Shift', 'Control', 'PageUp']);
durationPicker.keys('Control');
durationPicker.keys('Shift');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "00:00:01", "The value of seconds is +1");
// act
durationPicker.click();
durationPicker.keys(['Shift', 'Control', 'PageDown']);
durationPicker.keys('Shift');
durationPicker.keys('Control');

// assert
assert.strictEqual(durationPicker.shadow$("ui5-input").getProperty("value"), "00:00:00", "The value of seconds is +1");
});
});