Filament Date Range Picker and Filter

repository·x5·Indexed 41 days ago

https://github.com/malzariey/filament-daterangepicker-filter

A native Alpine.js date range picker for Filament providing DateRangePicker for forms and DateRangeFilter for tables. Features include time selection, keyboard input, predefined ranges, and specialized month and year picker modes. Supports custom query logic via modifyQueryUsing and dual state mode for separate start and end date properties.

Tokens
3.4K
Snippets
15
Records
17
Agent score
50%

What's inside Filament Date Range Picker

  1. Install the Filament Date Range Picker and Filter

    x5

    Install the package via Composer to add date range selection capabilities to your Filament project.

    composer require malzariey/filament-daterangepicker-filter

    (Optional) To customize the translation strings, publish the vendor translations:

    php artisan vendor:publish --tag="filament-daterangepicker-filter-translations"
  2. Migrate from v3.x/v4.x to current version

    x5

    Breaking Changes

    • jQuery/Moment.js removed: The component now uses Alpine.js and Day.js.
    • Format tokens: Use ->format() with PHP Carbon tokens (e.g., d/m/Y). Do not use Day.js tokens manually; they are auto-converted.

    Deprecated Methods (Use replacements instead)

    Deprecated MethodReplacement
    displayFormat()format()
    setTimePickerOption()timePicker()
    setTimePickerIncrementOption()timePickerIncrement()
    setAutoApplyOption()autoApply()
    setLinkedCalendarsOption()linkedCalendars()
    getTimePickerIncrementOption()timePickerIncrement()
  3. Use DateRangePicker as a Field

    x5

    Use the DateRangePicker class to add a date range selection field to your Filament forms.

    use Malzarieyilament-daterangepicker-filter\Fields\DateRangePicker;
    
    DateRangePicker::make('created_at'),
    use Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;
    
    DateRangePicker::make('created_at'),
  4. Enable Keyboard Input and Teleportation

    x5

    Keyboard Input

    Use allowInput() to allow users to type dates directly. The component validates the format on Enter. Invalid partial input will not overwrite a previous valid selection unless the field is cleared.

    Teleportation

    To avoid z-index issues in Modals or Slide-overs, the dropdown teleports to the <body> by default. You can control this with teleport():

    DateRangePicker::make('dates')
        ->teleport();      // Enabled by default
    
    DateRangePicker::make('dates')
        ->teleport(false); // Disable teleportation
    DateRangePicker::make('dates')
        ->allowInput();
    
    DateRangePicker::make('dates')
        ->teleport(false);
  5. Listen to Date Picker Custom Events

    x5

    The component dispatches browser events that can be intercepted via JavaScript or Livewire:

    EventFired When
    apply.daterangepickerUser clicks Apply or autoApply triggers
    cancel.daterangepickerUser clicks Cancel or presses Escape
    show.daterangepickerPicker dropdown opens
    hide.daterangepickerPicker dropdown closes

    Example usage:

    document.addEventListener('apply.daterangepicker', (e) => {
        console.log('Start:', e.detail.startDate);
        console.log('End:', e.detail.endDate);
        console.log('Formatted:', e.detail.formattedValue);
    });
  6. Configure Month Picker mode

    x5

    Enable monthPicker() mode to allow users to select entire months instead of specific days. This mode includes a compact numeric year input in the header for quick navigation.

    When using monthPicker(), you can also enable keyboard input via allowInput() and specify a format. For example, 06/2026 - 08/2026 is a valid input format for month ranges.

    Constraints like minYear(), maxYear(), minDate(), and maxDate() are respected in this mode.

    use Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;
    
    DateRangePicker::make('billing_period')
        ->monthPicker()
        ->singleCalendar()
        ->allowInput()
        ->format('m/Y');
  7. Use Dual State Mode for separate properties

    x5

    If you need to store the start and end dates in separate Livewire properties instead of a single range string, use useDualState().

    DateRangePicker::make('date_range')
        ->useDualState('start_date', 'end_date');
    
    // In your Livewire component:
    public ?string $start_date = null;
    public ?string $end_date = null;
    DateRangePicker::make('date_range')
        ->useDualState('start_date', 'end_date');
  8. Customize DateRangeFilter queries

    x5

    When using DateRangeFilter in a table, you can customize how the selected range affects the database query using modifyQueryUsing().

    use Malzariey\FilamentDaterangepickerFilter\Filters\DateRangeFilter;
    
    DateRangeFilter::make('created_at')
        ->modifyQueryUsing(fn(Builder $query, ?Carbon $startDate, ?Carbon $endDate, $dateString) =>
            $query->when(!empty($dateString),
                fn(Builder $query) => $query->whereBetween('created_at', [$startDate, $endDate])
            )
        );

    You can also add a filter indicator using withIndicator().

  9. Configure Date Range Picker options

    x5

    The DateRangePicker supports extensive configuration for time, constraints, and display:

    Time Selection

    • timePicker(): Enable time selection.
    • timePicker24(): Use 24-hour format.
    • timePickerSecond(): Show seconds.
    • timePickerIncrement(int $minutes): Set time increments (e.g., 30).

    Constraints and Defaults

    • startDate(Carbon $date) / endDate(Carbon $date): Set initial dates.
    • defaultToday(), defaultLast7Days(), defaultThisMonth(), defaultLastYear(): Use built-in shortcuts.
    • minDate(Carbon $date) / maxDate(Carbon $date): Set date boundaries.
    • minYear(int $year) / maxYear(int $year): Set year boundaries.
    • disabledDates(array $dates): Pass an array of date strings to disable.
    • maxSpan(array $span): Limit the selection span (e.g., ['months' => 1]).

    Display and UI

    • format(string $format): Set the display format using PHP Carbon tokens (e.g., d/m/Y). The component auto-converts this for JavaScript.
    • ranges(array $ranges): Define predefined date ranges (e.g., ['Today' => [now(), now()]]).
    • useRangeLabels(): Display preset labels instead of dates.
    • showWeekNumbers() / showISOWeekNumbers(): Display week numbers.
    • showDropdowns(): Show month and year dropdowns in the header.
    • drops(DropDirection $direction): Set dropdown direction.
    • opens(OpenDirection $direction): Set how the picker opens.
  10. Use DateRangePicker as a Filament Form Field

    x5

    The DateRangePicker class is a Filament field used to allow users to select a range of dates within a form. It stores the selection as a single string, typically formatted with a separator (e.g., 2023-01-01 - 2023-01-31).

    Key features include:

    • Predefined date ranges (e.g., 'Last 7 Days', 'This Month').
    • Custom date formatting and separators.
    • Support for single calendar views or linked calendars.
    • Constraints like minDate, maxDate, and disabledDates.
    use Malzariey\FilamentDaterangepickerFilter\Fields\DateRangePicker;
    
    DateRangePicker::make('appointment_range')