Skip to content

Commit 175f56b

Browse files
committed
chore: extend toggle button to support close functionality
1 parent 22e9d39 commit 175f56b

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

core/pfe-core/controllers/combobox-controller.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,6 @@ export class ComboboxController<
368368
const expanded = this.options.isExpanded();
369369
this.#button?.setAttribute('aria-expanded', String(expanded));
370370
this.#input?.setAttribute('aria-expanded', String(expanded));
371-
if (this.#hasTextInput) {
372-
this.#button?.setAttribute('tabindex', '-1');
373-
} else {
374-
this.#button?.removeAttribute('tabindex');
375-
}
376371
this.#initLabels();
377372
}
378373

core/pfe-core/controllers/test/combobox-controller.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ abstract class TestCombobox extends ReactiveElement {
108108
expect(await a11ySnapshot()).axTreeFocusedNode.to.have.axRole('combobox');
109109
});
110110

111-
describe('Tab', function() {
112-
beforeEach(press('Tab'));
113-
beforeEach(updateComplete);
114-
beforeEach(nextFrame);
115-
116-
it('does not focus the toggle button', async function() {
117-
expect(await a11ySnapshot()).to.not.axContainQuery({ focused: true });
118-
});
119-
});
111+
// describe('Tab', function() {
112+
// beforeEach(press('Tab'));
113+
// beforeEach(updateComplete);
114+
// beforeEach(nextFrame);
115+
116+
// it('does not focus the toggle button', async function() {
117+
// expect(await a11ySnapshot()).to.not.axContainQuery({ focused: true });
118+
// });
119+
// });
120120

121121
describe('ArrowDown', function() {
122122
beforeEach(press('ArrowDown'));

elements/pf-search-input/pf-search-input.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
cursor: not-allowed;
178178
}
179179

180-
#close-button {
180+
.close-button {
181181
--pf-c-button--PaddingLeft: var(--pf-global--spacer--sm, 0.5rem);
182182
--pf-c-button--PaddingRight: var(--pf-global--spacer--sm, 0.5rem);
183183
--pf-c-button--PaddingTop: var(--pf-global--spacer--xs, 0.25rem);

elements/pf-search-input/pf-search-input.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ export class PfSearchInput extends LitElement {
8383

8484
@query('#placeholder') private _placeholder?: PfOption;
8585

86-
@query('#outer') private _searchInputContainer!: HTMLElement;
87-
8886
#internals = InternalsController.of(this);
8987

9088
#float = new FloatingDOMController(this, { content: () => this._listboxContainer });
9189

9290
#slots = new SlotController(this, null, 'placeholder');
9391

92+
/** True when the user just clicked the close button */
93+
#clickedCloseButton = false;
94+
9495
#combobox = ComboboxController.of(this, {
9596
getItems: () => this.options,
9697
getFallbackLabel: () => this.accessibleLabel
@@ -100,7 +101,7 @@ export class PfSearchInput extends LitElement {
100101
getListboxElement: () => this._listbox ?? null,
101102
getToggleButton: () => this._toggleButton ?? null,
102103
getComboboxInput: () => this._toggleInput ?? null,
103-
isExpanded: () => this.expanded,
104+
isExpanded: () => this.#setIsExpanded(),
104105
requestShowListbox: () => this.#showListbox(),
105106
requestHideListbox: () => void (this.expanded &&= false),
106107
setItemHidden: (item, hidden) => (item.id !== 'placeholder') && void (item.hidden = hidden),
@@ -149,17 +150,17 @@ export class PfSearchInput extends LitElement {
149150
@keyup="${this.#onSubmit}"
150151
@keydown="${this.#onKeyDown}">
151152
<div class="close-button-container">
152-
<pf-button id="close-button"
153+
<pf-button id="toggle-button"
154+
class="close-button"
153155
plain
154156
label="Close"
155157
?hidden="${this.#hideCloseButton()}"
156-
@click="${this.#onClose}">
158+
@click="${this.#onClickCloseButton}">
157159
<pf-icon size="md"
158160
icon="close"
159161
set="patternfly">close</pf-icon>
160162
</pf-button>
161163
</div>
162-
<button type="button" aria-label="toggle button" inert class="visually-hidden" id="toggle-button"></button>
163164
</div>
164165
<div id="listbox-container"
165166
?hidden="${!expanded}"
@@ -245,13 +246,9 @@ export class PfSearchInput extends LitElement {
245246
}
246247
}
247248

248-
async #onClose() {
249-
if (this.expanded) {
250-
await this.hide();
251-
}
252-
this.value = '';
253-
this._toggleInput!.value = this.value;
254-
this.#combobox.selected = [];
249+
async #onClickCloseButton() {
250+
this._toggleInput!.value = '';
251+
this.#clickedCloseButton = true;
255252
this._toggleInput?.focus();
256253
}
257254

@@ -310,6 +307,15 @@ export class PfSearchInput extends LitElement {
310307
item?.scrollIntoView({ behavior: 'auto', block: 'nearest', inline: 'nearest' });
311308
}
312309
}
310+
311+
#setIsExpanded() {
312+
if (this.#clickedCloseButton) {
313+
this.#clickedCloseButton = false;
314+
// prevent the listbox from showing when we only intend to clear the input
315+
return true;
316+
}
317+
return this.expanded;
318+
}
313319
}
314320

315321
declare global {

elements/pf-select/pf-select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { PfChipGroup } from '../pf-chip/pf-chip-group.js';
22
import type { Placement } from '@patternfly/pfe-core/controllers/floating-dom-controller.js';
33
import type { TemplateResult } from 'lit';
44

5-
import { LitElement, html, isServer } from 'lit';
5+
import { LitElement, html, isServer, nothing } from 'lit';
66
import { customElement } from 'lit/decorators/custom-element.js';
77
import { property } from 'lit/decorators/property.js';
88
import { query } from 'lit/decorators/query.js';
@@ -368,7 +368,7 @@ export class PfSelect extends LitElement {
368368
?hidden="${!typeahead}"
369369
?disabled="${disabled}"
370370
placeholder="${placeholder || this.#buttonLabel}">`}
371-
<button id="toggle-button">
371+
<button tabindex=${typeahead ? '-1' : nothing} id="toggle-button">
372372
<span id="button-text" style="display: contents;">
373373
<span id="toggle-text"
374374
class="${classMap({ 'visually-hidden': !!typeahead, badge })}">${this.#buttonLabel}</span>${!hasBadge ? '' : html`

0 commit comments

Comments
 (0)