Open In App

ReactJS UI Ant Design Input Component

Last Updated : 02 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Ant Design Library has this component pre-built, and it is very easy to integrate as well. Input Component is used to create a basic widget for getting the user input is a text field. We can use the following approach in ReactJS to use the Ant Design Input Component.

Input Props:

  • addonAfter: It is used to denote the label text displayed after the input field.
  • addonBefore: It is used to denote the label text displayed before the input field.
  • allowClear: It is used to remove input content with a clear icon if it is set to true.
  • bordered: It is used to indicate whether it has border style or not.
  • defaultValue: It is used to denote the initial input content.
  • disabled: It is used to indicate whether the input is disabled or not.
  • id: It is used to denote the ID for input.
  • maxLength: It is used to denote the max length.
  • prefix: It is used to denote the prefix icon for the Input.
  • size: It is used to denote the size of the input box.
  • suffix: It is used to denote the suffix icon for the Input.
  • type: It is used to denote the type of input.
  • value: It is used to denote the input content value.
  • onChange: It is the callback function that is triggered when user input changes.
  • onPressEnter: It is the callback function that is triggered when Enter key is pressed.

Input.TextArea Props:

  • allowClear: It is used to remove input content with a clear icon if it is set to true.
  • autoSize: It is used to denote the Height autosize feature.
  • bordered: It is used to indicate whether it has border style or not.
  • defaultValue: It is used to denote the initial input content.
  • maxLength: It is used to denote the max length.
  • showCount: It is used to indicate whether show text counts or not.
  • value: It is used to denote the input content value.
  • onPressEnter: It is the callback function that is triggered when Enter key is pressed.
  • onResize: It is the callback function that is triggered when resize happens.

Input.Search Props:

  • enterButton: It is used to indicate whether to show an enter button after an input or not.
  • loading: It is used to indicate whether to show search box with loading effect or not.
  • onSearch: It is the callback function that is triggered when you press enter key or click on the search-icon, clear-icon.

 

Input.Group Props:

  • compact: It is used to indicate whether to use compact style or not.
  • size: It is used to denote the size of the included Input fields.

 Input.Password Props:

  • iconRender: It is used for the custom toggle button.
  • visibilityToggle: It is used to indicate whether to show the toggle button or not.

Input Methods:

  • blur(): This function is used to remove focus.
  • focus(): This function is used to get focus.

Creating React Application And Installing Module:

  • Step 1: Create a React application using the following command:

    npx create-react-app foldername
  • Step 2: After creating your project folder i.e. foldername, move to it using the following command:

    cd foldername
  • Step 3: After creating the ReactJS application, Install the required module using the following command:

    npm install antd

Project Structure: It will look like the following.

Project Structure

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

App.js




import React, { useState } from 'react'
import "antd/dist/antd.css";
import { Input } from 'antd';
  
export default function App() {
  
  const [name, setName] = useState('')
  
  return (
    <div style={{
      display: 'block', width: 700, padding: 30
    }}>
      <h4>ReactJS Ant-Design Input Component</h4>
      <>
        <Input
          placeholder="Enter your Name"
          onChange={(e) => setName(e.target.value)}
        />
      Name: {name}
      </>
    </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://ant.design/components/input/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads