Search results

Getting started with Angular TextBox component

08 Apr 2021 / 4 minutes to read

This section briefly explains about how to create a simple TextBox through CSS classes using Angular seed repository.

Dependencies

The following list of dependencies are required to use the TextBox component in your application.

Copied to clipboard
|-- @syncfusion/ej2-angular-inputs
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-inputs
        |-- @syncfusion/ej2-base

Setup angular environment

Angular provides the easiest way to set angular CLI projects using Angular CLI tool.

Install the CLI application globally to your machine.

Copied to clipboard
npm install -g @angular/cli

Create a new application

Copied to clipboard
ng new syncfusion-angular-textbox

By default, it install the CSS style base application. To setup with SCSS, pass —style=scss argument on create project.

Example code snippet.

Copied to clipboard
ng new syncfusion-angular-textbox --style=scss

Navigate to the created project folder.

Copied to clipboard
cd syncfusion-angular-textbox

Adding TextBox package

All the available Essential JS 2 packages are published in npmjs.com registry.

To install TextBox component, use the following command.

Copied to clipboard
npm install @syncfusion/ej2-angular-inputs --save
(or)
npm i @syncfusion/ej2-angular-inputs --save

The —save will instruct NPM to include the TextBox package inside of the dependencies section of the package.json.

Adding TextBox to the application

  • Modify the template as HTML input element with e-inputclass in app.component.ts file to render the TextBox component.
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `<input class="e-input" type="text" placeholder="Enter Name" />`
})

export class AppComponent { }

Adding CSS reference

The following CSS files are available in ../node_modules/@syncfusion package folder. This can be referenced in [src/styles.css] using following code.

Copied to clipboard
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-inputs/styles/material.css';

The Custom Resource Generator (CRG) is an online web tool, which can be used to generate the custom script and styles for a set of specific components. This web tool is useful to combine the required component scripts and styles in a single file.

Adding icons to the TextBox

You can create a TextBox with icon as a group by creating the parent div element with the class e-input-group and add the icon element as span with the class e-input-group-icon. For detailed information, refer to the Groups section.

Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `<div class="wrap">
            <div class="e-input-group">
            <input class="e-input" name='input' type="text" (focus)="focusIn($event.target)"
            placeholder = "Enter Date" (blur)="focusOut($event.target)"/>
            <span class="e-input-group-icon e-input-popup-date" (mouseup)="onMouseUp($event.target)" (mousedown)="onMouseDown($event.target)"></span>
            </div>
          </div>`
})

export class AppComponent {
public focusIn(target: HTMLElement): void {
    target.parentElement.classList.add('e-input-focus');
}

public focusOut(target: HTMLElement): void {
    target.parentElement.classList.remove('e-input-focus');
}

public onMouseDown(target: HTMLElement): void {
    target.classList.add('e-input-btn-ripple');
}

public onMouseUp(target: HTMLElement): void {
    let ele: HTMLElement = target;
    setTimeout(
            () => {ele.classList.remove('e-input-btn-ripple'); },
            500);
}
}

Running the application

After completing the configuration required to render a basic TextBox, run the following command to display the output in your default browser.

Copied to clipboard
ng serve

The following example illustrates the output in your browser.

Source
Preview
app.component.ts
app.module.ts
main.ts
index.css
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `<div class="wrap">
            <div class="e-input-group">
            <input class="e-input" name='input' type="text" (focus)="focusIn($event.target)"
            placeholder = "Enter Date" (blur)="focusOut($event.target)"/>
            <span class="e-input-group-icon e-input-popup-date" (mouseup)="onMouseUp($event.target)" (mousedown)="onMouseDown($event.target)"></span>
            </div>
          </div>`
})

export class AppComponent {
public focusIn(target: HTMLElement): void {
    target.parentElement.classList.add('e-input-focus');
}

public focusOut(target: HTMLElement): void {
    target.parentElement.classList.remove('e-input-focus');
}

public onMouseDown(target: HTMLElement): void {
    target.classList.add('e-input-btn-ripple');
}

public onMouseUp(target: HTMLElement): void {
    let ele: HTMLElement = target;
    setTimeout(
            () => {ele.classList.remove('e-input-btn-ripple'); },
            500);
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Copied to clipboard
#container {
  visibility: hidden;
}

#loader {  
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

.wrap {
  box-sizing: border-box;
  margin: 0 auto;
  padding: 20px 10px;
  width: 260px;
}


.wrap label { /* csslint allow: adjoining-classes */
 font-weight:bold;
}

.wrap .e-input-group {/* csslint allow: adjoining-classes */
   margin:25px 0;
}

.e-input-group-icon:before {
  font-family: e-icons;
}

.e-input-group .e-input-group-icon.e-input-popup-date { /* csslint allow: adjoining-classes */
  font-size:16px;
}

.e-input-group.e-small .e-input-group-icon.e-input-popup-date { /* csslint allow: adjoining-classes */
  font-size:14px;
}

.e-input-group-icon.e-input-popup-date:before { /* csslint allow: adjoining-classes */
  content: "";
}

Floating label

The floating label TextBox floats the label above the TextBox after focusing, or filled with value in the TextBox. You can create the floating label TextBox by using the floatLabelType API.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `<div class="wrap">
<h4>Floating label as auto</h4>
<div class='textboxes'>
  <ejs-textbox placeholder="First Name" floatLabelType="Auto"></ejs-textbox>
</div>

<h4>Floating label as always</h4>
<div class='textboxes'>
  <ejs-textbox placeholder="First Name" floatLabelType="Always"></ejs-textbox>
</div>
  </div>`
})

export class AppComponent { }
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {TextBoxModule} from '@syncfusion/ej2-angular-inputs';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TextBoxModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

See Also