{{-- A date field with M3's date pickers: docked under the field, modal, or modal text input. `mode` picks M3's three: - `docked` (the default): a text field that takes a typed date in the locale's numeric format (`13.09.2026` in `de`, `09/13/2026` in `en-US`), with the calendar dropping open under it — on a press of the field, its calendar button, or ArrowDown. On a compact window (below 600px, `medium`) the calendar opens as the modal picker instead, where a docked one would not fit. - `modal`: the field only shows the date; pressing it (or Enter, Space, ArrowDown) opens the calendar in a dialog, with a pencil to switch to typing. - `input`: the same dialog, opened on its text field, with a calendar icon to switch back. A `range` picker on a compact window (below `medium`) is M3's full-screen range picker instead of the 360px dialog: an app bar with a close button and **Save**, the range as the headline under the supporting text, and the months in one vertically scrolling list rather than stepped one at a time. Nothing changes for a single date, or for a docked picker at `medium` and up. The calendar and the dialog's text field pick a draft; OK (or Enter on a day) makes it the value, Cancel or Escape leaves the value alone and returns focus to the field. What is typed into the docked field is the value as soon as it is a whole, allowed date. `wire:model` stores `Y-m-d` strings (`x-model` without Livewire). `range` picks a start and an end, bound as one array, `['start' => 'Y-m-d', 'end' => 'Y-m-d']` (either may be null) — one property rather than two, because a `wire:model` names one property, Livewire sends a range as one update so `after_or_equal:period.start` validates against the end it came with, and the errors for `period`, `period.start` and `period.end` all belong to this field. `min` and `max` (`Y-m-d` or a date object) disable the days outside them and keep the keyboard inside them. `label`, `hint`, `icon`, `variant` (`outlined`, `filled`) and `size` are the field's; `clearable` adds a button that empties it (a date, or both ends of a range) once it holds one; `name` adds hidden inputs carrying `Y-m-d` for a plain form post, and every other attribute (`required`, `disabled`, `readonly`) reaches the text field. Errors under the `wire:model` name replace the hint, and so does a typed date that cannot be read. Month and weekday names, the first day of the week and the typed format come from `Intl` for `app()->getLocale()` (resources/js/datepicker.js). An application that lets each person choose overrides the last two: `week-start` is the first day of the week, 0 (Sunday) to 6 (Saturday), and `format` the typed and displayed format, `dd`, `MM` and `yyyy` in any order around one delimiter (`.`, `/` or `-`: `dd.MM.yyyy`, `MM/dd/yyyy`, `yyyy-MM-dd`). The field, the typed-date reader, the calendar's columns and weekday header, Home and End, and the dialog's text fields all follow them; the names stay the locale's, and `wire:model` still stores `Y-m-d`. A value that is neither (`week-start="7"`, `format="d.M.yy"`) is ignored, as `null` is. Replaces ReStride's flatpickr picker: its `config` becomes `min`, `max`, `range` and `mode`. M3's date pickers (DatePickerModalTokens and DateInputModalTokens from androidx Compose Material 3, androidx commit 27cf9a7d5788aa0f5f2d8b6699ce279560daf326, with the layout of DatePicker.kt, DateRangePicker.kt and DateInput.kt; the docked picker's values from material-web's md-comp-date-picker-docked tokens, v0_192, as Compose has none; all Apache-2.0): surface-container-high at elevation 3, 360px wide, the extra-large corner (modal) or large (docked), 40px days in 48px cells, today outlined in primary, the chosen day in primary and a range's middle in secondary-container. The styles are resources/css/components/datepicker.css; the year, the menu buttons and the list options render `md-state-layer md-focus-ring`, the month's announcement `md-visually-hidden`, and nothing else here writes a class. --}} @props([ 'label' => null, 'hint' => null, 'icon' => null, 'size' => 'md', 'variant' => null, 'mode' => 'docked', 'range' => false, 'min' => null, 'max' => null, 'value' => null, 'clearable' => false, 'weekStart' => null, 'format' => null, ]) @php $model = $attributes->wire('model')->value() ?: null; $mode = in_array($mode, ['docked', 'modal', 'input'], true) ? $mode : 'docked'; $id = $attributes->get('id') ?? 'field-'.substr(md5($model.'|'.$label.'|datepicker'), 0, 12); $anchor = '--material-datepicker-'.preg_replace('/[^A-Za-z0-9_-]/', '-', $id); // A plain form's field is named, not bound: its errors are under its name (`files[]` → `files`, `a[b]` → `a.b`). $errorKey = $model ?? (filled($attributes->get('name')) ? str_replace(['[]', '[', ']'], ['', '.', ''], (string) $attributes->get('name')) : null); $messages = $errorKey !== null && isset($errors) ? array_values(array_unique(\Illuminate\Support\Arr::flatten([$errors->get($errorKey), $errors->get($errorKey.'.*')]))) : []; $locale = str_replace('_', '-', app()->getLocale()); $weekStart = (is_int($weekStart) || is_string($weekStart)) && preg_match('/^[0-6]\z/', (string) $weekStart) === 1 ? (int) $weekStart : null; $format = is_string($format) && preg_match('/^(dd|MM|yyyy)([.\/-])(dd|MM|yyyy)\2(dd|MM|yyyy)\z/', $format, $units) === 1 && count(array_unique([$units[1], $units[3], $units[4]])) === 3 ? $format : null; $toIso = function (mixed $date): ?string { if ($date instanceof \DateTimeInterface) { return $date->format('Y-m-d'); } if (is_string($date) && preg_match('/^(\d{4})-(\d{2})-(\d{2})(?:$|[T ])/', $date, $match) === 1 && checkdate((int) $match[2], (int) $match[3], (int) $match[1])) { return substr($date, 0, 10); } return null; }; // Rendered as the bound property already says, so the field does not change once Alpine starts. $current = $value; if ($model !== null && ($component = \Livewire\Livewire::current()) !== null) { $current = data_get($component, $model); } $current = $range ? ['start' => $toIso(data_get($current, 'start')), 'end' => $toIso(data_get($current, 'end'))] : $toIso($current); // The typed format: `format`, or as resources/js/datepicker.js derives it, from ICU's short date when PHP has intl. $pattern = $format ?? 'yyyy-MM-dd'; if ($format === null && class_exists(\IntlDateFormatter::class)) { $short = (string) (new \IntlDateFormatter(str_replace('-', '_', $locale), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE))->getPattern(); $candidate = rtrim(str_replace('My', 'M/y', (string) preg_replace(['/[^dMy\/\-.]/', '/d{1,2}/', '/M{1,2}/', '/y{1,4}/'], ['', 'dd', 'MM', 'yyyy'], $short)), '.'); if (preg_match('/^(?=.*dd)(?=.*MM)(?=.*yyyy)[dMy]+[\/\-.][dMy]+[\/\-.][dMy]+$/', $candidate) === 1) { $pattern = $candidate; } } $typed = fn (?string $date): string => $date === null ? '' : strtr($pattern, ['yyyy' => substr($date, 0, 4), 'MM' => substr($date, 5, 2), 'dd' => substr($date, 8, 2)]); $display = $range ? ($current['start'] === null && $current['end'] === null ? '' : $typed($current['start']).' – '.$typed($current['end'])) : $typed($current); $title = $range ? __('Select dates') : __('Select date'); $inputTitle = $range ? __('Enter dates') : __('Select date'); $fieldName = $attributes->get('name'); $invalid = $messages !== []; $config = [ 'locale' => $locale, 'mode' => $mode, 'range' => (bool) $range, 'min' => $toIso($min), 'max' => $toIso($max), 'weekStart' => $weekStart, 'format' => $format, 'disabled' => (bool) $attributes->get('disabled'), 'readonly' => (bool) $attributes->get('readonly'), 'strings' => [ 'selected' => __('Selected date'), 'entered' => __('Entered date'), 'start' => __('Start date'), 'end' => __('End date'), 'pattern' => __('Date does not match expected pattern: :pattern'), 'yearRange' => __('Date out of expected year range :start - :end'), 'notAllowed' => __('Date not allowed: :date'), 'invalidRange' => __('Invalid date range input'), ], ]; $fieldIconSize = ['sm' => 20, 'xs' => 16][$size] ?? 24; @endphp
only(['class', 'wire:key', 'x-model']) }} x-data="materialDatepicker({ @if ($model !== null) value: @entangle($attributes->wire('model')), @else value: @js($current), @endif ...@js($config), })" @if ($model === null) x-modelable="value" @endif x-on:focusout="leave($event)" x-on:pointerdown.outside="open && presentation === 'docked' && cancel(false)" data-md-datepicker >
whereDoesntStartWith('wire:model')->except(['class', 'id', 'wire:key', 'x-model', 'name', 'value', 'placeholder', 'type']) }} x-ref="input" id="{{ $id }}" type="text" role="combobox" aria-haspopup="dialog" aria-controls="{{ $id }}-picker" aria-expanded="false" x-bind:aria-expanded="open.toString()" aria-describedby="{{ $id }}-support" @if ($invalid) aria-invalid="true" @endif x-bind:aria-invalid="(fieldError !== '' || {{ $invalid ? 'true' : 'false' }}) ? 'true' : null" autocomplete="off" value="{{ $display }}" placeholder=" " data-md-field-control x-model="text" @if ($mode === 'docked') x-bind:placeholder="placeholder" x-on:input="typeInField()" x-on:change="commitField()" x-on:keydown.enter="commitField()" x-on:click="show(false)" x-on:keydown.arrow-down.prevent="open ? focusInside() : show()" x-on:keydown.escape="if (open) { $event.preventDefault(); $event.stopPropagation(); cancel(); }" @else readonly x-on:click="show()" x-on:keydown.enter.prevent="show()" x-on:keydown.space.prevent="show()" x-on:keydown.arrow-down.prevent="show()" @endif /> @if ($clearable) @endif

@if ($invalid)
@foreach ($messages as $message)

{{ $message }}

@endforeach
@elseif (filled($hint))

{{ $hint }}

@endif
@if (filled($fieldName)) @if ($range) @else @endif @endif
{{-- M3's full-screen range picker (compact): an app bar with a close button and Save, then the supporting text and the range as the headline, over a divider. --}}

{{ $title }}

{{ $title }}

@if ($range) @endif

{{-- The full-screen picker confirms from its app bar, so it draws no action row. --}}