Groups in React Textbox

Published on : August 03,2022
Groups in React Textbox

The following section explains you the steps required to create Text Box with icon and floating label.

Textbox :

  • Create a parent div element with the class e-input-group
  • Place input element with the class e-input inside the parent div element.

import * as React from "react";
import './App.css';
export default class App extends React.Component<{}, {}> {
public render() {
  return (
    // element which is going to render the TextBox
    <input className="e-input" type="text" placeholder="Enter Name" />
  );
}
};

 

Floating label :

  • Add the e-float-input class to the parent div element.
  • Remove the e-input class and add required attribute to the input element.
  • Place the span element with class e-float-line after the input element.
  • Place the label element with class e-float-text after the above created span element. When you focus or filled with value in the Text Box, the label floats above the Text Box.

 


import * as React from "react";
import './App.css';
export default class App extends React.Component<{}, {}> {
public render() {
  return (
    // element which is going to render the Floating TextBox
    <div className="e-float-input e-input-group">
        <input type="text" required={true}/>
        <span className="e-float-line"/>
        <label className="e-float-text">Enter Name </label>
    </div>
  );
}
};

And refer to the following sections to add the icons to the Textbox.

 

With icon and floating label

Create an icon element as a span with the class e-input-group-icon, and the user can place the icon in either side of Textbox by adding the created icon element before/after the input.

For the floating label enabled Textbox add the icon element as first or last element inside the Textbox wrapper, and based on the element position it will act as prefix or suffix icon.

 


import * as React from "react";
import * as ReactDOM from "react-dom";

export default class Default extends React.Component {

public render() {
   return (
<div>

<h4> TextBox with icons </h4>

<div className="e-input-group ">
    <input className="e-input" type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} placeholder="Enter Date"/>
    <span className="e-input-group-icon e-input-popup-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
</div>

<div className="e-input-group e-float-icon-left">
    <span className="e-input-group-icon e-input-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
    <div className="e-input-in-wrap">
        <input className="e-input" type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} placeholder="Enter Date"/>
    </div>
</div>

<div className="e-input-group e-float-icon-left">
    <span className="e-input-group-icon e-input-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
    <div className="e-input-in-wrap">
        <input className="e-input" type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} placeholder="Enter Date"/>
        <span className="e-input-group-icon e-input-down"/>
    </div>
</div>

<h4> Floating label with icons </h4>

<div className="e-float-input e-input-group">
    <input required = {true} type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} />
    <span className="e-float-line"/>
    <label className="e-float-text"> Enter Date </label>
    <span className="e-input-group-icon e-input-popup-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
</div>

<div className="e-float-input e-input-group e-float-icon-left">
    <span className="e-input-group-icon e-input-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
    <div className="e-input-in-wrap">
        <input required = {true} type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} />
        <span className="e-float-line"/>
        <label className="e-float-text"> Enter Date </label>
    </div>
</div>

<div className="e-float-input e-input-group e-float-icon-left">
    <span className="e-input-group-icon e-input-date" onMouseDown = {this.onIconMouseDown} onMouseUp = {this.onIconMouseUp}/>
    <div className="e-input-in-wrap">
        <input required = {true} type="text" onFocus = {this.onInputFocus} onBlur = {this.onInputBlur} />
        <span className="e-float-line"/>
        <label className="e-float-text"> Enter Date </label>
        <span className="e-input-group-icon e-input-down"/>
    </div>
</div>
</div>
);
}


public onInputFocus(args: React.FocusEvent) {
if (!((args.target as HTMLElement).parentElement as HTMLElement).classList.contains('e-input-in-wrap')) {
    ((args.target as HTMLElement).parentElement as HTMLElement).classList.add('e-input-focus');
} else {
    (((args.target as HTMLElement).parentElement as HTMLElement).parentElement as HTMLElement).classList.add('e-input-focus')
}
}

