dropdown_widget: Pass current value to get_options to avoid hacky this.

This commit is contained in:
evykassirer
2024-04-03 19:59:58 -07:00
committed by Tim Abbott
parent c449bb35e2
commit 9366d42f14
2 changed files with 22 additions and 18 deletions

View File

@@ -73,7 +73,7 @@ export class DropdownWidget {
widget_selector: string; widget_selector: string;
widget_wrapper_id: string; widget_wrapper_id: string;
widget_value_selector: string; widget_value_selector: string;
get_options: () => Option[]; get_options: (current_value: string | number | undefined) => Option[];
item_click_callback: ( item_click_callback: (
event: JQuery.ClickEvent, event: JQuery.ClickEvent,
instance: tippy.Instance, instance: tippy.Instance,
@@ -200,20 +200,24 @@ export class DropdownWidget {
"input.dropdown-list-search-input", "input.dropdown-list-search-input",
); );
this.list_widget = ListWidget.create($dropdown_list_body, this.get_options(), { this.list_widget = ListWidget.create(
name: `${CSS.escape(this.widget_name)}-list-widget`, $dropdown_list_body,
get_item: ListWidget.default_get_item, this.get_options(this.current_value),
modifier_html(item) { {
return render_dropdown_list({item}); name: `${CSS.escape(this.widget_name)}-list-widget`,
}, get_item: ListWidget.default_get_item,
filter: { modifier_html(item) {
$element: $search_input, return render_dropdown_list({item});
predicate(item, value) {
return item.name.toLowerCase().includes(value);
}, },
filter: {
$element: $search_input,
predicate(item, value) {
return item.name.toLowerCase().includes(value);
},
},
$simplebar_container: $popper.find(".dropdown-list-wrapper"),
}, },
$simplebar_container: $popper.find(".dropdown-list-wrapper"), );
});
$search_input.on("input.list_widget_filter", () => { $search_input.on("input.list_widget_filter", () => {
this.show_empty_if_no_items($popper); this.show_empty_if_no_items($popper);
@@ -371,7 +375,7 @@ export class DropdownWidget {
this.current_value = value; this.current_value = value;
} }
const all_options = this.get_options(); const all_options = this.get_options(this.current_value);
const option = all_options.find((option) => option.unique_id === this.current_value); const option = all_options.find((option) => option.unique_id === this.current_value);
// If provided, show custom text if cannot find current option. // If provided, show custom text if cannot find current option.

View File

@@ -33,19 +33,19 @@ export const COMMON_DROPDOWN_WIDGET_PARAMS = {
disable_for_spectators: true, disable_for_spectators: true,
}; };
export function filters_dropdown_options() { export function filters_dropdown_options(current_value) {
return [ return [
{ {
unique_id: FILTERS.FOLLOWED_TOPICS, unique_id: FILTERS.FOLLOWED_TOPICS,
name: $t({defaultMessage: "Followed topics"}), name: $t({defaultMessage: "Followed topics"}),
description: $t({defaultMessage: "Only topics you follow"}), description: $t({defaultMessage: "Only topics you follow"}),
bold_current_selection: this.current_value === FILTERS.FOLLOWED_TOPICS, bold_current_selection: current_value === FILTERS.FOLLOWED_TOPICS,
}, },
{ {
unique_id: FILTERS.UNMUTED_TOPICS, unique_id: FILTERS.UNMUTED_TOPICS,
name: $t({defaultMessage: "Standard view"}), name: $t({defaultMessage: "Standard view"}),
description: $t({defaultMessage: "All unmuted topics"}), description: $t({defaultMessage: "All unmuted topics"}),
bold_current_selection: this.current_value === FILTERS.UNMUTED_TOPICS, bold_current_selection: current_value === FILTERS.UNMUTED_TOPICS,
}, },
{ {
unique_id: FILTERS.ALL_TOPICS, unique_id: FILTERS.ALL_TOPICS,
@@ -53,7 +53,7 @@ export function filters_dropdown_options() {
description: $t({ description: $t({
defaultMessage: "Includes muted streams and topics", defaultMessage: "Includes muted streams and topics",
}), }),
bold_current_selection: this.current_value === FILTERS.ALL_TOPICS, bold_current_selection: current_value === FILTERS.ALL_TOPICS,
}, },
]; ];
} }