Skip to content

Commit 4e69215

Browse files
committed
chore: code cleanup
1 parent 36875aa commit 4e69215

File tree

3 files changed

+38
-125
lines changed

3 files changed

+38
-125
lines changed

elements/pf-search-input/demo/pf-search-input-autocomplete.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<pf-search-input variant="typeahead"
2-
placeholder="Select a state">
2+
placeholder="Search">
33
<pf-search-input-option value="Alabama"> Alabama </pf-search-input-option>
44
<pf-search-input-option value="New Jersey"> New Jersey </pf-search-input-option>
55
<pf-search-input-option value="New York"> New York </pf-search-input-option>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
#toggle-input {
266266
justify-content: space-between;
267267
padding: var(--pf-global--spacer--xs, 0.25rem) var(--pf-global--spacer--sm, 0.5rem);
268+
padding-left: 40px;
268269
}
269270

270271
.disabled #toggle-input {
@@ -377,3 +378,12 @@ pf-badge {
377378
background-color: var(--pf-c-divider--after--BackgroundColor);
378379
flex: 1 0 100%;
379380
}
381+
382+
div.search-icon{
383+
position: absolute;
384+
top: 50%;
385+
left: 0;
386+
transform: translateY(-50%);
387+
display: flex;
388+
align-items: center;
389+
}

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

Lines changed: 27 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { LitElement, html, isServer } from 'lit';
55
import { customElement } from 'lit/decorators/custom-element.js';
66
import { property } from 'lit/decorators/property.js';
77
import { query } from 'lit/decorators/query.js';
8-
import { repeat } from 'lit/directives/repeat.js';
98
import { styleMap } from 'lit/directives/style-map.js';
109
import { classMap } from 'lit/directives/class-map.js';
11-
import { ifDefined } from 'lit/directives/if-defined.js';
1210

1311
import { ComboboxController } from '@patternfly/pfe-core/controllers/combobox-controller.js';
1412
import { SlotController } from '@patternfly/pfe-core/controllers/slot-controller.js';
@@ -20,6 +18,7 @@ import { observes } from '@patternfly/pfe-core/decorators/observes.js';
2018

2119
import { PfSearchInputOption } from './pf-search-input-option.js';
2220
import styles from './pf-search-input.css';
21+
import type { PfButton } from '../pf-button/pf-button.js';
2322

