Skip to content

Commit f33ba4a

Browse files
authored
Merge pull request #1796 from patternfly/autosomepleat
feat: adds custom event for clear search event, adds an aria label for search button (when applicable), and resolves dupe "x" with input[type="search"]
2 parents 56aeff5 + a8d93b7 commit f33ba4a

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

elements/pfe-autocomplete/demo/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<link href="../../pfelement/dist/pfelement.min.css" rel="stylesheet">
2626

2727
<!-- uncomment the es5-adapter if you're using the umd version -->
28-
<script src="https://unpkg.com/@webcomponents/[email protected]/custom-elements-es5-adapter.js"></script>
28+
<!-- <script src="https://unpkg.com/@webcomponents/[email protected]/custom-elements-es5-adapter.js"></script>
2929
<script src="https://unpkg.com/@webcomponents/[email protected]/webcomponents-bundle.js"></script>
3030
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
3131
<script>
@@ -34,6 +34,10 @@
3434
'../../pfe-band/dist/pfe-band.umd.js'
3535
])
3636
37+
</script> -->
38+
<script type="module">
39+
import "../dist/pfe-autocomplete.js";
40+
import "../../pfe-band/dist/pfe-band.js";
3741
</script>
3842
</head>
3943

elements/pfe-autocomplete/docs/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ detail: {
145145
optionValue: String
146146
}
147147
```
148+
149+
### pfe-autocomplete:option-cleared
150+
Fires when a user clears the input field using the clear button.
151+
152+
```javascript
153+
detail: {
154+
searchValue: ""
155+
}
156+
```
148157
:::
149158

150159
::: section

elements/pfe-autocomplete/src/pfe-autocomplete.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@
4242

4343
<!-- Search button (when [button-text] attr provided) -->
4444
<pfe-button class="search-button--textual" hidden>
45-
<button class="search-button__text" disabled></button>
45+
<button class="search-button__text" aria-label="Search" disabled></button>
4646
</pfe-button>

elements/pfe-autocomplete/src/pfe-autocomplete.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class PfeAutocomplete extends PFElement {
6969
search: `${this.tag}:search-event`,
7070
select: `${this.tag}:option-selected`,
7171
optionsShown: `${this.tag}:options-shown`,
72+
optionCleared: `${this.tag}:option-cleared`,
7273
slotchange: `slotchange`,
7374
};
7475
}
@@ -146,6 +147,11 @@ class PfeAutocomplete extends PFElement {
146147
this._input.setAttribute("autocorrect", "off");
147148
this._input.setAttribute("autocapitalize", "off");
148149
this._input.setAttribute("spellcheck", "false");
150+
151+
this._input.setAttribute(
152+
"style",
153+
`input[type=search]::-ms-clear { display: none; width : 0; height: 0; }input[type = search]:: -ms - reveal { display: none; width: 0; height: 0; }" nput[type="search"]::-webkit-search-decoration, input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-results-button, input[type="search"]::-webkit-search-results-decoration { display: none; }`
154+
);
149155
}
150156

151157
disconnectedCallback() {
@@ -254,6 +260,11 @@ class PfeAutocomplete extends PFElement {
254260
this._searchBtn.setAttribute("disabled", "");
255261
this._searchBtnTextual.setAttribute("disabled", "");
256262
this._input.focus();
263+
this.emitEvent(PfeAutocomplete.events.optionCleared, {
264+
bubbles: true,
265+
composed: true,
266+
detail: { searchValue: "" },
267+
});
257268
}
258269

259270
_search() {
@@ -412,6 +423,7 @@ class PfeAutocomplete extends PFElement {
412423
* pfe-autocomplete:option-selected | Fires when an option is selected.
413424
event.details.optionValue contains the selected value.
414425
*/
426+
415427
class PfeSearchDroplist extends PFElement {
416428
static get tag() {
417429
return "pfe-search-droplist";

elements/pfe-autocomplete/src/pfe-autocomplete.scss

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ $LOCAL-VARIABLES: (
2828
}
2929

3030
button.clear-search {
31-
right: 10px;
31+
width: 40px;
32+
right: 0px;
3233
}
3334
}
3435

@@ -50,7 +51,7 @@ $LOCAL-VARIABLES: (
5051
flex: 1;
5152
box-shadow: pfe-local(BoxShadow) !important;
5253
padding-left: 10px;
53-
padding-right: 30px;
54+
padding-right: 0;
5455
border-radius: pfe-var(ui--border-radius);
5556
background-color: pfe-local(BackgroundColor);
5657
border: pfe-local(Border);
@@ -113,7 +114,9 @@ button {
113114
color: pfe-var(ui-base);
114115
}
115116
svg {
116-
width: 12px;
117+
width: 14px;
118+
position: relative;
119+
top: 2px;
117120
stroke: pfe-var(surface--border);
118121
}
119122
&:hover svg,
@@ -129,12 +132,18 @@ button {
129132
}
130133

131134
&.search-button {
135+
margin-top: 1px;
136+
margin-bottom: 1px;
132137
right: 1px;
133138
width: 30px;
134-
139+
background-color: pfe-var(surface--lightest);
135140
svg {
136141
fill: pfe-var(link);
137-
width: 16px;
142+
// width: 16px;
143+
width: 18px;
144+
position: relative;
145+
top: 2px;
146+
stroke: pfe-var(surface--border);
138147
}
139148

140149
&:hover svg,

elements/pfe-autocomplete/test/pfe-autocomplete_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ describe('<pfe-autocomplete>', () => {
177177
});
178178
});
179179

180+
it('should fire a pfe-autocomplete:option-cleared event when the input is cleared', done => {
181+
flush(() => {
182+
const items = ['option 1', 'option 2'];
183+
184+
autocompleteElem.autocompleteRequest = function (params, callback) {
185+
const regx = new RegExp("\^" + params.query, "i");
186+
callback(items.filter(function (item) {
187+
return regx.test(item);
188+
}));
189+
};
190+
191+
autocompleteElem.addEventListener("pfe-autocomplete:option-cleared", function (event) {
192+
assert.isTrue(true); // the event listener was called
193+
done();
194+
});
195+
autocompleteElem._clear();
196+
});
197+
});
198+
180199
it('should set selected-value attribute after user click on an option', done => {
181200
flush(() => {
182201
droplistElem.data = ['option 1', 'option 2'];

0 commit comments

Comments
 (0)