Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
pills: Add updatePill and getPillByPredicate methods.
santhoshkumarCodese058a61c96c5
diff --git a/web/src/input_pill.ts b/web/src/input_pill.tsindex a76e425885f..69c07e76429 100644--- a/web/src/input_pill.ts+++ b/web/src/input_pill.ts@@ -70,6 +70,8 @@ export type InputPillContainer<ItemType> = {appendValue: (text: string) => void;appendValidatedData: (item: ItemType, disabled?: boolean, quiet?: boolean) => void;getByElement: (element: HTMLElement) => InputPill<ItemType> | undefined;+ getPillByPredicate: (predicate: (item: ItemType) => boolean) => InputPill<ItemType> | undefined;+ updatePill: (element: HTMLElement, new_item: ItemType) => void;items: () => ItemType[];removePill: (element: HTMLElement,@@ -296,6 +298,34 @@ export function create<ItemType extends {type: string}>(return store.pills.find((pill) => pill.$element[0] === element);},+ // This searches for a pill using a predicate function and returns it,+ // or undefined if no pill matches.+ getPillByPredicate(+ predicate: (item: ItemType) => boolean,+ ): InputPill<ItemType> | undefined {+ return store.pills.find((pill) => predicate(pill.item));+ },++ // Updates a pill's item data and refreshes its HTML representation in-place.+ // This is useful for real-time updates like user deactivated status.+ updatePill(element: HTMLElement, new_item: ItemType): void {+ const pill = this.getByElement(element);+ if (!pill) {+ return;+ }++ // Update the item data+ pill.item = new_item;++ // Regenerate the pill HTML with updated data+ const pill_html = funcs.generatePillHtml(new_item, pill.disabled);++ // Replace the pill element in the DOM+ const $new_element = $(pill_html);+ pill.$element.replaceWith($new_element);+ pill.$element = $new_element;+ },+_get_pills_for_testing() {return store.pills;},@@ -503,6 +533,8 @@ export function create<ItemType extends {type: string}>(appendValidatedData: funcs.appendValidatedData.bind(funcs),getByElement: funcs.getByElement.bind(funcs),+ getPillByPredicate: funcs.getPillByPredicate.bind(funcs),+ updatePill: funcs.updatePill.bind(funcs),getCurrentText: funcs.getCurrentText.bind(funcs),items: funcs.items.bind(funcs),removePill: funcs.removePill.bind(funcs),diff --git a/web/tests/input_pill.test.cjs b/web/tests/input_pill.test.cjsindex 15f6907c0d5..4bb12239300 100644--- a/web/tests/input_pill.test.cjs+++ b/web/tests/input_pill.test.cjs@@ -698,3 +698,93 @@ run_test("onTextInputHook", () => {assert.ok(hookCalled);});++run_test("getPillByPredicate", ({mock_template}) => {+ mock_template("input_pill.hbs", true, (data, html) => {+ assert.equal(typeof data.display_value, "string");+ return html;+ });++ const info = set_up();++ const widget = input_pill.create(info.config);+ widget.appendValue("blue,red,yellow");++ // Test finding pill by color name+ const found_blue_pill = widget.getPillByPredicate((item) => item.color_name === "BLUE");+ assert.ok(found_blue_pill);+ assert.equal(found_blue_pill.item.color_name, "BLUE");+ assert.equal(found_blue_pill.item.description, "color of the sky");++ const found_red_pill = widget.getPillByPredicate((item) => item.color_name === "RED");+ assert.ok(found_red_pill);+ assert.equal(found_red_pill.item.color_name, "RED");+ assert.equal(found_red_pill.item.description, "color of stop signs");++ // Test with non-existent item returns undefined+ const nonexistent_pill = widget.getPillByPredicate((item) => item.color_name === "GREEN");+ assert.equal(nonexistent_pill, undefined);++ // Test finding pill by different property (description)+ const found_by_description = widget.getPillByPredicate(+ (item) => item.description === "color of the sky",+ );+ assert.ok(found_by_description);+ assert.equal(found_by_description.item.color_name, "BLUE");+});++run_test("updatePill", ({mock_template}) => {+ mock_template("input_pill.hbs", true, (data, html) => {+ assert.equal(typeof data.display_value, "string");+ $(html)[0] = `<pill-stub ${data.display_value}>`;+ $(html).length = 1;+ return html;+ });++ const info = set_up();++ const widget = input_pill.create(info.config);+ widget.appendValue("blue,red");++ const pills = widget._get_pills_for_testing();+ assert.equal(pills.length, 2);++ const blue_pill = pills[0];+ assert.equal(blue_pill.item.color_name, "BLUE");+ assert.equal(blue_pill.item.description, "color of the sky");++ // Update the blue pill with new data+ const updated_blue_data = {+ color_name: "DARK BLUE",+ description: "color of the deep ocean",+ type: "color",+ };++ let element_replaced = false;+ blue_pill.$element.replaceWith = ($new_elem) => {+ element_replaced = true;+ assert.equal($new_elem[0], "<pill-stub DARK BLUE>");+ };++ widget.updatePill(blue_pill.$element[0], updated_blue_data);++ // Verify the pill element was replaced with new HTML+ assert.ok(element_replaced);+ // Verify that pill.$element was updated by updatePill+ assert.equal(blue_pill.$element[0], "<pill-stub DARK BLUE>");+ // Verify the pill's internal data was updated+ assert.equal(blue_pill.item.color_name, "DARK BLUE");+ assert.equal(blue_pill.item.description, "color of the deep ocean");++ // Verify other pill is unchanged+ const red_pill = pills[1];+ assert.equal(red_pill.item.color_name, "RED");+ assert.equal(red_pill.item.description, "color of stop signs");++ // Test updating non-existent pill element - should be a no-op+ const fake_element = {};+ widget.updatePill(fake_element, updated_blue_data);+ // Blue pill data should remain unchanged+ assert.equal(blue_pill.item.color_name, "DARK BLUE");+ assert.equal(blue_pill.item.description, "color of the deep ocean");+});