+
-
+
diff --git a/src/Enums/FlatpickrMode.php b/src/Enums/FlatpickrMode.php
new file mode 100644
index 0000000..c1d16dc
--- /dev/null
+++ b/src/Enums/FlatpickrMode.php
@@ -0,0 +1,11 @@
+';
- public function weekSelect(bool $weekSelect = true): Flatpickr
+ protected ?string $prevArrow = '<';
+
+ protected bool $noCalendar = false;
+
+ protected bool $shorthandCurrentMonth = false;
+
+ protected bool $static = false;
+
+ protected bool $use24hr = false;
+
+ protected bool $weekNumbers = false;
+
+ protected bool $wrap = false;
+
+ protected int $showMonths = 1;
+
+ protected FlatpickrPosition $position = FlatpickrPosition::AUTO;
+
+ protected FlatpickrMonthSelectorType $monthSelectorType = FlatpickrMonthSelectorType::DROPDOWN;
+
+ protected bool $animate = true;
+
+ protected bool $closeOnSelect = true;
+
+ public function getConfig(): array
{
- $this->weekSelect = $weekSelect;
+ if ($this->isRangePicker()) {
+ $this->mode(FlatpickrMode::RANGE);
+ } elseif ($this->isMultiplePicker()) {
+ $this->mode(FlatpickrMode::MULTIPLE);
+ }
+ if ($this->isEnableTime()) {
+ if (! \Str::of($this->getDateFormat())->contains('H', ignoreCase: true)) {
+ $this->dateFormat('Y-m-d H:i:s');
+ }
+ if (! \Str::of($this->getAltFormat())->contains('H', ignoreCase: true)) {
+ $this->altFormat('F j Y H:i K');
+ }
+ }
+ if ($this->isTime()) {
+ $this->mode(FlatpickrMode::TIME);
+ $this->noCalendar();
+ $this->enableTime();
+ if (\Str::of($this->getDateFormat())->contains('Y')) {
+ $this->dateFormat($this->isUse24hr() ? 'H:i' : 'h:i K');
+ }
+ } elseif ($this->isMonthSelect()) {
+ $this->mode(FlatpickrMode::SINGLE);
+ $this->enableTime(false);
+ $this->time(false);
+ $this->range(false);
+ $this->dateFormat('Y-m');
+ $this->altFormat('F J');
+ $this->altInput();
+ } elseif ($this->isWeekSelect()) {
+ $this->mode(FlatpickrMode::SINGLE);
+ $this->enableTime(false);
+ $this->time(false);
+ $this->range(false);
+ $this->dateFormat('W');
+ $this->altFormat('\Week W');
+ $this->altInput();
+ }
+ $config = [
+ 'monthSelect' => $this->monthSelect,
+ 'weekSelect' => $this->weekSelect,
+ 'mode' => $this->mode->value,
+ 'altInput' => $this->altInput,
+ 'altFormat' => $this->altFormat,
+ 'enableTime' => $this->enableTime,
+ 'dateFormat' => $this->dateFormat,
+ 'theme' => $this->theme->value,
+ 'altInputClass' => $this->altInputClass,
+ 'allowInput' => $this->allowInput,
+ 'allowInvalidPreload' => $this->allowInvalidPreload,
+ 'ariaDateFormat' => $this->ariaDateFormat,
+ 'conjunction' => $this->conjunction,
+ 'clickOpens' => $this->clickOpens,
+ 'defaultHour' => $this->defaultHour,
+ 'defaultMinute' => $this->defaultMinute,
+ 'defaultSeconds' => $this->defaultSeconds,
+ 'disable' => $this->disabledDates,
+ 'disableMobile' => $this->disableMobile,
+ 'enableSeconds' => $this->enableSeconds,
+ 'hourIncrement' => $this->hourIncrement,
+ 'minuteIncrement' => $this->minuteIncrement,
+ 'inline' => $this->inline,
+ 'minDate' => $this->minDate,
+ 'maxDate' => $this->maxDate,
+ 'minTime' => $this->minTime,
+ 'maxTime' => $this->maxTime,
+ 'nextArrow' => $this->nextArrow,
+ 'prevArrow' => $this->prevArrow,
+ 'noCalendar' => $this->noCalendar,
+ 'shorthandCurrentMonth' => $this->shorthandCurrentMonth,
+ 'time_24hr' => $this->use24hr,
+ 'weekNumbers' => $this->weekNumbers,
+ 'wrap' => $this->wrap,
+ 'showMonths' => $this->showMonths,
+ 'position' => $this->position->value,
+ 'monthSelectorType' => $this->monthSelectorType->value,
+ 'animate' => $this->animate,
+ 'closeOnSelect' => $this->closeOnSelect,
+ ];
+ if ($this->getEnabledDates()) {
+ $config['enabled'] = $this->getEnabledDates();
+ }
+
+ return $config;
+ }
+
+ public function isTime(): bool
+ {
+ return $this->time;
+ }
+
+ public function time(bool $time = true): static
+ {
+ $this->time = $time;
return $this;
}
- public function isWeekSelect(): bool
+ protected function setUp(): void
{
- return $this->weekSelect;
+ parent::setUp();
+ $this->dehydrateStateUsing(static function (Flatpickr $component, $state) {
+ return self::dehydratePickerState($component, $state);
+ });
+
+ /*$this->rule(
+ 'date',
+ static fn(Flatpickr $component): bool => (!$component->isRangePicker() && !$component->isMultiplePicker() && !$component->isWeekSelect()),
+ );*/
}
- /**
- * @param array $config
- */
- public function config(array|\Closure $config): Flatpickr
+ public static function dehydratePickerState($component, $state)
{
- $this->config = $config;
+ if (blank($state)) {
+ return null;
+ }
+ if (! $state instanceof CarbonInterface) {
+ if ($component->isRangePicker() || $component->getMode() === FlatpickrMode::RANGE) {
+ $range = \Str::of($state)->explode(' to ');
+ $state = collect($range)->map(fn ($date) => Carbon::parse($date)
+ ->setTimezone(config('app.timezone'))->format($component->getDateFormat()))
+ ->toArray();
+ } elseif ($component->isMultiplePicker()) {
+ $range = \Str::of($state)->explode($component->getConjunction());
+ $state = collect($range)->map(fn ($date) => Carbon::parse($date)
+ ->setTimezone(config('app.timezone'))->format($component->getDateFormat()))
+ ->toArray();
+ }
+ }
+
+ return $state;
+ }
+
+ public function mode(FlatpickrMode $mode): static
+ {
+ $this->mode = $mode;
return $this;
}
- public function getConfig(): array
+ public function getMode(): string
{
- return $this->config;
+ return $this->mode->value;
}
- public function rangePicker(bool $rangePicker = true): Flatpickr
+ public function altInputClass(?string $altInputClass = ''): static
{
- $this->rangePicker = $rangePicker;
+ $this->altInputClass = $altInputClass;
return $this;
}
- public function isRangePicker(): bool
+ public function getAltInputClass(): ?string
{
- return $this->rangePicker;
+ return $this->altInputClass;
}
- public function multiplePicker(bool $multiplePicker = true): Flatpickr
+ public function allowInput(bool $allowInput = true): static
{
- $this->multiplePicker = $multiplePicker;
+ $this->allowInput = $allowInput;
return $this;
}
- public function isMultiplePicker(): bool
+ public function isAllowInput(): bool
{
- return $this->multiplePicker;
+ return $this->allowInput;
}
- protected function setUp(): void
+ public function isAllowInvalidPreload(): bool
{
- parent::setUp();
- $theme = config('filament-flatpickr.default_theme', 'default');
- $this->theme($theme);
- $this->reactive();
- $this->afterStateHydrated(static function (Flatpickr $component, $state): void {
- if (blank($state)) {
- return;
- }
+ return $this->allowInvalidPreload;
+ }
- if (! $state instanceof CarbonInterface) {
- try {
- $state = \Illuminate\Support\Carbon::createFromFormat($component->getDateFormat(), $state);
- } catch (InvalidFormatException $exception) {
- $state = Carbon::parse($state);
- }
- }
+ public function allowInvalidPreload(bool $allowInvalidPreload = true): static
+ {
+ $this->allowInvalidPreload = $allowInvalidPreload;
-// $state->setTimezone($component->getTimezone());
+ return $this;
+ }
- $component->state($state);
- });
+ public function getAriaDateFormat(): ?string
+ {
+ return $this->ariaDateFormat;
+ }
- $this->dehydrateStateUsing(static function (Flatpickr $component, $state) {
- if (blank($state)) {
- return null;
- }
+ public function ariaDateFormat(?string $ariaDateFormat): static
+ {
+ $this->ariaDateFormat = $ariaDateFormat;
- if (! $state instanceof CarbonInterface) {
- $state = Carbon::parse($state);
- }
+ return $this;
+ }
+
+ public function getConjunction(): ?string
+ {
+ return $this->conjunction;
+ }
-// $state->shiftTimezone($component->getTimezone());
- $state->setTimezone(config('app.timezone'));
+ public function conjunction(?string $conjunction): static
+ {
+ $this->conjunction = $conjunction;
- return $state->format($component->getDateFormat());
- });
+ return $this;
+ }
- $this->rule(
- 'date',
- static fn (Flatpickr $component): bool => (! $component->isRangePicker() && ! $component->isMultiplePicker() && ! $component->isWeekSelect()),
- );
+ public function isClickOpens(): bool
+ {
+ return $this->clickOpens;
+ }
+
+ public function clickOpens(bool $clickOpens = true): static
+ {
+ $this->clickOpens = $clickOpens;
+
+ return $this;
+ }
+
+ public function getDefaultHour(): int
+ {
+ return $this->defaultHour;
+ }
+
+ public function defaultHour(int $defaultHour): void
+ {
+ $this->defaultHour = $defaultHour;
+ }
+
+ public function getDefaultMinute(): int
+ {
+ return $this->defaultMinute;
+ }
+
+ public function defaultMinute(int $defaultMinute): static
+ {
+ $this->defaultMinute = $defaultMinute;
+
+ return $this;
+ }
+
+ public function getDefaultSeconds(): int
+ {
+ return $this->defaultSeconds;
+ }
+
+ public function defaultSeconds(int $defaultSeconds): static
+ {
+ $this->defaultSeconds = $defaultSeconds;
+
+ return $this;
+ }
+
+ public function getDisabledDates(): array
+ {
+ return $this->disabledDates;
+ }
+
+ public function disabledDates(array $disabledDates = []): static
+ {
+ $this->disabledDates = $disabledDates;
+
+ return $this;
+ }
+
+ public function getEnabledDates(): ?array
+ {
+ return $this->enabledDates;
+ }
+
+ public function enabledDates(array $enabledDates = []): static
+ {
+ $this->enabledDates = $enabledDates;
+
+ return $this;
+ }
+
+ public function isDisableMobile(): bool
+ {
+ return $this->disableMobile;
+ }
+
+ public function disableMobile(bool $disableMobile = true): static
+ {
+ $this->disableMobile = $disableMobile;
+
+ return $this;
+ }
+
+ public function isEnableSeconds(): bool
+ {
+ return $this->enableSeconds;
+ }
+
+ public function enableSeconds(bool $enableSeconds = true): static
+ {
+ $this->enableSeconds = $enableSeconds;
+
+ return $this;
+ }
+
+ public function getHourIncrement(): int
+ {
+ return $this->hourIncrement;
+ }
+
+ public function hourIncrement(int $hourIncrement = 1): static
+ {
+ $this->hourIncrement = $hourIncrement;
+
+ return $this;
+ }
+
+ public function getMinuteIncrement(): int
+ {
+ return $this->minuteIncrement;
+ }
+
+ public function minuteIncrement(int $minuteIncrement = 5): static
+ {
+ $this->minuteIncrement = $minuteIncrement;
+
+ return $this;
+ }
+
+ public function isInline(): bool
+ {
+ return $this->inline;
+ }
+
+ public function inline(bool $inline = true): static
+ {
+ $this->inline = $inline;
+
+ return $this;
+ }
+
+ public function getMaxDate(): Carbon|string|null
+ {
+ return $this->maxDate;
}
- /**
- * @param bool $monthSelect
- */
- public function monthSelect(?bool $monthSelect = true): Flatpickr
+ public function maxDate(Carbon|string|null $maxDate = 'now'): static
+ {
+ $this->maxDate = $maxDate ? Carbon::parse($maxDate) : $maxDate;
+
+ return $this;
+ }
+
+ public function getMinDate(): Carbon|string|null
+ {
+ return $this->minDate;
+ }
+
+ public function minDate(Carbon|string|null $minDate): static
+ {
+ $this->minDate = $minDate ? Carbon::parse($minDate) : $minDate;
+
+ return $this;
+ }
+
+ public function getMaxTime(): ?string
+ {
+ return $this->maxTime;
+ }
+
+ public function maxTime(?string $maxTime): static
+ {
+ $this->maxTime = $maxTime;
+
+ return $this;
+ }
+
+ public function getMinTime(): ?string
+ {
+ return $this->minTime;
+ }
+
+ public function minTime(?string $minTime): static
+ {
+ $this->minTime = $minTime;
+
+ return $this;
+ }
+
+ public function getNextArrow(): ?string
+ {
+ return $this->nextArrow;
+ }
+
+ public function nextArrow(?string $nextArrow = '>'): static
+ {
+ $this->nextArrow = $nextArrow;
+
+ return $this;
+ }
+
+ public function isNoCalendar(): bool
+ {
+ return $this->noCalendar;
+ }
+
+ public function noCalendar(bool $noCalendar = true): static
+ {
+ $this->noCalendar = $noCalendar;
+
+ return $this;
+ }
+
+ public function isShorthandCurrentMonth(): bool
+ {
+ return $this->shorthandCurrentMonth;
+ }
+
+ public function shorthandCurrentMonth(bool $shorthandCurrentMonth = true): static
+ {
+ $this->shorthandCurrentMonth = $shorthandCurrentMonth;
+
+ return $this;
+ }
+
+ public function isStatic(): bool
+ {
+ return $this->static;
+ }
+
+ public function static(bool $static = true): static
+ {
+ $this->static = $static;
+
+ return $this;
+ }
+
+ public function isUse24hr(): bool
+ {
+ return $this->use24hr;
+ }
+
+ public function use24hr(bool $use24hr = true): static
+ {
+ $this->use24hr = $use24hr;
+
+ return $this;
+ }
+
+ public function isWeekNumbers(): bool
+ {
+ return $this->weekNumbers;
+ }
+
+ public function weekNumbers(bool $weekNumbers = true): static
+ {
+ $this->weekNumbers = $weekNumbers;
+
+ return $this;
+ }
+
+ public function isWrap(): bool
+ {
+ return $this->wrap;
+ }
+
+ public function wrap(bool $wrap = true): static
+ {
+ $this->wrap = $wrap;
+
+ return $this;
+ }
+
+ public function getShowMonths(): int
+ {
+ return $this->showMonths;
+ }
+
+ public function showMonths(int $showMonths = 1): static
+ {
+ $this->showMonths = $showMonths;
+
+ return $this;
+ }
+
+ public function getPosition(): string
+ {
+ return $this->position->value;
+ }
+
+ public function position(FlatpickrPosition $position): static
+ {
+ $this->position = $position;
+
+ return $this;
+ }
+
+ public function getMonthSelectorType(): string
+ {
+ return $this->monthSelectorType->value;
+ }
+
+ public function monthSelectorType(FlatpickrMonthSelectorType $monthSelectorType): static
+ {
+ $this->monthSelectorType = $monthSelectorType;
+
+ return $this;
+ }
+
+ public function isAnimate(): bool
+ {
+ return $this->animate;
+ }
+
+ public function animate(bool $animate = true): static
+ {
+ $this->animate = $animate;
+
+ return $this;
+ }
+
+ public function isCloseOnSelect(): bool
+ {
+ return $this->closeOnSelect;
+ }
+
+ public function closeOnSelect(bool $closeOnSelect = true): static
+ {
+ $this->closeOnSelect = $closeOnSelect;
+
+ return $this;
+ }
+
+ public function getPrevArrow(): ?string
+ {
+ return $this->prevArrow;
+ }
+
+ public function prevArrow(?string $prevArrow): static
+ {
+ $this->prevArrow = $prevArrow;
+
+ return $this;
+ }
+
+ public function weekSelect(bool $weekSelect = true): static
+ {
+ $this->weekSelect = $weekSelect;
+
+ return $this;
+ }
+
+ public function isWeekSelect(): bool
+ {
+ return $this->weekSelect;
+ }
+
+ public function customConfig(array|\Closure $config): static
+ {
+ $this->config = $config;
+
+ return $this;
+ }
+
+ public function getCustomConfig(): array
+ {
+ return $this->config;
+ }
+
+ public function range(bool $rangePicker = true): static
+ {
+ $this->rangePicker = $rangePicker;
+
+ return $this;
+ }
+
+ public function isRangePicker(): bool
+ {
+ return $this->rangePicker;
+ }
+
+ public function multiple(bool $multiplePicker = true): static
+ {
+ $this->multiplePicker = $multiplePicker;
+
+ return $this;
+ }
+
+ public function isMultiplePicker(): bool
+ {
+ return $this->multiplePicker;
+ }
+
+ public function monthSelect(?bool $monthSelect = true): static
{
$this->monthSelect = $monthSelect;
@@ -142,7 +706,7 @@ public function isMonthSelect(): bool
return $this->monthSelect;
}
- public function altInput(bool $altInput = true): Flatpickr
+ public function altInput(bool $altInput = true): static
{
$this->altInput = $altInput;
@@ -154,14 +718,9 @@ public function isAltInput(): bool
return $this->altInput;
}
- public function enableTime(bool $enableTime = true): Flatpickr
+ public function enableTime(bool $enableTime = true): static
{
$this->enableTime = $enableTime;
- if ($enableTime) {
- $this->dateFormat('Y-m-d H:i:s');
- $this->altInput(false);
- $this->altFormat('Z');
- }
return $this;
}
@@ -171,7 +730,7 @@ public function isEnableTime(): bool
return $this->enableTime;
}
- public function dateFormat(string $dateFormat): Flatpickr
+ public function dateFormat(string $dateFormat): static
{
$this->dateFormat = $dateFormat;
@@ -183,7 +742,7 @@ public function getDateFormat(): ?string
return $this->dateFormat;
}
- public function altFormat(string $altFormat): Flatpickr
+ public function altFormat(string $altFormat): static
{
$this->altFormat = $altFormat;
@@ -195,20 +754,34 @@ public function getAltFormat(): ?string
return $this->altFormat;
}
- /**
- * @description Possible values: 'default','dark','material_blue','material_green','material_red','material_orange','airbnb','confetti'
- *
- * @see https://flatpickr.js.org/themes/
- */
- public function theme(string $theme): Flatpickr
+ public function theme(FlatpickrTheme $theme): static
{
$this->theme = $theme;
return $this;
}
- public function getTheme(): ?string
+ public function getTheme(): string
+ {
+ return $this->theme?->value;
+ }
+
+ public function getThemeAsset(): string
+ {
+ if ($this->getTheme() === FlatpickrTheme::DEFAULT->value) {
+ $this->theme(FlatpickrTheme::LIGHT);
+ }
+
+ return asset('vendor/'.\Savannabits\Flatpickr\Flatpickr::PACKAGE_NAME.'/flatpickr/themes/'.$this->getTheme().'.css');
+ }
+
+ public function getDarkThemeAsset(): string
+ {
+ return asset('vendor/'.\Savannabits\Flatpickr\Flatpickr::PACKAGE_NAME.'/flatpickr/themes/dark.css');
+ }
+
+ public function getLightThemeAsset(): string
{
- return $this->theme;
+ return asset('vendor/'.\Savannabits\Flatpickr\Flatpickr::PACKAGE_NAME.'/flatpickr/themes/light.css');
}
}
diff --git a/src/FlatpickrServiceProvider.php b/src/FlatpickrServiceProvider.php
index 2da168a..ead59f9 100644
--- a/src/FlatpickrServiceProvider.php
+++ b/src/FlatpickrServiceProvider.php
@@ -2,29 +2,45 @@
namespace Savannabits\Flatpickr;
-use Filament\PluginServiceProvider;
+use Filament\FilamentManager;
use Spatie\LaravelPackageTools\Package;
+use Spatie\LaravelPackageTools\PackageServiceProvider;
-class FlatpickrServiceProvider extends PluginServiceProvider
+class FlatpickrServiceProvider extends PackageServiceProvider
{
protected array $styles = [
- 'flatpickr-css' => __DIR__.'/../public/dist/flatpickr.min.css',
- 'month-select-style' => __DIR__.'/../public/dist/plugins/monthSelect/style.css',
- 'confirm-date-style' => __DIR__.'/../public/dist/plugins/confirmDate/confirmDate.css',
+ 'flatpickr-css' => __DIR__.'/../resources/dist/flatpickr/flatpickr.css',
+ 'month-select-style' => __DIR__.'/../resources/dist/flatpickr/plugins/monthSelect/style.css',
+ 'confirm-date-style' => __DIR__.'/../resources/dist/flatpickr/plugins/confirmDate/confirmDate.css',
];
protected array $beforeCoreScripts = [
- 'flatpickr-core' => __DIR__.'/../public/dist/flatpickr.min.js',
- 'flatpickr-range-plugin' => __DIR__.'/../public/dist/plugins/rangePlugin.js',
- 'flatpickr-month-select' => __DIR__.'/../public/dist/plugins/monthSelect/index.js',
- 'flatpickr-week-select' => __DIR__.'/../public/dist/plugins/weekSelect/weekSelect.js',
- 'flatpickr-confirm-date' => __DIR__.'/../public/dist/plugins/confirmDate/confirmDate.js',
- 'filament-flatpickr' => __DIR__.'/../resources/dist/datepicker.js',
+ // 'flatpickr-core' => __DIR__ . '/../public/dist/flatpickr.min.js',
+ // 'flatpickr-range-plugin' => __DIR__ . '/../public/dist/plugins/rangePlugin.js',
+ // 'flatpickr-month-select' => __DIR__ . '/../public/dist/plugins/monthSelect/index.js',
+ // 'flatpickr-week-select' => __DIR__ . '/../public/dist/plugins/weekSelect/weekSelect.js',
+ // 'flatpickr-confirm-date' => __DIR__ . '/../public/dist/plugins/confirmDate/confirmDate.js',
+ // 'async-alpine' => "https://cdn.jsdelivr.net/npm/async-alpine@1.x.x/dist/async-alpine.script.js"
+ // 'filament-flatpickr' => __DIR__.'/../resources/dist/datepicker.js',
];
protected array $scripts = [
];
+ public function packageBooted()
+ {
+ if ($this->app->has('filament')) {
+ /**
+ * @var FilamentManager $filament
+ */
+ $filament = $this->app['filament'];
+ $filament->serving(function () use ($filament) {
+ $filament->registerStyles($this->styles);
+ $filament->registerScripts($this->beforeCoreScripts, true);
+ });
+ }
+ }
+
public function configurePackage(Package $package): void
{
/*
@@ -33,8 +49,9 @@ public function configurePackage(Package $package): void
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
- ->name('filament-flatpickr')
+ ->name(Flatpickr::PACKAGE_NAME)
->hasConfigFile()
+ ->hasAssets()
->hasViews();
}