Design Converter
Education
Software Development Executive - I
Last updated on Nov 3, 2023
Last updated on Oct 11, 2023
In React development, the need for a robust and user-friendly date picker component is ubiquitous. Among the myriad options available, one stands out due to its lightweight nature and seamless integration with Chakra UI - the @orange_digital/chakra-datepicker.
To begin with, we need to add the Chakra UI datepicker to our project. This can be achieved using either yarn or npm. Here's how you can do it:
1$ yarn add @orange_digital/chakra-datepicker 2# or 3$ npm install --save @orange_digital/chakra-datepicker
With the package installed, the next step is to import the datepicker into your React component. Here's an example of how to import and use the datepicker:
1import React from 'react'; 2import { DatePicker } from '@orange_digital/chakra-datepicker'; 3 4export const Component: React.FC = () => { 5 return ( 6 <DatePicker initialValue={new Date()} /> 7 ) 8}
In the above code snippet, we import the DatePicker from @orange_digital/chakra-datepicker and use it in our functional component. The initialValue prop sets the initial date to the current date.
One of the main advantages of using the Chakra UI datepicker is the ease of customization. Chakra UI provides a default theme that can be overridden to match your application's styles. Let's dive into the details of how to customize the styles of our datepicker component.
Chakra UI uses a system of style props for CSS in JS. These props are based on common CSS properties but are camel cased. For instance, to change the background color of the datepicker, you can use the backgroundColor prop.
1<DatePicker initialValue={new Date()} backgroundColor="blue.500" />
In the above example, we've changed the background color of the datepicker to a blue shade from Chakra UI's color palette.
Chakra UI allows you to override the default theme styles. This can be done using the extendTheme function from Chakra UI. Here's an example of how to override the styles of the datepicker component:
1import { extendTheme } from "@chakra-ui/react"; 2 3const theme = extendTheme({ 4 components: { 5 DatePicker: { 6 baseStyle: { 7 background: "blue.500", 8 }, 9 }, 10 }, 11}); 12 13// Then pass the theme to the ChakraProvider
In the above code, we've overridden the default background color of the datepicker component to be blue.500.
Implementing a datepicker in your React application can be a breeze with the right tools. The @orange_digital/chakra-datepicker package provides a lightweight and customizable datepicker component that integrates seamlessly with Chakra UI. With the knowledge from this blog post, you should understand how to add, import, and customize the datepicker in your React applications.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.