Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Input and select in one form field

I want the input field and the drop down field in the same area like the one on the left (I did this on the inspector) Dropdown menus

No matter what class I put in my stylesheet it won't shrink the size of the select menu. This is the html code I have.

<mat-form-field appearance="outline">
     <mat-label>End Time</mat-label>
     <input matInput type="time" placeholder="HH:MM" id="end_time_hour" [formControl]="timeFormControl">
     <mat-select name="ampm" class="ampm" [(ngModel)]="event.eampm">
          <mat-option value="AM">AM</mat-option>
          <mat-option value="PM">PM</mat-option>
     </mat-select>
</mat-form-field>
like image 851
Luis Liz Avatar asked Jan 08 '19 03:01

Luis Liz


People also ask

How do you limit Angular materials multiple select items to n items?

In your component create a global variable named mySelections . This will store your selections :) It will hold an array of strings. Change the number 3 to N and presto, you're done.

What is disableOptionCentering?

disableOptionCentering: boolean. Whether to center the active option over the trigger. @Input() disableRipple: boolean. Whether ripples are disabled.

What is Matinput?

Matinput is an Angular directive that primarily allows input and text area elements to work with a form field. With this, you can display placeholders perfectly, add custom error messages, a clear button, specify the maximum length of the text or add prefixes and suffixes for a seamless user experience.


1 Answers

You could wrap your form field in a div and assign it a class so that you can nest the CSS. The reason to nest the CSS is to avoid it affecting the rest of the controls. I did something like this:

<div class="time-picker-component">
    <mat-form-field appearance="outline">
        <mat-label>End Time</mat-label>
        <input matInput type="time" placeholder="HH:MM" id="end_time_hour" [formControl]="timeFormControl">
     <mat-select name="ampm" class="ampm" [(ngModel)]="event.eampm">
          <mat-option value="AM">AM</mat-option>
          <mat-option value="PM">PM</mat-option>
     </mat-select>
   </mat-form-field>
</div>

and then add the folloing CSS somewhere globally:

.time-picker-component .mat-form-field-infix {
  display: inherit;
}

Demo

like image 162
Awad Maharoof Avatar answered Nov 08 '22 18:11

Awad Maharoof