Angular unfocus tab and set focus to different tab - css

I have a Material Tab component in Angular 2.
I want to have a button at the end of the tabs, that acts as a add tab button. Upon clicking on it a new tab is created before it.
I tried putting a button there, but couldn't find how to place it exactly next to the last tab.
So what I did is I added a tab that acts like a button. When this tab is clicked, a new tab is created.
However, when this tab is clicked, it gains focus. While I can change which tab is selected, the tab still has the focused UI (it is colored).
How can I make it lose it's focus completely?
P.S. If there is a way to add a regular button next to the last tab, without making it a tab, this would also be good.
Edit - Code:
This is how my tabs are setup:
<mat-tab-group (selectedTabChange)="selectedTab($event)">
<mat-tab>
<ng-template mat-tab-label>
Basic Details
</ng-template>
</mat-tab>
<mat-tab #categoryTab *ngFor="let table of mCase.Tables; let tableIndex = index" [attr.data-index]="tableIndex">
<mat-tab>
<mat-tab #addCategory>
<ng-template mat-tab-label>
<div color="white" class="center">Add category</div>
</ng-template>
</mat-tab>
</mat-tab-group>
Code behind:
public selectedTab(e) {
if (e.index == 1 + this.mCase.Tables.length) {
//Add new category
this.CreateTable();
this.selectedIndex = this.mCase.Tables.length;
}
Promise.resolve().then(() => this.selectedIndex = e.index);
}

You could try add the button like this:
<mat-tab-group>
<mat-tab>
<ng-template mat-tab-label>
Basic Details
</ng-template>
</mat-tab>
<mat-tab disabled>
<ng-template mat-tab-label>
<button mat-icon-button (click)="foo()">
<mat-icon>add_circle</mat-icon>
</button>
</ng-template>
</mat-tab>
</mat-tab-group>
Working example: https://stackblitz.com/edit/angular-zrklwg
By the way, if you are bothered by the button having disabled like styles, you can just override that with some custom CSS! :D

You can use .blur() on the event.currentTarget of the button which has been clicked. .blur() is the opposite of the .focus() method and will remove focus from an element.
In you question you said:
Upon clicking on it a new tab is created before it.
You didn't add any html, so I just went and created an example of how you can dynamically append buttons to the html via a data bound array.
this.buttons.splice(clickedButtonIndex, 0, {Name: "New Button"});
To learn about how this works, see this answer.
This is the crux of the component code that you can use to blur focus on the element.
addButton(clickedButtonIndex, event){
this.buttons.splice(clickedButtonIndex, 0, {Name: "New Button"});
event.currentTarget.blur();
}
Working Example: https://stackblitz.com/edit/angular-vslscm
In the example, I have set a red background in css to be applied to buttons when they are focused. You can see that when you click on a button, it momentarily will turn red before being toggled off again. Clicking the buttons will add a new button before the button which was clicked.

Related

Button element and 'open in new tab' option

I have button element like below:
<button class="button-8" role="button" id="clients" onclick="location.href='Clients_list.php';">Clients</button>
Is it possible to make it work the way 'a' elements work, so the user can right click and pick "open in new tab"?

How to position google autocomplete predictions?

I have a table with a list of contacts as seen in the image. I also have an event that gets triggered when a row is clicked so it can show the selected contact detail (I am using angular NGIF directive to show and hide the detail)
The thing is because my google input is placed inside an NGIF block, when user expands the contact detail and tries to type in it, the predictions (pac-container) is showing on the row that I clicked (within the table) rather than underneath the google input autocomplete search box.
I tried to manipulate the css of the class pac-container but I couldn't figure out a way to override the css of that class.
Here is my the html of my autocomplete search box
<div *ngIf="showGoogleAddress" class="mg-b-10" fxLayoutGap="10px" fxLayout="row" fxLayout.xs="column" fxLayout.sm="column" fxFlexFill>
<div fxFlex>
<mat-form-field appearance="outline" class="form-field">
<mat-label>Google Address Search Box</mat-label>
<input [debounceTime]="500" ngxAutocomPlace autocomplete="off" (selectedPlace)="placeChanged($event)" placeholder="ex: Phoenix, Arizona" matInput #googleAddress="matInput" >
<mat-icon matSuffix>location_on</mat-icon>
<mat-hint>Search for compolete address, or city</mat-hint>
</mat-form-field>
</div>
</div>
also, here is a screenshot of the entire form after expanding the detail
If the google autocomplete box was outside the NGIF, then it will render the predictions just fine without an issue. It seems that because I am using NGIF directive, the google component is not able to determine the right position (calculate the right position) to render its predictions..
When I tried
.pac-container {
position: absolute !important;
z-index: 100000;
}
it did not work.
I had to go with a DOM Manipulation way using JS.. so I added a (keyUp) event handler on google search box with a method to calculate the position like the following:
adjustResPosition() {
setTimeout(() => {
var gSearchBar = document.getElementById('googleBar');
var topPos = (gSearchBar?.getBoundingClientRect().top || 0) + window.scrollY + 25;
var leftPos = (gSearchBar?.getBoundingClientRect().left || 0) + window.scrollX;
var gPreds = document.querySelector('.pac-container');
gPreds?.setAttribute('style', `top: ${topPos}px; position: absolute!important; width: auto; left: ${leftPos}px`);
}, 600);
}
The reason why I put setTimeout to 600ms is because I have a debounce 500 ms each until it grabs the next predictions and put it in the pac-container div
Note, this is not an optimal solution and it can be improved with ViewChild or ElementRef

