malzariey/filament-daterangepicker-filter

This package uses daterangepciker library to filter date by a range or predefined date ranges (Today , Yesterday ...etc)

Fund package maintenance!
Malzariey

Installs: 233 159

Dependents: 9

Suggesters: 0

Security: 0

Stars: 101

Watchers: 3

Forks: 45

Open Issues: 7

Language:JavaScript

3.0.5 2024-10-01 14:57 UTC

README

Latest Version on Packagist Total Downloads

This package that adds a date range picker and filter to the Filament panel using the daterangepicker library. It provides options to filter dates by a specific range or predefined ranges such as "Today", "Yesterday", etc.

Installation

You can install the package via composer:

composer require malzariey/filament-daterangepicker-filter

You can publish the config file with:

Optionally, you can publish the views using

php artisan vendor:publish --tag="filament-daterangepicker-filter-views"

Usage

As a Field

use Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;

DateRangePicker::make('created_at'),

As a Filter

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at'),

Options

Timezone

Set the picker timezone, defaults to the project timezone. Example setting timezone to 'UTC'.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->timezone('UTC')

Start and End Dates

You can specify initial selected Start and End Dates for the filter. The following example will initialize the filter to today's date.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->startDate(Carbon::now())->endDate(Carbon::now())

Default Today

You could also use a shortcut for above using the following

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->defaultToday()

Min and Max Dates

Specify the minimum and maximum dates for the calendar. The following example will only enable selecting previous month to next month.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->minDate(Carbon::now()->subMonth())->maxDate(Carbon::now()->addMonth())

First Day of Week

Set Monday as the first day of the week on the calendar of your DateRangeFilter.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->firstDayOfWeek(1)

Always Show Calendar

Normally, if you use the ranges option to specify pre-defined date ranges, calendars for choosing a custom date range are not shown until the user clicks "Custom Range". When this option is set to true, the calendars for choosing a custom date range.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->alwaysShowCalendar()

Time Picker

Adds select boxes to choose times in addition to dates.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->timePicker()

Show Time Picker Seconds

Show seconds in the timePicker.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->timePickerSecond()

Time Picker 24-Hour Format

Use 24-hour instead of 12-hour times, removing the AM/PM selection

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->timePicker24()

Time Picker Increment Option

Increment of the minutes selection list for times (i.e. 30 to allow only selection of times ending in 0 or 30).

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->timePickerIncrement(30)

Auto Apply

Hide the apply and cancel buttons, and automatically apply a new date range as soon as two dates are clicked.

Note: Does not work with timePicker option.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->autoApply()

Linked Calendars

When enabled, the two calendars displayed will always be for two sequential months (i.e. January and February), and both will be advanced when clicking the left or right arrows above the calendars. When disabled, the two calendars can be individually advanced and display any month/year

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->linkedCalendars()

Single Calendar

Show only a single calendar to choose one date, instead of a range picker with two calendars. Used Only with DateRangePicker.

use Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;

DateRangePicker::make('created_at')->singleCalendar()

Disabled Dates

Indicate whether that date should be available for selection or not.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->disabledDates(['array of Dates'])

Display and Picker Formats

Specify the format for the display and selection of dates.

DateRangeFilter::make('created_at')
//Picker Date Display Format in (Javascript Date Format)
->displayFormat('date format')
//Carbon Format reading from the Javascript displayFormat in (PHP Date Format)
->format('date format')

Customize Query

Apply a custom filter query.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;

DateRangeFilter::make('created_at')
    ->modifyQueryUsing(fn(Builder $query, ?Carbon $startDate , ?Carbon $endDate , $dateString) =>
        $query->when(!empty($dateString),
            fn (Builder $query, $date) : Builder =>
                $query->whereBetween('created_at', [$startDate->subDays(3),$endDate]))
    )

Filter Indicator

Show an indicator when the filter is active.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->withIndicator()

Predefined Ranges

