react-native-ui-datepicker

repository·main·Indexed 21 days ago

https://github.com/farhoudshapouran/react-native-ui-datepicker

A highly customizable, lightweight DateTimePicker component for React Native supporting single, range, and multiple date selection modes. It features compatibility with NativeWind, support for multiple calendar systems (Gregory and Jalali), IANA time zone configuration, and localization options for locales and numeral systems. The library allows for deep UI customization through a styles prop, classNames for Tailwind CSS, and the ability to replace default calendar elements with custom components.

Tokens
11.8K
Snippets
34
Records
42
Agent score
74%

What's inside react-native-ui-datepicker

  1. Basic Usage of DateTimePicker

    main

    To use the component, import DateTimePicker, DateType, and useDefaultStyles. You must specify a mode (single, range, or multiple) and provide an onChange handler to manage the selected date state.

    import { useState } from  'react';
    import DateTimePicker, { DateType, useDefaultStyles } from 'react-native-ui-datepicker';
    
    export function Calendar() {
      const defaultStyles = useDefaultStyles();
      const [selected, setSelected] = useState<DateType>();
    
      return (
        <DateTimePicker
          mode="single"
          date={selected}
          onChange={({ date }) =>  setSelected(date)}
          styles={defaultStyles}
        />
      );
    }
  2. Implement Custom Components in DateTimePicker

    main

    Use the components prop to replace default calendar elements with your own custom components. This allows for deep UI customization of days, months, years, and more.

    To implement this, create a CalendarComponents object containing your custom components and pass it to the DateTimePicker.

    import DateTimePicker, {
      CalendarDay,
      CalendarMonth,
      CalendarComponents,
    } from 'react-native-ui-datepicker';
    
    const components: CalendarComponents = {
      Day: (day: CalendarDay) => <YourCustomDay day={day} />,
      Month: (month: CalendarMonth) => <YourCustomMonth month={month} />
      // etc
    };
    
    export function Calendar() {
      return (
          <DateTimePicker
            components={components}
          />
      );
    }
  3. Handle Jalali calendar support and locales

    main

    The library provides built-in support for the Jalali (Persian) calendar. It includes specific month names for both English (en) and Persian (fa) locales. Use isValidJalaliLocale to verify if a locale is supported for Jalali features.

    import { isValidJalaliLocale } from 'react-native-ui-datepicker/src/utils';
    
    const isSupported = isValidJalaliLocale('fa'); // true
  4. Style with NativeWind (Tailwind CSS)

    main

    If your project uses NativeWind, use the classNames prop to apply Tailwind CSS classes. Use useDefaultClassNames() to get the base classes and override them as needed.

    import DateTimePicker, { useDefaultClassNames } from 'react-native-ui-datepicker';
    
    export function Calendar() {
      const defaultClassNames = useDefaultClassNames();
    
      return (
        <DateTimePicker
          classNames={{
            ...defaultClassNames,
            today: 'border-amber-500',
            selected: 'bg-amber-500 border-amber-500',
            selected_label: "text-white",
            day: `${defaultClassNames.day} hover:bg-amber-100`,
            disabled: 'opacity-50',
          }}
        />
      );
    }
  5. Apply Custom Styles to DateTimePicker

    main

    You can customize the component's appearance using the styles prop. It is recommended to use useDefaultStyles() and spread it into your custom object to ensure you only override specific parts. Styles are mapped to the UI Theme enums.

    import DateTimePicker, { useDefaultStyles } from 'react-native-ui-datepicker';
    
    export function Calendar() {
      const defaultStyles = useDefaultStyles();
    
      return (
        <DateTimePicker
          styles={{
            ...defaultStyles,
            today: { borderColor: 'blue', borderWidth: 1 }, // Add a border to today's date
            selected: { backgroundColor: 'blue' }, // Highlight the selected day
            selected_label: { color: 'white' }, // Highlight the selected day label
          }}
        />
      );
    }
  6. Configure DateTimePicker calendar and localization

    main

    You can customize the calendar system, locale, and numbering system using the following props:

    • calendar: The calendar system to use. Supports 'gregory' (default) and 'jalali' (Persian).
    • locale: The locale string (e.g., 'en', 'fa') used for formatting.
    • numerals: The numbering system (e.g., 'latn' for Latin/Western digits, or others supported by dayjs).
    • timeZone: Sets the timezone for date calculations. When the timeZone changes, the component updates the current date to the new timezone.
    • firstDayOfWeek: An integer (0-6) representing the first day of the week (0 is typically Sunday).
  7. Configure Single Mode props

    main

    When mode="single" is used, these props control the single date selection and time picker behavior:

    | Name         | Type               | Description                                                   |
    | ------------ | ------------------ | ------------------------------------------------------------- |
    | `date`       | `DateType`         | Specifies the currently selected date.                        |
    | `onChange`   | `({date}) => void` | Callback function triggered when the date change.             |
    | `timePicker` | `boolean`          | Whether to enable the time picker.                          |
    | `use12Hours` | `boolean`          | Whether to use a 12-hour format (AM/PM) in the time picker.  |