public onInputBlur(args: React.FocusEvent) {
if (!((args.target as HTMLElement).parentElement as HTMLElement).classList.contains('e-input-in-wrap')) {
   ((args.target as HTMLElement).parentElement as HTMLElement).classList.remove('e-input-focus');
} else {
    (((args.target as HTMLElement).parentElement as HTMLElement).parentElement as HTMLElement).classList.remove('e-input-focus');
}
}


public onIconMouseDown(args: React.MouseEvent) {
args.persist();
setTimeout(
    () => {
        (args.target as HTMLElement).classList.add('e-input-btn-ripple');
    },
300);
}

public onIconMouseUp(args: React.MouseEvent) {
(args.target as HTMLElement).classList.remove('e-input-btn-ripple');
}
}

ReactDOM.render(<Default />, document.getElementById('input-container'));

CSS :


#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}
#input-container {
  width: 240px;
  margin: 0 auto;
  padding: 20px 0px;
}

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

.e-input-group-icon.e-input-popup-date:before {
  content: "e901";
}

.e-input-group-icon.e-input-down:before { 
  content: "e83d";
}

.e-input-group-icon.e-input-date:before { 
  content: "e901";
}

#input-container .e-input-group { 
  margin: 26px 0;
}

#input-container .e-float-input { 
  margin: 26px 0;
}

.wrap label { 
font-weight:bold;
}

 

With clear button and floating label

The clear button is added to the input for clearing the value given in the TextBox. It is shown only when the input field has a value, otherwise not shown.

You can add the clear button to the Textbox by using showClearButton API in textbox.

import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";
export default class App extends React.Component<{}, {}> {
public render() {
    return (
          <div className="App">
            <div className="textboxes">
                <h4>Textbox with clear button</h4>
                <TextBoxComponent placeholder="First Name" showClearButton= {true} floatLabelType="Never"/>
            </div>
            <div className="textboxes">
                <h4>Floating textbox with clear button</h4>
                <TextBoxComponent placeholder="Last Name" showClearButton= {true} floatLabelType="Auto"/>
            </div>
        </div>
    )
}
};

ReactDOM.render(<App />, document.getElementById('input-container'));

 

Floating Label without required attribute

You can render the Floating label TextBox without required attribute by manually float the label above of the Textbox using input events. You can manually float the label above of the Textbox by adding the below list of classes to the floating label element. The classes are:

Class Name Description
e-label-top Floats the label above of the TextBox.
e-label-bottom Label to be placed as placeholder for the TextBox.

 


import * as React from "react";
import * as ReactDOM from "react-dom";

export default class App extends React.Component<{}, {}> {
public textboxInstance: any;
constructor(props: any) {
    super(props);
    this.onFocusOut = this.onFocusOut.bind(this);
    this.onFocusIn = this.onFocusIn.bind(this);
    this.onInputEvt = this.onInputEvt.bind(this);
}

public onFocusOut(args: React.FocusEvent) {
    /* Update the label position based on Input value */
    this.updateLabelState((args.target as HTMLInputElement).value, ((args.target as HTMLElement).parentElement as HTMLElement).querySelector('.e-float-text') as HTMLElement);
}

public onFocusIn(args: React.FocusEvent) {
    const label = ((args.target as HTMLElement).parentElement as HTMLElement).querySelector('.e-float-text') as HTMLElement;
    label.classList.add('e-label-bottom');
    label.classList.remove('e-label-top');
}
public onInputEvt(args: React.FormEvent) {
/* Update the label position based on Input value */
  this.updateLabelState((args.target as HTMLInputElement).value, ((args.target as HTMLElement).parentElement as HTMLElement).querySelector('.e-float-text') as HTMLElement);
}

/* Update the label position based on Input value */
public updateLabelState(value: string ,label: HTMLElement) {

/* e-label-top - Float the label above of the Input */
/* e-label-bottom - Move the label to the Input */

    if (value) {
        label.classList.add('e-label-top');
        label.classList.remove('e-label-bottom');
    } else {
        label.classList.add('e-label-bottom');
        label.classList.remove('e-label-top');
    }
}

public render() {
    return (
        <div className="inner-container">
            <h4> Floating label without required attribute </h4>
            <div className="e-float-input">
              <input id='inpt1' type="text" onInput= {this.onInputEvt}  onFocus={this.onFocusIn} onBlur={ this.onFocusOut } ref = {e => this.textboxInstance = e!} />
              <span className="e-float-line"/>
              <label className="e-float-text">First Name</label>
            </div>
        </div>
    )
}

public componentDidMount() {
    /* Update the label position based on initial input value */
    this.updateLabelState(this.textboxInstance.value, this.textboxInstance.parentElement.querySelector('.e-float-text'));
}
};
ReactDOM.render(<App />, document.getElementById('input-container'));

 

