DEV Community

Cover image for How to create a beautiful datepicker using Contrast in React.
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to create a beautiful datepicker using Contrast in React.

Date picker is a plugin that adds the function of selecting time without the necessity of using custom JavaScript code. This can be accomplished by using contrast which also enables you to use whatever style you wish.
Contrast also known as CDBReact is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites, and web apps.

Prerequisites

The datepicker and timepicker would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:

  • JavaScript knowledge
  • Basic React Knowledge
  • Basic Bootstrap knowledge
  • NPM installed

The datapicker we are going to build will look like this
DatePicker

Setup

First Check that you have node installed. To do this, run the following command in your terminal.

Code

node-v
Enter fullscreen mode Exit fullscreen mode

This should show you the current version of node you have installed on your machine.
If you don’t have nodejs installed, download it here.
Installing node also installs npm on your PC but you can still confirm using npm-v. Now that we have node installed, we can start up our React project by going to the directory of our choice and running

Code:

npx create-react-app datepicker-app
Enter fullscreen mode Exit fullscreen mode

I named the project datepicker-app but you can use any name of your choice.

Install CDBReact-Pro
Now, we have to install CDBReact in our project. In order to use the datepicker you need to download the pro version here. After the download you can now run the following command to install CBDReact.

npm install --save ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Datepicker

Let us go ahead to create a file named datepicker.js which would contain our datepicker component. Import the various datepicker components that we’ll be using.

Code:

import React, { useState } from "react";
import { CDBDatePicker, CDBContainer } from "cdbreact-pro";
Enter fullscreen mode Exit fullscreen mode

in the code above, we imported the date picker component from contrast.
Let us go ahead and write the code for the body of the datepicker.

Code

const DatePicker = () => {
  const [value, onChange] = useState(new Date());

  return (
    <CDBContainer>
          <CDBDatePicker onChange={onChange} value={value} />
    </CDBContainer>
  );
};
export default DatePicker;
Enter fullscreen mode Exit fullscreen mode

The code above shows the datepicker body code and functions with styling. We can now go ahead to render the code in our app.js

Code:

import './App.css';
import DatePicker from './datepicker.js';
import { BrowserRouter as Router } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="App">
       <Datepicker />
      </div>
    </Router>
  );
}
export default App;
Enter fullscreen mode Exit fullscreen mode

In the above code, it renders the datepicker on the app.js which shows on the web. Your work should look like this.

DatePicker

TIME PICKER

TimePicker

Code:

npx create-react-app timepicker-app
Enter fullscreen mode Exit fullscreen mode

I named the project timepicker-app but you can use any name of your choice.

Install CDBReact:
Now, we have to install CDBReact in our project. In order to use the timepicker you need to download the pro version here. After the download you can now run the following command to install CBDReact

npm install --save ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Timepicker

Bootstrap material time picker is a component which uses a dialog to select a single time in hours: minutes format. The selected time is indicated by the filled circle at the end of the clock hand.
Let us go ahead to create a file named timepicker.js which would contain our timepicker component. Import the various timepicker components that we’ll be using.

Code

import React from "react";
import { CDBTimePicker, CDBContainer } from "cdbreact-pro";
Enter fullscreen mode Exit fullscreen mode

In the code above, we imported the timepicker components

Code:

const TimePicker = () => {
  return (
    <CDBContainer>
        <CDBTimePicker placeholder="10:05 AM" theme="classic" colorPalette="dark" />
    </CDBContainer>
  );
};
export default TimePicker;
Enter fullscreen mode Exit fullscreen mode

The code above shows the function of telling and setting time.
We can go ahead the render this timepicker on our app.js

Code:

import './App.css';
import TimePicker from './timepicker.js';
import { BrowserRouter as Router } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="App">
        <TimePicker  />
      </div>
    </Router>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

TimePicker

Conclusion

TimePicker and Datepicker are simple to include in your project using contrast and allows for better styling features. You can also check out some other features you can use in CDBReact Datepicker Docs and CDBReact TimePicker Docs.

Create Stunning Websites and Web Apps

Building different custom components in react for your web apps or websites can get very stressful. Thats why we decided to build contrast. We have put together a UI kit with over 10000+ components, 5 admin dashboards and 23 additional different pages template for building almost any type of web app or webpage into a single product called Contrast Pro. Try contrast pro!

Contrast Design Boostrap

Contrast React Bootstrap PRO is a Multipurpose pro template, UI kit to build your next landing, admin, SAAS, prelaunch, etc. project with a clean, well documented, well crafted template and UI components. Learn more about Contrast Pro

Resources

Top comments (0)