Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

input_pill: Add disabled flag to make a pill disabled.

Shubham Padia274fdc590849
diff --git a/web/src/input_pill.ts b/web/src/input_pill.ts
index 7892bb7dbf3..6e22ffe9248 100644
--- a/web/src/input_pill.ts
+++ b/web/src/input_pill.ts
@@ -30,7 +30,7 @@ type InputPillCreateOptions<ItemType> = {
) => ItemType | undefined;
get_text_from_item: (item: ItemType) => string;
get_display_value_from_item: (item: ItemType) => string;
- generate_pill_html?: (item: ItemType) => string;
+ generate_pill_html?: (item: ItemType, disabled?: boolean) => string;
on_pill_exit?: (
clicked_pill: HTMLElement,
all_pills: InputPill<ItemType>[],
@@ -42,6 +42,7 @@ type InputPillCreateOptions<ItemType> = {
export type InputPill<ItemType> = {
item: ItemType;
$element: JQuery;
+ disabled: boolean;
};
type InputPillStore<ItemType> = {
@@ -66,7 +67,7 @@ type InputPillStore<ItemType> = {
// These are the functions that are exposed to other modules.
export type InputPillContainer<ItemType> = {
appendValue: (text: string) => void;
- appendValidatedData: (item: ItemType) => void;
+ appendValidatedData: (item: ItemType, disabled?: boolean) => void;
getByElement: (element: HTMLElement) => InputPill<ItemType> | undefined;
items: () => ItemType[];
removePill: (
@@ -154,18 +155,20 @@ export function create<ItemType extends {type: string}>(
// This is generally called by typeahead logic, where we have all
// the data we need (as opposed to, say, just a user-typed email).
- appendValidatedData(item: ItemType) {
+ appendValidatedData(item: ItemType, disabled = false) {
let pill_html;
if (store.generate_pill_html !== undefined) {
- pill_html = store.generate_pill_html(item);
+ pill_html = store.generate_pill_html(item, disabled);
} else {
pill_html = render_input_pill({
display_value: store.get_display_value_from_item(item),
+ disabled,
});
}
const payload: InputPill<ItemType> = {
item,
$element: $(pill_html),
+ disabled,
};
store.pills.push(payload);
@@ -215,6 +218,9 @@ export function create<ItemType extends {type: string}>(
const idx = store.pills.findIndex((pill) => pill.$element[0] === element);
if (idx !== -1) {
+ if (store.pills[idx]!.disabled) {
+ return undefined;
+ }
store.pills[idx]!.$element.remove();
const pill = util.the(store.pills.splice(idx, 1));
if (store.onPillRemove !== undefined) {
@@ -240,7 +246,7 @@ export function create<ItemType extends {type: string}>(
removeLastPill(trigger: RemovePillTrigger, quiet?: boolean) {
const pill = store.pills.pop();
- if (pill) {
+ if (pill && !pill.disabled) {
pill.$element.remove();
if (!quiet && store.onPillRemove !== undefined) {
store.onPillRemove(pill, trigger);
diff --git a/web/templates/input_pill.hbs b/web/templates/input_pill.hbs
index c8432adfc6e..1fd81c0c0de 100644
--- a/web/templates/input_pill.hbs
+++ b/web/templates/input_pill.hbs
@@ -28,7 +28,9 @@
<i class="zulip-icon zulip-icon-bot" aria-label="{{t 'Bot' }}"></i>
{{~/if~}}
</span>
+ {{#unless disabled}}
<div class="exit">
<a role="button" class="zulip-icon zulip-icon-close pill-close-button"></a>
</div>
+ {{/unless}}
</div>
diff --git a/web/tests/input_pill.test.cjs b/web/tests/input_pill.test.cjs
index 9eb57de9ec1..15f6907c0d5 100644
--- a/web/tests/input_pill.test.cjs
+++ b/web/tests/input_pill.test.cjs
@@ -459,6 +459,7 @@ run_test("insert_remove", ({mock_template}) => {
assert.ok(removed);
assert.equal(color_removed, "YELLOW");
assert.ok(prev_pill_focused);
+ color_removed = undefined;
prev_pill_focused = false;
assert.deepEqual(widget.items(), [items.blue, items.red]);
@@ -472,12 +473,29 @@ run_test("insert_remove", ({mock_template}) => {
$container.set_find_results(".pill:focus", $focus_pill_stub);
+ const red_pill = pills.find((pill) => pill.item.color_name === "RED");
+ // Disabled pill should not be removed.
+ red_pill.disabled = true;
+
key_handler = $container.get_on_handler("keydown", ".pill");
key_handler({
key: "Backspace",
preventDefault: noop,
});
+ assert.equal(color_removed, undefined);
+ assert.ok(prev_pill_focused);
+
+ // We should be able to remove the pill after marking it as not
+ // disabled.
+ red_pill.disabled = false;
+ prev_pill_focused = false;
+ assert.deepEqual(widget.items(), [items.blue, items.red]);
+
+ key_handler({
+ key: "Backspace",
+ preventDefault: noop,
+ });
assert.equal(color_removed, "RED");
assert.ok(prev_pill_focused);
});