Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-material input with an addon

I'm trying to create an input with an extra text after it using angular-material. I want to achieve a similar effect as with bootstrap's .input-group-addon: enter image description here

The closest I got is this:

<md-content layout-padding>
    <div layout="row" class="md-whiteframe-z1" style="width: 40%">
        <md-select placeholder="Type" ng-model="discount.type" flex="50" class="md-select-full-width">
            <md-option value="AMOUNT">Amount</md-option>
            <md-option value="PERCENT">Percent</md-option>
        </md-select>
        <div flex="50" layout="row" layout-align="center center">
            <md-input-container flex>
                <label>Value</label>
                <input ng-model="discount.value">
            </md-input-container>
            <span>%</span>
        </div>
    </div>
</md-content>

What gives following result: enter image description here

As you can see the 2 fields are misaligned.

I also tried to use vertical-align on the <span>%</span> instead of layout-align="center center" but it seems to be ignored.

like image 832
Lukasz Wiktor Avatar asked Jun 24 '15 11:06

Lukasz Wiktor


People also ask

What is the use of Matinput?

matInput is a directive that allows native <input> and <textarea> elements to work with <mat-form-field> .

What is cdkFocusInitial?

cdkFocusInitial specifies the element that will receive focus upon initialization of the region. cdkFocusRegionStart and cdkFocusRegionEnd define the region within which focus will be trapped. When using the tab key, focus will move through this region and wrap around on either end.

What is matSuffix?

The matSuffix is a configuration directive for the material input field, that will ensure that the calendar icon is added at the end (to the right) of the input field.

What is MatFormFieldControl?

MatFormFieldControl. An interface which allows a control to work inside of a MatFormField .


2 Answers

I figured out a solution using <md-icon>:

<md-content layout-padding>
    <div layout="row" class="md-whiteframe-z1" style="width: 40%">
        <md-select placeholder="Type" ng-model="discount.type" flex="50" class="md-select-full-width">
            <md-option value="AMOUNT">Amount</md-option>
            <md-option value="PERCENT">Percent</md-option>
        </md-select>
        <div flex="50" layout="row">
            <md-input-container flex>
                <label>Value</label>
                <input ng-model="discount.value">
            </md-input-container>
            <md-icon md-font-set="regular-font">%</md-icon>
        </div>
    </div>
</md-content>

The "regular-font" is some non-existing icon font library to make sure that the text inside <md-icon> won't be interpreted as an Material icon.

Now it's well aligned:

enter image description here

You can see the working solution here: http://codepen.io/LukaszWiktor/pen/oXoqYg

like image 124
Lukasz Wiktor Avatar answered Oct 12 '22 14:10

Lukasz Wiktor


I just figured out a way to do this too for when you'd like the add-on to contain something more than a single character, using flexbox.

<md-input-container flex>
    <label>Length</label>
    <input type="number" ng-model="length" flex="80">
    <span flex>seconds</span>
</md-input-container>

The important parts are flex="80" on the input element, and flex on the span. This is telling the input to take up 80% of the space, and for the span to fill the remainder (at least I think so - I'm still learning flexbox).

The end result looks like this:

enter image description here

like image 28
shauneba Avatar answered Oct 12 '22 12:10

shauneba