typeahead: Pass query to highlighter_html instead of using hacky this.

This commit is contained in:
evykassirer 2024-03-24 20:33:14 -07:00 committed by Tim Abbott
parent cf9cf14dde
commit 9fc6793809
3 changed files with 10 additions and 10 deletions

View File

@ -193,7 +193,7 @@ class Typeahead<ItemType extends string | object> {
items: number;
matcher: (item: ItemType, query: string) => boolean;
sorter: (items: ItemType[]) => ItemType[];
highlighter_html: (item: ItemType) => string | undefined;
highlighter_html: (item: ItemType, query: string) => string | undefined;
updater: (
item: ItemType,
event?: JQuery.ClickEvent | JQuery.KeyUpEvent | JQuery.KeyDownEvent,
@ -404,7 +404,7 @@ class Typeahead<ItemType extends string | object> {
render(final_items: ItemType[], matching_items: ItemType[]): this {
const $items: JQuery[] = final_items.map((item) => {
const $i = $(ITEM_HTML).data("typeahead-value", item);
const item_html = this.highlighter_html(item) ?? "";
const item_html = this.highlighter_html(item, this.query) ?? "";
const $item_html = $i.find("a").html(item_html);
const option_label_html = this.option_label(matching_items, item);
@ -688,7 +688,7 @@ class Typeahead<ItemType extends string | object> {
* =========================== */
type TypeaheadOptions<ItemType> = {
highlighter_html: (item: ItemType) => string | undefined;
highlighter_html: (item: ItemType, query: string) => string | undefined;
items: number;
source: (query: string) => ItemType[];
// optional options

View File

@ -65,8 +65,8 @@ export function set_up($input, pills, opts) {
}
return source;
},
highlighter_html(item) {
if (include_streams(this.query)) {
highlighter_html(item, query) {
if (include_streams(query)) {
return typeahead_helper.render_stream(item);
}

View File

@ -160,7 +160,7 @@ run_test("set_up", ({mock_template, override}) => {
if (opts.stream) {
// Test stream highlighter_html for widgets that allow stream pills.
assert.equal(
config.highlighter_html.call(fake_stream_this, denmark),
config.highlighter_html(denmark, fake_stream_this.query),
$fake_rendered_stream,
);
}
@ -168,23 +168,23 @@ run_test("set_up", ({mock_template, override}) => {
// If user is also allowed along with user_group
// then we should check that each of them rendered correctly.
assert.equal(
config.highlighter_html.call(fake_group_this, testers),
config.highlighter_html(testers, fake_group_this.query),
$fake_rendered_group,
);
assert.equal(
config.highlighter_html.call(fake_person_this, me),
config.highlighter_html(me, fake_person_this.query),
$fake_rendered_person,
);
}
if (opts.user && !opts.user_group) {
assert.equal(
config.highlighter_html.call(fake_person_this, me),
config.highlighter_html(me, fake_person_this.query),
$fake_rendered_person,
);
}
if (!opts.user && opts.user_group) {
assert.equal(
config.highlighter_html.call(fake_group_this, testers),
config.highlighter_html(testers, fake_group_this.query),
$fake_rendered_group,
);
}