Skip to content

Commit 644d6f2

Browse files
authored
feat: table filters now always show their placeholder even when a selection is made (#1296)
1 parent f46c9d8 commit 644d6f2

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

projects/components/src/multi-select/multi-select.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ export class MultiSelectComponent<V> implements ControlValueAccessor, AfterConte
145145
@Input()
146146
public placeholder?: string;
147147

148+
@Input()
149+
public prefix?: string;
150+
148151
@Input()
149152
public disabled: boolean = false;
150153

@@ -275,13 +278,21 @@ export class MultiSelectComponent<V> implements ControlValueAccessor, AfterConte
275278
const selectedItems: SelectOptionComponent<V>[] = options.filter(item => this.isSelectedItem(item));
276279

277280
return {
278-
label: selectedItems.length === 0 ? this.placeholder : selectedItems[0]?.label,
281+
label: this.getLabel(selectedItems),
279282
selectedItemsCount: selectedItems.length
280283
};
281284
})
282285
);
283286
}
284287

288+
private getLabel(selectedItems: SelectOptionComponent<V>[]): string {
289+
if (selectedItems.length === 0) {
290+
return this.placeholder === undefined ? '' : this.placeholder;
291+
}
292+
293+
return this.prefix === undefined ? selectedItems[0]?.label : `${this.prefix}${selectedItems[0]?.label}`.trim();
294+
}
295+
285296
private propagateValueChangeToFormControl(value: V[] | undefined): void {
286297
this.propagateControlValueChange?.(value);
287298
this.propagateControlValueChangeOnTouch?.(value);

projects/components/src/table/controls/table-controls-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface TablePropertyControlOption {
3737

3838
export interface TableSelectControl {
3939
placeholder: string;
40+
prefix?: string;
4041
isMultiSelect: boolean;
4142
options: TableSelectControlOption[];
4243
}

projects/components/src/table/controls/table-controls.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
*ngIf="selectControl.isMultiSelect"
4949
[selected]="this.appliedFilters(selectControl)"
5050
[placeholder]="selectControl.placeholder"
51+
[prefix]="selectControl.prefix"
5152
class="control select"
5253
[ngClass]="{ applied: this.appliedFilters(selectControl).length > 0 }"
5354
showBorder="true"

projects/observability/src/shared/dashboard/widgets/table/table-widget-renderer.component.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { InteractionHandler } from '../../interaction/interaction-handler';
5454
import { TableWidgetRowInteractionModel } from './selections/table-widget-row-interaction.model';
5555
import { TableWidgetBaseModel } from './table-widget-base.model';
5656
import { SpecificationBackedTableColumnDef } from './table-widget-column.model';
57+
import { TableWidgetControlSelectOptionModel } from './table-widget-control-select-option.model';
5758
import { TableWidgetViewToggleModel } from './table-widget-view-toggle.model';
5859
import { TableWidgetModel } from './table-widget.model';
5960

@@ -232,39 +233,51 @@ export class TableWidgetRendererComponent
232233
.filter(selectControlModel => selectControlModel.visible)
233234
.map(selectControlModel => {
234235
if (selectControlModel.placeholder === changed?.placeholder) {
235-
return of(changed);
236+
return this.buildTableSelectControl(selectControlModel, changed);
236237
}
237238

238-
// Fetch the values for the selectFilter dropdown
239-
return selectControlModel.getOptions().pipe(
240-
take(1),
241-
withLatestFrom(this.selectFilterSubject),
242-
map(([options, filters]) => {
243-
const foundPreferences = preferences.selections
244-
? preferences.selections.find(
245-
preferencesSelectionControl =>
246-
selectControlModel.placeholder === preferencesSelectionControl.placeholder
247-
)
248-
: undefined;
239+
const foundPreferences = preferences.selections
240+
? preferences.selections.find(
241+
preferencesSelectionControl =>
242+
selectControlModel.placeholder === preferencesSelectionControl.placeholder
243+
)
244+
: undefined;
249245

250-
return (
251-
foundPreferences ?? {
252-
placeholder: selectControlModel.placeholder,
253-
isMultiSelect: selectControlModel.isMultiselect,
254-
options: options.map(option => ({
255-
...option,
256-
applied: this.isFilterApplied(option.metaValue, filters)
257-
}))
258-
}
259-
);
260-
})
261-
);
246+
// Fetch the values for the selectFilter dropdown
247+
return this.buildTableSelectControl(selectControlModel, foundPreferences);
262248
})
263249
)
264250
)
265251
);
266252
}
267253

254+
private buildTableSelectControl(
255+
model: TableWidgetControlSelectOptionModel,
256+
override?: TableSelectControl
257+
): Observable<TableSelectControl> {
258+
return model.getOptions().pipe(
259+
take(1),
260+
withLatestFrom(this.selectFilterSubject),
261+
map(([options, filters]) => {
262+
const mergedOptions = options.map(option => {
263+
const found = override?.options.find(o => o.label === option.label);
264+
265+
return {
266+
...option,
267+
applied: found?.applied || this.isFilterApplied(option.metaValue, filters)
268+
};
269+
});
270+
271+
return {
272+
placeholder: model.placeholder,
273+
prefix: `${model.placeholder}: `,
274+
isMultiSelect: model.isMultiselect,
275+
options: mergedOptions
276+
};
277+
})
278+
);
279+
}
280+
268281
private isFilterApplied(filterOption: TableFilter, appliedFilters: TableFilter[]): boolean {
269282
// This gets just a little tricky because multiple options for a single select could be IN filtered together
270283
return (

0 commit comments

Comments
 (0)