Multi-line input with floating label

Add the HTML textarea element with the e-input class to create default multi-line input.

Add the floating label support to the multi-line input by creating the floating label structure as defined in the initial section.

import * as React from "react";
import * as ReactDOM from "react-dom";
export default class App extends React.Component<{}, {}> {
public render() {
    return (
        <div>
            <textarea className="e-input" placeholder="Address"/>
            <div className="e-float-input">
                <textarea required={true}/>
                <span className="e-float-line"/>
                <label className="e-float-text"> Address</label>
            </div>
        </div>
    )
}
};

ReactDOM.render(<App />, document.getElementById('input-container'));

Categories : React

Tags : React Js

Praful Sangani
Praful Sangani
I'm a passionate full-stack developer with expertise in PHP, Laravel, Angular, React Js, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap. I enjoy sharing my knowledge by writing tutorials and providing tips to others in the industry. I prioritize consistency and hard work, and I always aim to improve my skills to keep up with the latest advancements. As the owner of Open Code Solution, I'm committed to providing high-quality services to help clients achieve their business goals.


27 Comments

buy tricor medication tricor tablet tricor pill


cialis usa canadian viagra viagra next day delivery usa


order generic ketotifen 1mg ziprasidone 40mg for sale order imipramine for sale


buy minoxytop generic oral mintop best ed pill for diabetics


precose pill buy generic acarbose over the counter buy griseofulvin 250 mg generic


aspirin cheap buy imiquimod imiquimod usa


dipyridamole price pravachol 10mg us purchase pravachol generic


order duphaston 10mg order sitagliptin pills generic empagliflozin 25mg


order monograph without prescription cilostazol 100 mg us cilostazol for sale


order prasugrel 10 mg pill order prasugrel online detrol 1mg generic


buy ferrous sulfate 100mg sale generic ascorbic acid 500mg buy generic sotalol 40mg


order pyridostigmine 60mg online cheap feldene generic purchase maxalt pills


buy cheap generic vasotec order bicalutamide 50mg online buy lactulose online cheap


xalatan where to buy purchase xalatan sale oral rivastigmine 3mg


cheap premarin 600 mg order dostinex viagra fast shipping


omeprazole order oral lopressor lopressor 100mg without prescription


where can i buy telmisartan plaquenil 200mg price purchase molnunat generic


buy cialis 10mg pill tadalafil 20mg viagra pills


cenforce 50mg us buy naprosyn pill cheap chloroquine 250mg


buy provigil 200mg generic promethazine drug deltasone 10mg generic


omnicef 300 mg usa order cefdinir online cheap purchase lansoprazole pills


order absorica generic absorica online buy zithromax online buy


buy generic azithromycin for sale buy azipro 250mg online cheap gabapentin for sale online


order lipitor 10mg order generic albuterol amlodipine cost


money slots play online blackjack for real money order lasix 100mg for sale


order pantoprazole pill order phenazopyridine 200 mg generic buy phenazopyridine 200mg for sale


online casino usa real money jackpot party casino ventolin 4mg cheap


Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles