2.0.7 • Published 8 months ago

simple-date-range-calendar v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

React Date Range Calendar

React Date Range Calendar is a flexible and customizable date range calendar component for React applications. This library allows you to easily select date ranges with an intuitive interface, offering several themes out-of-the-box with full support for creating your own themes. You can also customize the appearance directly via props such as borderRadius and width.

Installation

Install the library using npm or yarn:

npm install simple-date-range-calendar
# or
yarn add simple-date-range-calendar

Basic Usage (No Theme)

If you prefer not to use a theme, you can directly use the Calendar component as shown below. The component will use the default Material UI styles.

import React from 'react';
import { Calendar } from 'simple-date-range-calendar';

const App = () => {
  return (
    <div>
      <Calendar startDate={new Date()} endDate={new Date()} />
    </div>
  );
};

export default App;

Usage with Theme

The calendar component supports optional theming via the ThemeProvider from Material UI. Here's how to use the Calendar component with theming:

import React from 'react';
import { ThemeProvider } from '@mui/material';
import { Calendar, lightTheme } from 'simple-date-range-calendar';

const App = () => {
  return (
    <ThemeProvider theme={lightTheme}>
      <Calendar startDate={new Date()} endDate={new Date()} />
    </ThemeProvider>
  );
};

export default App;

Props

The Calendar component accepts the following props:

PropTypeDescription
startDateDate \| nullOptional. The starting date of the selected range. Defaults to null.
endDateDate \| nullOptional. The ending date of the selected range. Defaults to null.
onDateRangeChange(startDate: Date \| null, endDate: Date \| null) => voidOptional. Callback triggered whenever the date range changes.
onDateRangeIsSelected(startDate: Date, endDate: Date) => voidOptional. Callback triggered when the date range is fully selected.
stylesobjectOptional. Custom styles like borderRadius and width.
styles.borderRadiusnumberOptional. Border radius of the calendar. Defaults to 25.
styles.widthnumber \| stringOptional. Width of the calendar. Defaults to 280px.

Example of Custom borderRadius and width

You can customize the appearance by passing a styles prop to modify the borderRadius and width of the calendar:

import React from 'react';
import { Calendar } from 'simple-date-range-calendar';

const App = () => {
  return (
    <Calendar
      startDate={new Date()}
      endDate={new Date()}
      styles={{ borderRadius: 15, width: 350 }}
    />
  );
};

export default App;

Themes

The library provides several themes out-of-the-box. A theme in this library uses the standard Material UI theming system.

Available Default Themes

You can use any of the following default themes:

  • Pink Theme
  • Royal Blue Theme
  • Green Theme
  • Light Theme
  • Dark Theme

Using Default Themes

Here’s how to use one of the default themes with the calendar:

import React from 'react';
import { ThemeProvider } from '@mui/material';
import { Calendar, royalBlueTheme } from 'simple-date-range-calendar';

const App = () => {
  return (
    <ThemeProvider theme={royalBlueTheme}>
      <Calendar startDate={new Date()} endDate={new Date()} />
    </ThemeProvider>
  );
};

export default App;

Extending an Existing Theme

You can extend any of the predefined themes by using Material UI's createTheme and deepmerge utilities. Here’s an example of how to extend the Royal Blue Theme and override just the background and text colors:

import { createTheme } from '@mui/material';
import { deepmerge, royalBlueTheme } from '@mui/utils';

const customTheme = createTheme(
  deepmerge(royalBlueTheme, {
    palette: {
      background: {
        default: '#f0f0f0', // New default background color
        paper: '#ffffff', // New paper background color
      },
      text: {
        primary: '#123456', // New primary text color
      },
    },
  }),
);

Full Example with Theme Selector

Here is a full example that includes multiple themes and allows you to switch between them dynamically:

import { useState } from 'react';
import { Container, Typography, Box, ThemeProvider } from '@mui/material';
import {
  Calendar,
  darkTheme,
  greenTheme,
  lightTheme,
  pinkTheme,
  royalBlueTheme,
} from 'simple-date-range-calendar';
import { addDays } from 'date-fns';

const themes = [
  { name: 'Pink', theme: pinkTheme },
  { name: 'Royal Blue', theme: royalBlueTheme },
  { name: 'Green', theme: greenTheme },
  { name: 'Light', theme: lightTheme },
  { name: 'Dark', theme: darkTheme },
];

const App = () => {
  const [themeIndex, setThemeIndex] = useState(0);
  const currentTheme = themes[themeIndex].theme;

  return (
    <ThemeProvider theme={currentTheme}>
      <Box
        sx={{
          minHeight: '100vh',
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: currentTheme.palette.background.default,
        }}
      >
        <Typography variant="h2" color={currentTheme.palette.text.secondary}>
          {themes[themeIndex].name}
        </Typography>
        <Container maxWidth="sm" sx={{ textAlign: 'center', mt: 4 }}>
          <Calendar startDate={addDays(new Date(), -15)} endDate={new Date()} />
        </Container>
      </Box>
    </ThemeProvider>
  );
};

export default App;

License

This project is licensed under the MIT License. See the LICENSE file for more details.

2.0.7

8 months ago

2.0.3

9 months ago

2.0.2

9 months ago

2.0.5

9 months ago

2.0.4

9 months ago

2.0.6

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago