Skip to content

Commit fafc7f1

Browse files
authored
Merge pull request #467 from nyaruka/tab-change-events
Ensure initial index doesn't fire without tabs
2 parents 83a7f84 + 61cc37e commit fafc7f1

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

Diff for: src/compose/Compose.ts

+6-16
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ export class Compose extends FormElement {
294294
private handleTabChanged() {
295295
const tabs = this.shadowRoot.querySelector('temba-tabs') as TabPane;
296296
this.currentTab = tabs.getCurrentTab();
297-
298297
if (this.currentTab && this.currentTab.name === 'Shortcuts') {
299298
const shortcuts = this.shadowRoot.querySelector(
300299
'temba-shortcuts'
@@ -441,9 +440,9 @@ export class Compose extends FormElement {
441440

442441
if (line.startsWith('/')) {
443442
// switch to the shortcuts tab
444-
const tabs = this.shadowRoot.querySelector('temba-tabs') as TabPane;
445-
tabs.focusTab('Shortcuts');
446-
443+
if (this.currentTab.name !== 'Shortcuts') {
444+
this.getTabs().focusTab('Shortcuts');
445+
}
447446
const shortcuts = this.shadowRoot.querySelector(
448447
'temba-shortcuts'
449448
) as ShortcutList;
@@ -489,19 +488,11 @@ export class Compose extends FormElement {
489488
tabs.index = num - 1;
490489
}
491490

492-
// if they type / as the first character in a line, switch to the shortcut
493-
if (evt.key === '/' && this.currentTab.name !== 'Shortcuts') {
494-
const line = this.getCurrentLine();
495-
const text = line.text.trim();
496-
if (text.trim().length === 1) {
497-
evt.preventDefault();
498-
tabs.index = tabs.tabs.findIndex((tab) => tab.name === 'Shortcuts');
499-
}
500-
} else if (evt.key === 'Backspace') {
491+
if (evt.key === 'Backspace') {
501492
const line = this.getCurrentLine();
502493
const text = line.text;
503494
if (text === '/') {
504-
tabs.index = tabs.tabs.findIndex((tab) => tab.name === 'Reply');
495+
tabs.focusTab('Reply');
505496
}
506497
}
507498

@@ -540,7 +531,7 @@ export class Compose extends FormElement {
540531
}
541532

542533
public resetTabs() {
543-
(this.shadowRoot.querySelector('temba-tabs') as TabPane).index = 0;
534+
this.getTabs().focusTab('Reply');
544535
}
545536

546537
public getTabs(): TabPane {
@@ -637,7 +628,6 @@ export class Compose extends FormElement {
637628
<temba-tabs
638629
embedded
639630
focusedname
640-
index="0"
641631
@temba-context-changed=${this.handleTabChanged}
642632
refresh="${(this.currentAttachments || []).length}|${this.index}|${this
643633
.currentQuickReplies.length}|${showOptins}|${this

Diff for: src/tabpane/TabPane.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class TabPane extends RapidElement {
5757
5858
.tab .name {
5959
margin-left: 0.4em;
60-
max-width: 80px;
60+
max-width: 200px;
6161
overflow: hidden;
6262
transition: max-width 500ms ease-in-out, margin 500ms ease-in-out;
6363
white-space: nowrap;
@@ -308,6 +308,7 @@ export class TabPane extends RapidElement {
308308
}
309309
}
310310
this.tabs = tabs;
311+
this.index = 0;
311312
}
312313

313314
public firstUpdated(
@@ -322,11 +323,22 @@ export class TabPane extends RapidElement {
322323

323324
public updated(changedProperties: Map<string, any>) {
324325
super.updated(changedProperties);
325-
if (changedProperties.has('index') || changedProperties.has('tabs')) {
326+
327+
if (changedProperties.has('tabs')) {
326328
this.tabs.forEach((tab, index) => {
327329
tab.selected = index == this.index;
328330
});
329-
this.fireEvent(CustomEventType.ContextChanged);
331+
}
332+
333+
if (changedProperties.has('index')) {
334+
if (this.tabs.length >= 0) {
335+
if (this.index !== changedProperties.get('index')) {
336+
this.tabs.forEach((tab, index) => {
337+
tab.selected = index == this.index;
338+
});
339+
this.fireEvent(CustomEventType.ContextChanged);
340+
}
341+
}
330342
}
331343

332344
// if our current tab is hidden, select the first visible one

0 commit comments

Comments
 (0)