Customize the predefine date ranges for quick selection.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')
->ranges(['Last 3 days' => [now()->subDays(3), now()]])

Use Range Labels

By using the useRangeLabels function, it enables the field to display the predefined range labels instead of actual date ranges. This can simplify the display and make it more user-friendly.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->useRangeLabels()

Disabling Custom Range Selection

If you want the users to only choose from the predefined ranges and prevent them from selecting custom ranges, you can use the disableCustomRange option.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->disableCustomRange()

Separator

Specify the separator for the date range.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->separator(' - ')

Drops Position

Specify the location the filter menu should drop at.

Options

DropDirection::DOWN : The picker will appear below field. (Default)

DropDirection::AUTO : Auto decide the location.

DropDirection::UP : The picker will appear above field.

Example
use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use Malzariey\FilamentDaterangepickerFilter\Enums\DropDirection;

DateRangeFilter::make('created_at')->drops(DropDirection::AUTO)

Open Position

Specify the location the filter menu should open to.

Options

OpenDirection::LEFT : The picker will appear left to the field. (Default)

OpenDirection::RIGHT : The picker will appear right to the field.

OpenDirection::CENTER : The picker will appear center of the field.

Example
use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use Malzariey\FilamentDaterangepickerFilter\Enums\OpenDirection;

DateRangeFilter::make('created_at')->opens(OpenDirection::LEFT)

Clearing the Date Range Picker

If you have selected a date range and want to remove it, simply click on the calendar icon within the field.

To disable this behavior, you can use the disableClear() method. This will prevent the user from clearing the selected date range.

To customize the icon, you can use the icon() method. It accepts a string or Closure.

use \Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use \Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;

DateRangeFilter::make('created_at')->disableClear()
DateRangePicker::make('created_at')->disableClear()

DateRangeFilter::make('created_at')->icon('heroicons-backspace')
DateRangePicker::make('created_at')->icon('heroicons-backspace')

Disabling Predefined Ranges

If you wish to disable the predefined ranges feature and provide users with a custom selection only, you may use the disableRanges() method. This will remove any preset date ranges from the picker.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->disableRanges()

Max Span

The maximum span between the selected start and end dates.

use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;

DateRangeFilter::make('created_at')->maxSpan(['months' => 1]), // days, months or years

Display week numbers

Show localized or ISO week numbers at the start of each week on the calendars.

use \Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use \Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;

DateRangeFilter::make('created_at')->showWeekNumbers() // Localized
DateRangePicker::make('created_at')->showISOWeekNumbers()

Display month and year dropdowns

Show month and year select boxes above calendars to jump to a specific month and year. You can also customize, minimum and maximum year shown in the dropdowns.

use \Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
use \Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;

DateRangeFilter::make('created_at')->showDropdowns()->minYear(2000)->maxYear(2030)
DateRangePicker::make('created_at')->showDropdowns()->minYear(2000)->maxYear(2030)

Screenshots

Light mode

DateRangePicker Widget

Dark mode

DateRangePicker Widget

Styling

If you're building a custom Filament theme, you need one more step to make the calendar theme match your custom theme.

Add this line to your resources/css/{panel_name}/theme.css file.

@import '/vendor/malzariey/filament-daterangepicker-filter/resources/css/filament-daterangepicker.css';

Credits

License

The MIT License (MIT). Please see License File for more information.

Acknowledgements

  • This project makes use of the fantastic Date Range Picker for Bootstrap by Dan Grossman.
  • Special thanks to JetBrains, whose support to open-source projects has been tremendously valuable for our project's progress and improvement. Through their Open Source Support Program, JetBrains has generously provided us with free licenses to their high-quality professional developer tools, including IntelliJ IDEA and PhpStorm. These tools have greatly improved our productivity and made it easier to maintain high quality code. JetBrains has demonstrated a strong commitment to assisting the open source community, making a significant contribution to promoting open-source software and collaboration. We wholeheartedly thank JetBrains for their support and for having us in their open-source project support program.

JetBrains Logo