dropdown_widget: Allow showing custom text if value is not in options.

If the current value is not in the calculated options,
`text_if_current_value_not_in_options` can be provided to
the widget to show custom text in that case.

Used by stream / user announcement settings if user doesn't
have access to information about the currently selected stream.
This commit is contained in:
Aman Agrawal
2023-11-22 09:53:37 +00:00
committed by Tim Abbott
parent 51b39cb682
commit 2acf3cbfa4
2 changed files with 11 additions and 11 deletions

View File

@@ -44,8 +44,8 @@ export class DropdownWidget {
// NOTE: Any value other than `null` will be rendered when class is initialized.
default_id = null,
unique_id_type = null,
// Show disabled state if the default_id is not in `get_options()`.
show_disabled_if_current_value_not_in_options = false,
// Text to show if the current value is not in `get_options()`.
text_if_current_value_not_in_options = null,
hide_search_box = false,
// Disable the widget for spectators.
disable_for_spectators = false,
@@ -70,8 +70,7 @@ export class DropdownWidget {
this.current_value = default_id;
this.unique_id_type = unique_id_type;
this.$events_container = $events_container;
this.show_disabled_if_current_value_not_in_options =
show_disabled_if_current_value_not_in_options;
this.text_if_current_value_not_in_options = text_if_current_value_not_in_options;
this.hide_search_box = hide_search_box;
this.disable_for_spectators = disable_for_spectators;
}
@@ -308,11 +307,12 @@ export class DropdownWidget {
}
const all_options = this.get_options();
let option = all_options.find((option) => option.unique_id === this.current_value);
const option = all_options.find((option) => option.unique_id === this.current_value);
// Show disabled if cannot find current option.
if (!option && this.show_disabled_if_current_value_not_in_options) {
option = all_options.find((option) => option.is_setting_disabled === true);
// If provided, show custom text if cannot find current option.
if (!option && this.text_if_current_value_not_in_options) {
$(this.widget_value_selector).text(this.text_if_current_value_not_in_options);
return;
}
if (!option) {

View File

@@ -626,7 +626,7 @@ export function init_dropdown_widgets() {
const disabled_option = {
is_setting_disabled: true,
unique_id: DISABLED_STATE_ID,
name: $t({defaultMessage: "Cannot view stream"}),
name: $t({defaultMessage: "Disabled"}),
};
options.unshift(disabled_option);
@@ -649,7 +649,7 @@ export function init_dropdown_widgets() {
},
default_id: page_params.realm_notifications_stream_id,
unique_id_type: dropdown_widget.DATA_TYPES.NUMBER,
show_disabled_if_current_value_not_in_options: true,
text_if_current_value_not_in_options: $t({defaultMessage: "Cannot view stream"}),
});
settings_components.set_notifications_stream_widget(notifications_stream_widget);
notifications_stream_widget.setup();
@@ -670,7 +670,7 @@ export function init_dropdown_widgets() {
},
default_id: page_params.realm_signup_notifications_stream_id,
unique_id_type: dropdown_widget.DATA_TYPES.NUMBER,
show_disabled_if_current_value_not_in_options: true,
text_if_current_value_not_in_options: $t({defaultMessage: "Cannot view stream"}),
});
settings_components.set_signup_notifications_stream_widget(signup_notifications_stream_widget);
signup_notifications_stream_widget.setup();