Chakra UI is a react library to create a website's UI components. Among the provided components, there is a component called `Textarea` which is used to take multiple line text inputs.
Prerequisites
Given below are the different approaches to using the Chakra Textarea component discussed in this article.
Approach 1: Basic Textarea:
The basic textarea provided in Chakra UI has no special behavior. It's like the normal textarea input fields and it's the easiest way to implement the textarea.
Syntax:
function MyForm() {
return (
<Textarea placeholder="Enter your text" />
);
}
Approach 2: Controlled Textarea:
Controlled textarea provides the feature of managing the value inside the textarea through state. That means, you can explicitly update the value of the textarea on each user input.
Syntax:
function MyForm() {
const [value, setValue] = useState('');
const handleChange = (event) => {
setValue(event.target.value);
};
return (
<Textarea value={value}
onChange={handleChange}
placeholder="Enter your text" />
);
}
Approach 3: Resizing the behavior of the textarea:
In this approach of using the textarea, we can provide a `resize` prop to the component to change the behavior of textarea expanding. (horizontal or vertical)
Syntax:
function MyForm() {
return (
<Textarea resize="horizontal/vertical" placeholder="Enter your text" />
);
}
Approach 4: Disabled textarea:
Disabled textarea prevents user input into the textarea field. This is helpful in case of conditional prevention for using the textarea.
Syntax:
function MyForm() {
return (
<Textarea disabled placeholder="Enter your text" />
);
}
Approach 5: Invalid Textarea:
This approach of using the textarea allows us to provide indication on user inputs provided is valid or not.
Syntax:
function MyForm() {
return (
<Textarea isInvalid placeholder="Enter your text" />
);
}
Steps to create a React app and installing the modules:
Step 1: Create a React app and enter into it by using the following commands:
npx create-react-app my-react-app
cd my-react-app
Step 2: Install ChakraUI dependency by using the following command:
npm i @chakra-ui/react
Project Structure:
project structureThe updated dependencies in package.json file will look like:
"dependencies": {
"@chakra-ui/react": "^2.8.2",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: Given below are the examples of different approaches of using a textarea.
JavaScript
// Basic.js
import React,
{
useState
} from 'react';
import {
Textarea,
Button
} from '@chakra-ui/react';
function Basic() {
const [value, setValue] = useState('');
const handleChange = (event) => {
setValue(event.target.value);
};
const handleSubmit = () => {
console.log("Data entered:", value);
};
return (
<>
<p>Basic</p>
<Textarea value={value}
onChange={handleChange}
placeholder="Enter your text" />
<Button color={"white"} padding={"8px"}
border={"none"} background={"teal"}
onClick={handleSubmit}>
Submit
</Button>
</>
);
}
export default Basic;
JavaScript
// Controlled.js
import React,
{
useState
} from 'react';
import {
Textarea,
Button
} from '@chakra-ui/react';
function Controlled() {
const [value, setValue] = useState('');
const handleChange =
(event) => {
setValue(event.target.value);
};
const handleSubmit =
() => {
console.log("Data entered:", value);
};
return (
<>
<p>Controlled</p>
<Textarea value={value} onChange={handleChange}
placeholder="Enter your text" />
<Button color={"white"} padding={"8px"}
border={"none"} background={"green"}
onClick={handleSubmit} >
Submit
</Button>
</>
);
}
export default Controlled;
JavaScript
// Disabled.js
import React from 'react';
import {
Textarea
} from '@chakra-ui/react';
function Disabled() {
return (
<>
<p>Disabled</p>
<Textarea disabled
placeholder="Enter your text" />
</>
);
}
export default Disabled;
JavaScript
// Invalid.js
import React,
{
useState
} from 'react';
import {
Textarea,
Button, Alert
} from '@chakra-ui/react';
function Invalid() {
const [value, setValue] = useState('');
// Assume initially it's invalid
const [isValid, setIsValid] = useState(false);
const handleChange = (event) => {
setValue(event.target.value);
// Perform validation here
// Invalid if the input is empty
setIsValid(event.target.value.length > 0);
};
const handleSubmit =
() => {
if (isValid) {
console.log("Data entered:", value);
} else {
console.log("Form is invalid. Cannot submit.");
}
};
return (
<>
<p>Invalid</p>
<Textarea value={value} onChange={handleChange}
placeholder="Enter your text"
border={
!isValid && "3px solid red"
} borderColor={isValid ? undefined : "red"} />
<Button color={"white"} padding={"8px"}
border={"none"} background={"crimson"}
colorScheme='teal' size='md'
onClick={handleSubmit}>
Submit
</Button>
{!isValid && (
<Alert color={'red'} status="error"
borderRadius="md">
Please enter valid text.
</Alert>
)}
</>
);
}
export default Invalid;
JavaScript
// Resize.js
import React from 'react';
import {
Textarea
} from '@chakra-ui/react';
function Resize() {
return (
<>
<p>Resize Vertical</p>
<Textarea resize="vertical"
placeholder="Enter your text" />
</>
);
}
export default Resize;
JavaScript
// App.js
import logo from './logo.svg';
import './App.css';
import Basic from './components/Basic';
import Controlled from './components/Controlled';
import Disabled from './components/Disabled';
import Invalid from './components/Invalid';
import Resize from './components/Resize';
function App() {
return (
<div className="App" style={{
display: "flex",
flexDirection: "column",
maxWidth: "400px",
gap: "12px",
margin: "auto",
padding: "40px"
}}>
<Basic />
<Resize />
<Controlled />
<Disabled />
<Invalid />
</div>
);
}
export default App;
Step to start your application with the help of the command provided below:
npm start
Output:
Output
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React Projects involve displaying data collections- like user profiles, product catalogs, or tasks- understanding how to work with lists.To render
4 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. the When rendering a list, you need to assign a unique key prop to each element i
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects