Skip to content
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

fix(ui5-input): improve lazy loading behaviour #4763

Merged
merged 6 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/base/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ydKuAqEtZBlQWCoH/ufRUwgv+qU=
XgquM3ggzotOfecoo9jjwDTSvdQ=
46 changes: 32 additions & 14 deletions packages/main/src/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ const metadata = {
type: Boolean,
},

openOnMobile: {
type: Boolean,
},

open: {
type: Boolean,
},

/**
* Indicates whether the visual focus is on the value state header
* @private
Expand All @@ -374,10 +382,6 @@ const metadata = {
type: Boolean,
},

open: {
type: Boolean,
},

_input: {
type: Object,
},
Expand Down Expand Up @@ -586,6 +590,9 @@ class Input extends UI5Element {
// Indicates, if the user pressed the BACKSPACE key.
this._backspaceKeyDown = false;

// Indicates, if the user is typing. Gets reset once popup is closed
this.isTyping = false;

// all sementic events
this.EVENT_CHANGE = "change";
this.EVENT_INPUT = "input";
Expand Down Expand Up @@ -616,9 +623,17 @@ class Input extends UI5Element {
}

this.effectiveShowClearIcon = (this.showClearIcon && !!this.value && !this.readonly && !this.disabled);
this.open = this.open && (!!this.suggestionItems.length || this._isPhone);

const FormSupport = getFeature("FormSupport");
const hasItems = this.suggestionItems.length;
const hasValue = !!this.value;
const isFocused = this === document.activeElement;

if (this._isPhone) {
this.open = this.openOnMobile;
} else {
this.open = hasValue && hasItems && isFocused && this.isTyping;
}

if (FormSupport) {
FormSupport.syncNativeHiddenInput(this);
Expand Down Expand Up @@ -794,8 +809,6 @@ class Input extends UI5Element {
this._isValueStateFocused = false;
this.focused = true;
}

this.open = false;
}

async _onfocusin(event) {
Expand Down Expand Up @@ -827,13 +840,13 @@ class Input extends UI5Element {
return;
}

this.closePopover();
this.open = false;
this._clearPopoverFocusAndSelection();

this.previousValue = "";
this.lastConfirmedValue = "";
this.focused = false; // invalidating property
this.open = false;
this.isTyping = false;
}

_clearPopoverFocusAndSelection() {
Expand All @@ -851,7 +864,7 @@ class Input extends UI5Element {
_click(event) {
if (isPhone() && !this.readonly && this.Suggestions) {
this.blur();
this.open = true;
this.openOnMobile = true;
}
}

Expand Down Expand Up @@ -956,11 +969,9 @@ class Input extends UI5Element {

if (this.Suggestions) {
this.Suggestions.updateSelectedItemPosition(null);

if (!this._isPhone) {
this.open = !!inputDomRef.value;
}
}

this.isTyping = true;
}

_handleResize() {
Expand All @@ -986,6 +997,10 @@ class Input extends UI5Element {
this.blur();
this.focused = false;
}

this.isTyping = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should add the same thing (this.isTyping = false;) in the _handleChange().
This will prevent the popup from opening when the user has typed something and pressed the "Enter" key before the data is received. Should we consider pressing enter the end of the user interaction in such cases?

this.openOnMobile = false;
this.open = false;
}

/**
Expand Down Expand Up @@ -1053,6 +1068,9 @@ class Input extends UI5Element {
this.suggestionSelectionCanceled = false;

this.fireEvent(this.EVENT_SUGGESTION_ITEM_SELECT, { item });

this.isTyping = false;
this.openOnMobile = false;
}

previewSuggestion(item) {
Expand Down
207 changes: 207 additions & 0 deletions packages/main/test/pages/InputsLazyLoading.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Input Lazy Loading</title>

<script src="../../webcomponentsjs/webcomponents-loader.js"></script>
<script src="../../resources/bundle.esm.js" type="module"></script>
<script nomodule src="../../resources/bundle.es5.js"></script>

<style>
.demo-container {
margin-top: 1rem;
border: 1px dashed red;
padding: 2rem;
box-sizing: border-box;
text-align: center;
}

.demo-container:first-child {
margin-top: 0;
}

.demo-container h1 {
font-size: 1.5rem;
font-weight: normal;
}

.devider {
border: 1px solid black;
margin: 1rem;
}
</style>
</head>

<body>

<div class="demo-container">
<h1>Lazy loading items oninput</h1>

<ui5-input id="field" show-suggestions></ui5-input>
</div>

<div class="demo-container">
<h1>Lazy load items on first type in</h1>

<ui5-input id="first-type" show-suggestions></ui5-input>
</div>

<div class="demo-container">
<h1>Preload items</h1>

<ui5-input id="preload" show-suggestions></ui5-input>
</div>

<div class="demo-container">
<h1>Hardcoded items</h1>

<ui5-input show-suggestions>
<ui5-suggestion-item>Dryanovo</ui5-suggestion-item>
<ui5-suggestion-item>Gabrovo</ui5-suggestion-item>
<ui5-suggestion-item>Sofia</ui5-suggestion-item>
<ui5-suggestion-item>Yablanitsa</ui5-suggestion-item>
<ui5-suggestion-item>New York</ui5-suggestion-item>
</ui5-input>
</div>

<div class="devider"></div>

<div class="demo-container">
<h1>Lazy loading items oninput</h1>

<ui5-combobox id="combo-field" show-suggestions></ui5-combobox>
</div>

<div class="demo-container">
<h1>Lazy load items on first type in</h1>

<ui5-combobox id="combo-first-type" show-suggestions></ui5-combobox>
</div>

<div class="demo-container">
<h1>Preload items</h1>

<ui5-combobox id="combo-preload" show-suggestions></ui5-combobox>
</div>

<div class="demo-container">
<h1>Hardcoded items</h1>

<ui5-combobox show-suggestions>
<ui5-cb-item text="Dryanovo"></ui5-cb-item>
<ui5-cb-item text="Gabrovo"></ui5-cb-item>
<ui5-cb-item text="Sofia"></ui5-cb-item>
<ui5-cb-item text="Yablanitsa"></ui5-cb-item>
<ui5-cb-item text="New York"></ui5-cb-item>
</ui5-combobox>
</div>


<script>
const entries = [{ key: "A", text: "A" }, { key: "Afg", text: "Afghanistan" }, { key: "Arg", text: "Argentina" }, { key: "Alb", text: "Albania" }, { key: "Arm", text: "Armenia" }, { key: "Alg", text: "Algeria" }, { key: "And", text: "Andorra" }, { key: "Ang", text: "Angola" }, { key: "Ast", text: "Austria" }, { key: "Aus", text: "Australia" }, { key: "Aze", text: "Azerbaijan" }, { key: "Aruba", text: "Aruba" }, { key: "Antigua", text: "Antigua and Barbuda" }, { key: "B", text: "B" }, { key: "Bel", text: "Belarus" }, { key: "Bel", text: "Belgium" }, { key: "Bg", text: "Bulgaria" }, { key: "Bra", text: "Brazil" }, { key: "C", text: "C" }, { key: "Ch", text: "China" }, { key: "Cub", text: "Cuba" }, { key: "Chil", text: "Chili" }, { key: "L", text: "L" }, { key: "Lat", text: "Latvia" }, { key: "Lit", text: "Litva" }, { key: "P", text: "P" }, { key: "Prt", text: "Portugal" }, { key: "S", text: "S" }, { key: "Sen", text: "Senegal" }, { key: "Ser", text: "Serbia" }, { key: "Sey", text: "Seychelles" }, { key: "Sierra", text: "Sierra Leone" }, { key: "Sgp", text: "Singapore" }, { key: "Sint", text: "Sint Maarten" }, { key: "Slv", text: "Slovakia" }, { key: "Slo", text: "Slovenia" }];


const fetchData = async () => {
// load data real API
// return (await (await fetch("https://restcountries.com/v3.1/all")).json());

// load data fake request
await new Promise(e => setTimeout(e, 1000));

return entries;
};

const clearChildren = element => {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
};

const fillItems = (itemsData, inputElement, tagName) => {
itemsData.forEach(data => {
const element = document.createElement(tagName);
element.setAttribute("text", data.text);

inputElement.appendChild(element);
});
};

const filterData = (data, value) => {
return data.filter(entry => {
return entry.text.toLowerCase().startsWith(value.toLowerCase())
});
};

const enableLazyLoadingOnInput = () => {
document.getElementById("field").addEventListener("ui5-input", async event => {
if (event.target.value === "") {
return clearChildren(event.target);
}

// load data
const data = await fetchData();

// filter data
const filteredData = filterData(data, event.target.value);

// cleanup old items
clearChildren(event.target);

// fill new items (posibly redux / react state)
fillItems(filteredData, event.target, "ui5-suggestion-item");
});
}

const enableFirsTypein = () => {
const field = document.getElementById("first-type");

field.addEventListener("ui5-input", async event => {
const { value, suggestionItems } = event.target;

if (value.length === 1 && !suggestionItems.length) {
const data = await fetchData();

// fill new items (posibly redux / react state)
fillItems(data, event.target, "ui5-suggestion-item");

} else if (!value.length && suggestionItems.length) {
clearChildren(event.target);
}
});
};

const enablePreload = async () => {
const data = await fetchData();

fillItems(data, document.getElementById("preload"), "ui5-suggestion-item");
};

enableLazyLoadingOnInput();
enableFirsTypein();
enablePreload();

const enableCbLazyOnInput = () => {
const cb = document.getElementById("combo-field");

cb.addEventListener("ui5-input", async event => {
const { value } = event.target;

const data = await fetchData();

// cleanup old items
clearChildren(event.target);

// fill new items (posibly redux / react state)
fillItems(data, event.target, "ui5-cb-item");
});
};

enableCbLazyOnInput();
</script>
</body>

</html>
42 changes: 42 additions & 0 deletions packages/main/test/specs/Input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,3 +1118,45 @@ describe("XSS tests for suggestions", () => {
}));
});
});


describe("Lazy loading", () => {
beforeEach(async () => {
await browser.url(`http://localhost:${PORT}/test-resources/pages/InputsLazyLoading.html`);
});

it("Lazy loading opens the picker once items are populated", async () => {
const input = await $("#field");
const inner = await input.shadow$("input");
const staticAreaClassName = await browser.getStaticAreaItemClassName("#field");
const respPopover = await $(`.${staticAreaClassName}`).shadow$("ui5-responsive-popover");

await inner.click();
await inner.keys("a");

await browser.waitUntil(() => respPopover.getProperty("opened"), {
timeout: 3000,
timeoutMsg: "Popover should be displayed"
});
});

it("Does not reopeon picker on focus in", async () => {
const input = await $("#field");
const inner = await input.shadow$("input");
const staticAreaClassName = await browser.getStaticAreaItemClassName("#field");
const respPopover = await $(`.${staticAreaClassName}`).shadow$("ui5-responsive-popover");

await inner.click();
await inner.keys("a");

// go to next focusable
await browser.keys(["Shift", "Tab"]);

// go to previous
await browser.keys("Tab");

await browser.pause(3000);

assert.notOk(await respPopover.getProperty("opened"), "Picker should not be open");
});
});