Skip to content

feat: table filters now always show their placeholder even when a sel… #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion projects/components/src/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export class MultiSelectComponent<V> implements ControlValueAccessor, AfterConte
@Input()
public placeholder?: string;

@Input()
public prefix?: string;

@Input()
public disabled: boolean = false;

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

return {
label: selectedItems.length === 0 ? this.placeholder : selectedItems[0]?.label,
label: this.getLabel(selectedItems),
selectedItemsCount: selectedItems.length
};
})
);
}

private getLabel(selectedItems: SelectOptionComponent<V>[]): string {
if (selectedItems.length === 0) {
return this.placeholder === undefined ? '' : this.placeholder;
}

return this.prefix === undefined ? selectedItems[0]?.label : `${this.prefix}${selectedItems[0]?.label}`.trim();
Copy link
Contributor

@anandtiwary anandtiwary Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh.. don't you think this is confusing? Currently, we show a placeholder, something like "Select labels" and once we select a few labels, we would show "label 1 + 2" others. Now we are showing that upfront. IMO, we lose the distinction and it will be hard to distinguish between your new placeholder and the existing selection option trigger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Fair point. There are probably a few uses that we don't want to follow this pattern. Let me make it configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so I just looked back at the code and I had the foresight to already make this configurable. If the user doesn't see the prefix input, the behavior should be the same as before this PR.

}

private propagateValueChangeToFormControl(value: V[] | undefined): void {
this.propagateControlValueChange?.(value);
this.propagateControlValueChangeOnTouch?.(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface TablePropertyControlOption {

export interface TableSelectControl {
placeholder: string;
prefix?: string;
isMultiSelect: boolean;
options: TableSelectControlOption[];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
*ngIf="selectControl.isMultiSelect"
[selected]="this.appliedFilters(selectControl)"
[placeholder]="selectControl.placeholder"
[prefix]="selectControl.prefix"
class="control select"
[ngClass]="{ applied: this.appliedFilters(selectControl).length > 0 }"
showBorder="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { InteractionHandler } from '../../interaction/interaction-handler';
import { TableWidgetRowInteractionModel } from './selections/table-widget-row-interaction.model';
import { TableWidgetBaseModel } from './table-widget-base.model';
import { SpecificationBackedTableColumnDef } from './table-widget-column.model';
import { TableWidgetControlSelectOptionModel } from './table-widget-control-select-option.model';
import { TableWidgetViewToggleModel } from './table-widget-view-toggle.model';
import { TableWidgetModel } from './table-widget.model';

Expand Down Expand Up @@ -232,39 +233,51 @@ export class TableWidgetRendererComponent
.filter(selectControlModel => selectControlModel.visible)
.map(selectControlModel => {
if (selectControlModel.placeholder === changed?.placeholder) {
return of(changed);
return this.buildTableSelectControl(selectControlModel, changed);
}

// Fetch the values for the selectFilter dropdown
return selectControlModel.getOptions().pipe(
take(1),
withLatestFrom(this.selectFilterSubject),
map(([options, filters]) => {
const foundPreferences = preferences.selections
? preferences.selections.find(
preferencesSelectionControl =>
selectControlModel.placeholder === preferencesSelectionControl.placeholder
)
: undefined;
const foundPreferences = preferences.selections
? preferences.selections.find(
preferencesSelectionControl =>
selectControlModel.placeholder === preferencesSelectionControl.placeholder
)
: undefined;

return (
foundPreferences ?? {
placeholder: selectControlModel.placeholder,
isMultiSelect: selectControlModel.isMultiselect,
options: options.map(option => ({
...option,
applied: this.isFilterApplied(option.metaValue, filters)
}))
}
);
})
);
// Fetch the values for the selectFilter dropdown
return this.buildTableSelectControl(selectControlModel, foundPreferences);
})
)
)
);
}

private buildTableSelectControl(
model: TableWidgetControlSelectOptionModel,
override?: TableSelectControl
): Observable<TableSelectControl> {
return model.getOptions().pipe(
take(1),
withLatestFrom(this.selectFilterSubject),
map(([options, filters]) => {
const mergedOptions = options.map(option => {
const found = override?.options.find(o => o.label === option.label);

return {
...option,
applied: found?.applied || this.isFilterApplied(option.metaValue, filters)
};
});

return {
placeholder: model.placeholder,
prefix: `${model.placeholder}: `,
isMultiSelect: model.isMultiselect,
options: mergedOptions
};
})
);
}

private isFilterApplied(filterOption: TableFilter, appliedFilters: TableFilter[]): boolean {
// This gets just a little tricky because multiple options for a single select could be IN filtered together
return (
Expand Down