Open In App

React Suite DatePicker Usage Disabled and read only

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application.

In this article, we’ll learn about React suite DatePicker Disabled and Readonly. DatePicker is used to quickly select a date or time. The DatePicker can be disabled and can also be used as a read-only component.

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 @mui/material

Project Structure: It will look like the following.

 

Example 1: Below example demonstrates the various types of disabled DatePicker components. 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 "rsuite/dist/rsuite.min.css";
import { DatePicker } from "rsuite";
import isBefore from 'date-fns/isBefore';
  
export default function App() {
    return (
        <div>
            <div style={{ textAlign: "center" }}>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite DatePicker Disabled and Readonly
                </h4>
            </div>
            <div style={{ padding: 20, textAlign: "center" }}>
                <div>
                    <label>Disabled: </label>
                    <DatePicker disabled style={{ width: 200 }} />
                    <br />
                    <label>Disabled date: </label>
                    <DatePicker disabledDate={date => 
                        isBefore(date, new Date())} 
                        style={{ width: 200 }} />
                    <br />
                    <label>Disabled month: </label>
                    <DatePicker
                        disabledDate={date => 
                            isBefore(date, new Date())}
                        format="yyyy-MM"
                        style={{ width: 200 }}
                    />
                    <br />
                    <label>Disabled time: </label>
                    <DatePicker
                        format="HH:mm:ss"
                        ranges={[]}
                        defaultValue={new Date
                            ('2017-12-12 09:15:30')}
                        disabledHours=
                            {hour => hour < 8 || hour > 18}
                        disabledMinutes={minute => 
                            minute % 15 !== 0}
                        disabledSeconds={second => 
                            second % 30 !== 0}
                        style={{ width: 200 }}
                    />
                </div>
            </div>
        </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:

 

Example 2: Below example demonstrates the read-only DaterPicker component. Change your App.js like the one below.

App.js




import "rsuite/dist/rsuite.min.css";
import { DatePicker } from "rsuite";
  
export default function App() {
    return (
        <div>
            <div style={{ textAlign: "center" }}>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite DatePicker Disabled and Readonly
                </h4>
            </div>
            <div style={{ padding: 20, textAlign: "center" }}>
                <div>
                    <label>Read only: </label>
                    <DatePicker readOnly defaultValue=
                        {new Date()} style={{ width: 200 }} />
                </div>
            </div>
        </div>
    );
}


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

npm start

Output:

 

Reference: https://rsuitejs.com/components/date-picker/#disabled-and-read-only



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

Similar Reads