Open In App

React.js Blueprint DateInput2

In this article, we will learn about the DateInput2 component offered by the blueprint.js library. BlueprintJS is a React-based UI toolkit for the web. It is written in Typescript. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications that run in a modern web browser.

The DateInput2 component renders an InputGroup component that shows a DatePicker component on focus, inside a Popover2 component. It also optionally (depending on the props passed) displays a TimezoneSelect component on the right side of the InputGroup, allowing the user to change the timezone of the selected date.



DateInput2 props:

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 @blueprintjs/core
npm install @blueprint/datetime @blueprintjs/datetime2

Project Structure: It will look like the following.

 

Example 1: In this example, we will try to create a simple date-picker application that allows us to select date and time using the DateInput2 component provided by BlueprintJS.

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




import { DateInput2 } from "@blueprintjs/datetime2";
import { useCallback, useState } from "react";
import '@blueprintjs/datetime/lib/css/blueprint-datetime.css';
import '@blueprintjs/core/lib/css/blueprint.css';
  
export default function App() {
    const [dateValue, setDateValue] = useState(null);
    const handleChange = useCallback(setDateValue, []);
    const formatDate = useCallback((date) => date.toLocaleString());
    const parseDate = useCallback((str) => new Date(str));
  
    return (
        <div style={{
            display: 'block', width: 400, padding: 30
        }}>
            <h3>ReactJS blueprint DateInput2 component</h3>
            <DateInput2
                formatDate={formatDate}
                onChange={handleChange}
                parseDate={parseDate}
                placeholder="M/D/YYYY"
                value={dateValue}
            />
        </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: In this example, we will put a timezone select dropdown as well. Also, for this example, as we are using the TimePrecision object, please install the @blueprint/datetime library as well.




import { DateInput2 } from "@blueprintjs/datetime2";
import { useCallback, useState } from "react";
import { TimePrecision } from "@blueprintjs/datetime";
import '@blueprintjs/datetime/lib/css/blueprint-datetime.css';
import '@blueprintjs/core/lib/css/blueprint.css';
import { Tag } from "@blueprintjs/core";
  
export default function App() {
    const [dateValue, setDateValue] = useState(null);
    const handleChange = (date) => {
        setDateValue(date)
    }
    const formatDate = useCallback((date) => date.toLocaleString());
    const parseDate = useCallback((str) => new Date(str));
  
    return (
        <div style={{
            display: 'block', width: 400, padding: 30
        }}>
            <h3>ReactJS blueprint DateInput2 component</h3>
            <DateInput2
                formatDate={formatDate}
                onChange={handleChange}
                parseDate={parseDate}
                placeholder="M/D/YYYY"
                value={dateValue}
                showTimezoneSelect={true}
                timePrecision={TimePrecision.MINUTE}
            />
            {dateValue == null ?
                <Tag minimal={true}>no date</Tag> :
                <Tag intent="primary">{dateValue}</Tag>}
        </div>
    );
}

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

npm start

Output:

 

Reference: https://blueprintjs.com/docs/#datetime2/date-input2


Article Tags :