Button not appearing using angular material while using go to next tab

I am using angular material tab library to display vertical and horizontal tabs. There are 3 horizontal tabs and in 2nd horizontal tab, there are vertical tabs. When I put Next button it should go to next horizontal tab. It's working with 1st horizontal tab but in 2nd horizontal tab Next button is not appearing.
Here is: stackblitz - ngular-material-tabs-inside-tab
in tab-group-basic-example.html in 2nd horizontal tab, I put Next button:
<div class="m-10">
<button (click)="nextTab(2)">Next</button>
</div>
in ts file:
nextTab(index: number): void {
this.selectedIndex = index;
}
Looks like the issue is that you want do display something outside of mat-tab. Moving the next button inside it does the trick:
<mat-tab *ngFor="let tab of tabs; let index = index" [label]="tab">
<div class="m-10" #appenHere></div>
Contents for {{tab}} tab <br> <br>
<button (click)="addNewComponent()">
Append to {{tab}}
</button>
<div class="m-10">
<button (click)="nextTab(2)">Next</button>
</div>
</mat-tab>

font-awesome icon doesn't show up in [ngClass] in Angular 4

I'm facing a strange error in my Angular 4 HTML Template, I'm trying to toggle the class of <i> tag, but only one class shows up, but not the other. Here is my code;
<i [ngClass]="{'fa fa-eye': visible, 'fa fa-eye-slash': !visible}" aria-hidden="true" (click) = "toggle(pass)"></i>
TS:
visible = false;
toggle(event){
this.visible = !this.visible;
}
When i check my app, the first icon shows up i.e. the default on fa fa-eye-slash but when i click on it it shows only box.
Edit:
Tried with different icons from FA, but nothing shows up.
You need to change the html to:
<i class="fa" [ngClass]="{'fa-eye': visible, 'fa-eye-slash': !visible}" aria-hidden="true" (click) = "toggle(pass)"></i>

autoit - IE click a button based on its text.

I have a button on a form that I need to click and it looks something like this
<button id="****" class="****">Click Me</button>
I am using the following code to click other buttons which works fine but that is because the other buttons has a value="*****" assigned to it
$oButtons = _IETagnameGetCollection($oIE, "button")
For $oButton in $oButtons
If String($oButton.value) = "Click Me" Then
_IEAction($oButton, "click")
EndIf
Next
I know that the $oButton.value is what is getting the value for the button, but what other "operator" is there besides value that can get the anchor text of the button?
The example that you posted should be used only in a situation when you don't have an ID of the element. ID is always unique.
If the button that you want to click has an ID, you can use it as a reference to that button.
EXAMPLE:
HTML:
<button id="MyButton" class="someClass">Click Me</button>
CODE:
$oBtn = _IEGetObjById($oIE, "MyButton")
_IEAction($oBtn, "click")
Apart from .Value you can use all of the below:
$oBtn.innerText
$oBtn.outerText
$oBtn.innerHtml
$oBtn.outerHtml
$oBtn.classname
$oBtn.id
$oBtn.name
$oBtn.href
$oBtn.src
$oBtn.click
$oBtn.focus
...
Note that you can use $oBtn.value to read or write to the "Value" property of the html element, but for eg. "itemProp" property $oBtn.itemProp won't work
EXAMPLE:
HTML:
<button id="MyButton" class="someClass" itemprop="streetAddressSubmit" >Click Me</button>
This wont work:
$ItempropValue = $oBtn.itemprop
This is how it should be done:
$ItempropValue = $oBtn.getAttributeNode ('itemprop').NodeValue

Resources