Skip to content

Commit 3f738c9

Browse files
starryeyez024kylebuch8
authored andcommitted
pfe-autocomplete (#249)
* initial rh-autocomplete commit * rh-autocomplete first commit * added debounce value as an attribute * improved accessibility * added rh-autocomplete to demo file * added comment * added test * rh-autocomplete added theming * added search icon * removed console.log * added test * fixed a mistake * added default value and observer on init-value attr * added disabled fill collor on search svg icon * added test... * added loading attribute * added scroll to droplist * update test - moved from BDD style to TDD * added init value of false for loading attr * updated demo file * code refactoring... * added compiled file from prev commit... * fixed dropdown heigh * added disabled attr * style fix ... * editted demo page * fixed a bug... * added selected value by key up/dowm to input box * Revert "added selected value by key up/dowm to input box" This reverts commit e332b36. * refactored code to be able to use it with react * removed selection on first item when drop-down window is open * centered close button * added selected value to input box when user click on an option * refactoring... * fixed failing test * fixed a bug - when init-value is changed clear and search button were not in right state * changed opacity of input and button to 0.5 when ther are disabled * renamed rh-autocomplete to pfe-autocomplete * changed rh elements to pfe elements * renamed repo scope name from @PFElements to @patternfly * moved input to light dom * updated demo file * renamed test files * moveed the element inside text-fixture * update read.me file * added iron-test-helpers lib and added more test * removed old logo from read.me file * fixed the element name in package.json * updating demo page * fixing issues with key inputs If the value is empty, reset things. If keyup or keydown and the dropdown list is not open, return. * removing pagination test
1 parent 85bcaae commit 3f738c9

38 files changed

+2281
-65
lines changed

doc/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
<title>PFElements</title>
7+
</head>
8+
<body>
9+
10+
<h1>PFElements demos</h1>
11+
12+
<ul>
13+
<li><a href="../elements/pfe-accordion/demo">pfe-accordion</a></li>
14+
<li><a href="../elements/pfe-card/demo">pfe-card</a></li>
15+
<li><a href="../elements/pfe-cta/demo">pfe-cta</a></li>
16+
<li><a href="../elements/pfe-datetime/demo">pfe-datetime</a></li>
17+
<li><a href="../elements/pfe-health-index/demo">pfe-health-index</a></li>
18+
<li><a href="../elements/pfe-icon-panel/demo">pfe-icon-panel</a></li>
19+
<li><a href="../elements/pfe-icon/demo">pfe-icon</a></li>
20+
<li><a href="../elements/pfe-number/demo">pfe-number</a></li>
21+
<li><a href="../elements/pfe-tabs/demo">pfe-tabs</a></li>
22+
<li><a href="../elements/pfe-autocomplete/demo">pfe-autocomplete</a></li>
23+
</ul>
24+
25+
</body>
26+
</html>

elements/pfe-accordion/pfe-accordion.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

elements/pfe-autocomplete/.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": [["env", { "modules": false }]],
3+
"plugins": ["external-helpers"]
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# EditorConfig: http://EditorConfig.org
2+
3+
# Top-most EditorConfig file
4+
root = true
5+
6+
# Rules for JavaScript files:
7+
8+
[*.{js,py,json,sh,html}]
9+
# 4 space indentation
10+
indent_style = space
11+
indent_size = 2
12+
# No trailing spaces
13+
trim_trailing_whitespace = true
14+
# Unix-style newlines
15+
end_of_line = lf
16+
# Newline ending every file
17+
insert_final_newline = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
addons:
5+
firefox: "latest"
6+
apt:
7+
sources:
8+
- google-chrome
9+
packages:
10+
- google-chrome-stable
11+
node_js: stable
12+
before_install:
13+
- npm install -g web-component-tester
14+
install:
15+
- npm install
16+
before_script:
17+
script:
18+
- xvfb-run npm run test
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright 2018 Red Hat, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# PFElements Autocomplete Element
2+
3+
<pfe-autocomplete> is a Web Component that provides options in a dropdown list as user types in an input box by showing result from an api call.
4+
5+
## Install
6+
```
7+
npm install @patternfly/cp-theme
8+
npm install @patternfly/pfe-autocomplete
9+
```
10+
11+
cp-theme is a package that contains css variables setting that is used for theming patternfly elements.
12+
13+
Once installed, import it to your application:
14+
```
15+
import '@patternfly/cp-theme.umd.js';
16+
import '@patternfly/pfe-autocomplete.umd.js';
17+
```
18+
19+
## Usage
20+
```
21+
<pfe-autocomplete debounce="500" init-value="uni">
22+
<input placeholder="Enter Your Search Term"/>
23+
</pfe-autocomplete>
24+
```
25+
26+
## Setup
27+
28+
**`autocompleteRequest`**
29+
`autocompleteRequest` is a property that is assigned a function. When user types, component calls this function.
30+
31+
It is called inside component but we define it outside component. First param is the typed phrase by user and second param is a callback function to send api response back to the component.
32+
33+
In the function, we add loading attribute then send api call. When result is ready, we remove loading attribute and pass the result to web component by calling callback function. Here is an example:
34+
35+
```
36+
// autocomplete call
37+
searchAutocomplete.autocompleteRequest = function(params, callback) {
38+
var xhr = new XMLHttpRequest();
39+
40+
searchAutocomplete.setAttribute('loading', '');
41+
xhr.onload = function() {
42+
searchAutocomplete.removeAttribute('loading');
43+
44+
if(xhr.status === 404) {
45+
callback([]);
46+
} else {
47+
const response = JSON.parse(xhr.responseText);
48+
const regx = new RegExp("\^" + params.query, "i");
49+
50+
var responseReady = response.reduce(function(acc, item) {
51+
if (regx.test(item.name)) acc.push(item.name);
52+
return acc;
53+
}, []);
54+
callback(responseReady);
55+
}
56+
};
57+
58+
const url = 'https://restcountries.eu/rest/v2/name/' + params.query;
59+
60+
xhr.open('GET', url, true);
61+
xhr.send();
62+
};
63+
```
64+
65+
**`debounce`**
66+
This attribute is optional. By default, It has been set tp 300ms. User may type very fast. We allow to input box value changes trigger autocomplete api call each 300ms.
67+
68+
**`loading`**
69+
loading is a boolean attribute. If you add this attribute on element a loading icon is added in input box. Add this attribute before autocomplete api call and remove this attribute form element when api call response is ready.
70+
71+
**`init-value`**
72+
Set this attribute when you want to set a value in input box when web component is added to page.
73+
74+
**`is-disabled`**
75+
is-disabled is a boolean attribute. Add this attribute to element when you want to make the element disabled. By adding this attribute input box and buttons become disabled.
76+
77+
## Get selected item
78+
User can select an item by clicking on search button, type press enter or select an item by using keyboard and press enter. The selected item can be captured by listening to an event(`pfe-search-event`) or observing attribute(`selected-value`) change.
79+
80+
### pfe-search-event
81+
When user performs search, `pfe-search-event` event is fired. By listening to this event you can get selected phrase by getting `e.detail.searchValue`.
82+
83+
```
84+
searchAutocomplete.addEventListener('pfe-search-event', function(e) {
85+
console.log('do search= ' + e.detail.searchValue);
86+
});
87+
```
88+
89+
### selected-value attribute
90+
By observing `selected-value` attribute you can detect autocomplete selected value.
91+
92+
## Dependencies
93+
94+
Make sure you have [Polyserve][polyserve] and [Web Component Tester][web-component-tester] installed.
95+
96+
npm install -g polyserve web-component-tester
97+
98+
## Dev
99+
100+
npm start
101+
102+
## Test
103+
104+
npm run test
105+
106+
## Build
107+
108+
npm run build
109+
110+
## Demo
111+
112+
Run `npm start` and Polyserve will start a server and open your default browser to the demo page of the element.
113+
114+
## Code style
115+
116+
Autocomplete (and all PFElements) use [Prettier][prettier] to auto-format JS and JSON. The style rules get applied when you commit a change. If you choose to, you can [integrate your editor][prettier-ed] with Prettier to have the style rules applied on every save.
117+
118+
[prettier]: https://github.com/prettier/prettier/
119+
[prettier-ed]: https://github.com/prettier/prettier/#editor-integration
120+
[polyserve]: https://github.com/Polymer/polyserve
121+
[web-component-tester]: https://github.com/Polymer/web-component-tester
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>PFElements: pfe-autocomplete Demo</title>
6+
7+
<noscript>
8+
<link href="../../pfelement/pfelement-noscript.min.css" rel="stylesheet">
9+
</noscript>
10+
11+
<link href="../../pfelement/pfelement.min.css" rel="stylesheet">
12+
13+
<!-- uncomment the es5-adapter if you're using the umd version -->
14+
<script src="/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
15+
<script src="/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
17+
<script>require([
18+
'../../../themes/cp-theme/cp-theme.umd.js',
19+
'../pfe-autocomplete.umd.js'
20+
])</script>
21+
</head>
22+
<body unresolved>
23+
<h1>&lt;pfe-autocomplete&gt;</h1>
24+
<h2>Static Data</h2>
25+
<p>Uses an array of strings.</p>
26+
<div>
27+
<pfe-autocomplete id="static" debounce="300">
28+
<input placeholder="Enter Your Search Term"/>
29+
</pfe-autocomplete>
30+
</div>
31+
<h4>Static Selected Value: <span id="staticSelectedValue"></span></h4>
32+
33+
<h2>Lazy Loading with init-value</h2>
34+
<p>Makes an ajax call, filters the response and sends the filtered data to the component.</p>
35+
<div>
36+
<pfe-autocomplete id="search-lazy-loading" debounce="500" init-value="uni">
37+
<input placeholder="Enter Your Search Term"/>
38+
</pfe-autocomplete>
39+
</div>
40+
<h4>Ajax Selected Value: <span id="ajaxSelectedValue"></span></h4>
41+
42+
<h2>Disabled</h2>
43+
<div>
44+
<pfe-autocomplete is-disabled init-value="uni"><input placeholder="Enter Your Search Term"/></pfe-autocomplete>
45+
</div>
46+
<script>
47+
const staticAutocomplete = document.querySelector("#static");
48+
const items = [
49+
"Item 1",
50+
"Item 2",
51+
"United States",
52+
"Chicago Cubs",
53+
"Red Hat",
54+
"Purple",
55+
"Curious George",
56+
"United Kingdom",
57+
"Elephant",
58+
"Baseball",
59+
"Bingo",
60+
"Book",
61+
"Android",
62+
"iOS",
63+
"Linux",
64+
"Red Hat Enterprise Linux"
65+
];
66+
67+
staticAutocomplete.autocompleteRequest = function(params, callback) {
68+
const regx = new RegExp("\^" + params.query, "i");
69+
callback(items.filter(item => regx.test(item)));
70+
};
71+
72+
staticAutocomplete.addEventListener('pfe-search-event', function(e) {
73+
document.querySelector("#staticSelectedValue").textContent = e.detail.searchValue;
74+
});
75+
76+
const searchAutocomplete = document.querySelector('#search-lazy-loading');
77+
78+
// autocomplete call
79+
searchAutocomplete.autocompleteRequest = function(params, callback) {
80+
var xhr = new XMLHttpRequest();
81+
82+
searchAutocomplete.setAttribute('loading', '');
83+
xhr.onload = function() {
84+
searchAutocomplete.removeAttribute('loading');
85+
86+
if(xhr.status === 404) {
87+
callback([]);
88+
} else {
89+
const response = JSON.parse(xhr.responseText);
90+
const regx = new RegExp("\^" + params.query, "i");
91+
92+
var responseReady = response.reduce(function(acc, item) {
93+
if (regx.test(item.name)) acc.push(item.name);
94+
return acc;
95+
}, []);
96+
callback(responseReady);
97+
}
98+
};
99+
100+
const url = 'https://restcountries.eu/rest/v2/name/' + params.query;
101+
102+
xhr.open('GET', url, true);
103+
xhr.send();
104+
};
105+
106+
// when user performs search, this event is fired
107+
// search phrase is passed to e.detail.searchValue
108+
searchAutocomplete.addEventListener('pfe-search-event', function(e) {
109+
document.querySelector("#ajaxSelectedValue").textContent = e.detail.searchValue;
110+
});
111+
</script>
112+
</body>
113+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rollup.config.js
2+
const gulpFactory = require("../../scripts/gulpfile.factory.js");
3+
const pfelementPackage = require("./package.json");
4+
5+
gulpFactory(pfelementPackage.pfelement);

0 commit comments

Comments
 (0)