2423
export class PfSelectChangeEvent extends Event {
2524
constructor() {
@@ -42,16 +41,10 @@ export class PfSearchInput extends LitElement {
4241
delegatesFocus: true,
4342
};
4443

45-
/** Variant of rendered Select */
46-
@property() variant: 'single' | 'checkbox' | 'typeahead' | 'typeaheadmulti' = 'single';
4744

4845
/** Accessible label for the select */
4946
@property({ attribute: 'accessible-label' }) accessibleLabel?: string;
5047

51-
/** Accessible label for chip group used to describe chips */
52-
@property({
53-
attribute: 'accessible-current-selections-label',
54-
}) accessibleCurrentSelectionsLabel = 'Current selections';
5548

5649
/** Multi listbox button text */
5750
@property({ attribute: 'items-selected-text' }) itemsSelectedText = 'items selected';
@@ -80,17 +73,10 @@ export class PfSearchInput extends LitElement {
8073
*/
8174
@property({ reflect: true }) position: Placement = 'bottom';
8275

83-
/** Flag indicating if selection badge should be hidden for checkbox variant,default false */
84-
@property({
85-
attribute: 'checkbox-selection-badge-hidden',
86-
type: Boolean,
87-
}) checkboxSelectionBadgeHidden = false;
88-
89-
//@query('pf-chip-group') private _chipGroup?: PfChipGroup;
9076

9177
@query('#toggle-input') private _toggleInput?: HTMLInputElement;
9278

93-
@query('#toggle-button') private _toggleButton?: HTMLButtonElement;
79+
@query('#toggle-button') private _toggleButton?: PfButton;
9480

9581
@query('#listbox') private _listbox?: HTMLElement;
9682

@@ -107,7 +93,6 @@ export class PfSearchInput extends LitElement {
10793
#slots = new SlotController(this, null, 'placeholder');
10894

10995
#combobox = ComboboxController.of(this, {
110-
multi: this.variant === 'typeaheadmulti' || this.variant === 'checkbox',
11196
getItems: () => this.options,
11297
getFallbackLabel: () => this.accessibleLabel
11398
|| this.#internals.computedLabelText
@@ -151,75 +136,42 @@ export class PfSearchInput extends LitElement {
151136
}
152137
}
153138

154-
/** Whether select has badge for number of selected items */
155-
get #hasBadge() {
156-
// NOTE: revisit this in v5
157-
return this.variant === 'checkbox' && !this.checkboxSelectionBadgeHidden;
158-
}
159-
160139
get #buttonLabel(): string {
161140
const { selected } = this.#combobox;
162-
switch (this.variant) {
163-
case 'typeaheadmulti':
164-
return `${selected?.length ?? 0} ${this.itemsSelectedText}`;
165-
case 'checkbox':
166-
return this.#computePlaceholderText()
167-
|| 'Options';
168-
default:
169-
return (selected ? this.value : '')
170-
|| this.#computePlaceholderText()
171-
|| 'Select a value';
172-
}
141+
return `${selected?.length ?? 0} ${this.itemsSelectedText}`;
173142
}
174143

175144
override render(): TemplateResult<1> {
176-
const { disabled, expanded, variant, placeholder } = this;
145+
const { disabled, expanded, placeholder } = this;
177146
const { anchor = 'bottom', alignment = 'start', styles = {} } = this.#float;
178147
const { height, width } = this.getBoundingClientRect?.() || {};
179-
const hasBadge = this.#hasBadge;
180-
const selectedOptions = this.#combobox.selected ?? [];
181-
const typeahead = variant.startsWith('typeahead');
182-
const checkboxes = variant === 'checkbox';
183-
const badge = hasBadge && 'badge';
184-
const hasSelection = !!(Array.isArray(this.selected) ? this.selected.length : this.selected);
185-
const hideLightDomItems = typeahead && !ComboboxController.supportsCrossRootActiveDescendant;
186-
const placeholderIsInert = !placeholder && this.#slots.isEmpty('placeholder');
148+
const hideLightDomItems = !ComboboxController.supportsCrossRootActiveDescendant;
187149

188150
return html`
189151
<div id="outer"
190152
style="${styleMap(styles)}"
191-
class="${classMap({ disabled, typeahead, expanded, [anchor]: !!anchor, [alignment]: !!alignment })}">
153+
class="${classMap({ disabled, expanded, [anchor]: !!anchor, [alignment]: !!alignment })}">
192154
<div id="toggle">
193-
${!typeahead ? '' : html`
194-
<pf-button disabled plain label="Search">
195-
<svg fill="currentColor" height="1em" width="1em" viewBox="0 0 512 512">
196-
<path d="M256.233,5.756c-71.07,15.793-141.44,
197-
87.863-155.834,159.233c-11.495,57.076,0.3,111.153,
198-
27.688,154.335L6.339,441.172c-8.596,8.596-8.596,
199-
22.391,0,30.987l33.286,33.286c8.596,8.596,22.391,
200-
8.596,30.987,0L192.26,383.796c43.282,
201-
27.688,97.559,39.683,154.734,28.188c79.167-15.893,142.04-77.067,
202-
159.632-155.934C540.212,104.314,407.968-27.93,256.233,
203-
5.756z M435.857,208.37c0,72.869-59.075,131.944-131.944,131.944
204-
S171.969,281.239,171.969,208.37S231.043,76.426,303.913,
205-
76.426S435.857,135.501,435.857,208.37z">
206-
</path>
207-
</svg>
208-
</pf-button>
155+
<div class="search-icon">
156+
<svg fill="currentColor" height="1em" width="1em" viewBox="0 0 512 512">
157+
<path d="M256.233,5.756c-71.07,15.793-141.44,
158+
87.863-155.834,159.233c-11.495,57.076,0.3,111.153,
159+
27.688,154.335L6.339,441.172c-8.596,8.596-8.596,
160+
22.391,0,30.987l33.286,33.286c8.596,8.596,22.391,
161+
8.596,30.987,0L192.26,383.796c43.282,
162+
27.688,97.559,39.683,154.734,28.188c79.167-15.893,142.04-77.067,
163+
159.632-155.934C540.212,104.314,407.968-27.93,256.233,
164+
5.756z M435.857,208.37c0,72.869-59.075,131.944-131.944,131.944
165+
S171.969,281.239,171.969,208.37S231.043,76.426,303.913,
166+
76.426S435.857,135.501,435.857,208.37z">
167+
</path>
168+
</svg>
169+
</div>
209170
<input id="toggle-input"
210-
?hidden="${!typeahead}"
211171
?disabled="${disabled}"
212-
placeholder="${placeholder || this.#buttonLabel}">`}
213-
<button id="toggle-button">
214-
<span id="button-text" style="display: contents;">
215-
<span id="toggle-text"
216-
class="${classMap({ 'visually-hidden': !!typeahead, badge })}">${this.#buttonLabel}</span>${!hasBadge ? '' : html`
217-
<span id="toggle-badge">
218-
<pf-badge number="${selectedOptions.length}">${selectedOptions.length}</pf-badge>
219-
</span>`}
220-
</span>
221-
</button>
222-
<pf-button disabled plain label="Close">
172+
placeholder="${placeholder || this.#buttonLabel}">
173+
174+
<pf-button id="toggle-button" plain label="Close">
223175
<svg fill="currentColor" height="1em" width="1em" viewBox="0 0 352 512">
224176
<path d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19
225177
0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176
@@ -238,13 +190,8 @@ export class PfSearchInput extends LitElement {
238190
marginTop: `${height || 0}px`,
239191
width: width ? `${width}px` : 'auto',
240192
})}">
241-
<div id="listbox" class="${classMap({ checkboxes })}">
242-
<pf-search-input-option id="placeholder"
243-
disabled
244-
?inert="${placeholderIsInert}"
245-
aria-hidden="${ifDefined(placeholderIsInert ? undefined : String(!!hasSelection))}"
246-
?hidden="${!placeholder && this.#slots.isEmpty('placeholder')}"
247-
><slot name="placeholder">${placeholder ?? ''}</slot></pf-search-input-option>
193+
<div id="listbox">
194+
248195
${this.#combobox.renderItemsToShadowRoot()}
249196
<slot ?hidden="${hideLightDomItems}"></slot>
250197
</div>
@@ -273,48 +220,16 @@ export class PfSearchInput extends LitElement {
273220
private async selectedChanged(_: PfSearchInputOption[], selected: PfSearchInputOption[]) {
274221
this.value = selected.map(x => x.value).join();
275222
await this.updateComplete;
276-
switch (this.variant) {
277-
case 'single':
278-
this.hide();
279-
this._toggleButton?.focus();
280-
break;
281-
case 'typeahead':
282-
this._toggleInput!.value = this.value;
283-
}
223+
this._toggleInput!.value = this.value;
284224
}
285225

286-
@observes('variant')
287-
private async variantChanged() {
288-
this.#combobox.hostDisconnected();
289-
this.#combobox.multi = this.variant === 'typeaheadmulti' || this.variant === 'checkbox';
290-
this.#combobox.hostConnected();
291-
if (this.variant === 'checkbox') {
292-
import('@patternfly/elements/pf-badge/pf-badge.js');
293-
}
294-
}
295226

296227
@observes('value')
297228
private valueChanged() {
298229
this.#internals.setFormValue(this.value ?? '');
299230
this.dispatchEvent(new PfSelectChangeEvent());
300231
}
301232

302-
@observes('variant')
303-
@observes('value')
304-
private focusChips(): void {
305-
// whether select has removable chips for selected items
306-
// NOTE: revisit this in v5
307-
// reset input if chip has been added
308-
// const hasChips = this.variant === 'typeaheadmulti';
309-
// if (hasChips && this._toggleInput?.value) {
310-
// // const chip =
311-
// // this.shadowRoot?.querySelector(`pf-chip#chip-${this._toggleInput?.value}`) as PfChip;
312-
// // if (chip && this._chipGroup) {
313-
// // this._chipGroup.focusOnChip(chip);
314-
// // this._toggleInput.value = '';
315-
// // }
316-
// }
317-
}
318233

319234
async #doExpand() {
320235
try {
@@ -334,18 +249,6 @@ export class PfSearchInput extends LitElement {
334249
}
335250
}
336251

337-
/**
338-
* handles chip's remove button clicking
339-
* @param event remove event
340-
* @param opt pf-option
341-
*/
342-
// #onChipRemove(opt: PfOption, event: Event) {
343-
// if (event instanceof PfChipRemoveEvent) {
344-
// opt.selected = false;
345-
// this._toggleInput?.focus();
346-
// }
347-
// }
348-
349252
#computePlaceholderText() {
350253
return this.placeholder
351254
|| this.querySelector?.<HTMLSlotElement>('[slot=placeholder]')

0 commit comments

Comments
 (0)