Skip to content

Commit

Permalink
Merge pull request #7 from TappNetwork/add_options_to_show_by_country…
Browse files Browse the repository at this point in the history
…_or_region

Add options to list timezones by country or by region
  • Loading branch information
andreia authored Mar 27, 2024
2 parents 5f1591d + 95cb563 commit 77044f6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,32 @@ public static function form(Form $form): Form
}
```

All [Filament select field](https://filamentphp.com/docs/2.x/forms/fields#select) methods are available to use:
To list only the timezones for a country, you can pass the country code to `->byCountry()` method. For example, to list only United States timezones:

```php
TimezoneSelect::make('timezone')
->byCountry('US')
```

It's also possible to list the timezones for a region using `->byRegion()` method. You can specify a region with a [Region enum value](src/Enums/Region.php):

```php
use Tapp\FilamentTimezoneField\Enums\Region;

TimezoneSelect::make('timezone')
->byRegion(Region::Australia)
```

or you can use one of the [PHP's DateTimeZone predifined constants](https://www.php.net/manual/en/class.datetimezone.php):

```php
use DateTimeZone;

TimezoneSelect::make('timezone')
->byRegion(DateTimeZone::AUSTRALIA)
```

Also all [Filament select field](https://filamentphp.com/docs/2.x/forms/fields#select) methods are available to use:

```php
use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect;
Expand Down
57 changes: 55 additions & 2 deletions src/Concerns/HasTimezoneOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

use DateTime;
use DateTimeZone;
use Tapp\FilamentTimezoneField\Enums\Region;

trait HasTimezoneOptions
{
{
protected string | Closure | null $byCountry = null;

protected Region | int | Closure | null $byRegion = null;

public function getOptions(): array
{
$options = $this->getTimezones();
Expand All @@ -18,7 +23,11 @@ public function getOptions(): array

public function getTimezones(): array
{
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
$timezones = match(true) {
!empty($this->byCountry) => $this->listTimezonesByCountry($this->byCountry),
!empty($this->byRegion) => $this->listTimezonesByRegion($this->byRegion),
default => $this->listAllTimezones(),
};

$data = [];

Expand All @@ -40,4 +49,48 @@ public function getTimezones(): array

return $data;
}

public function byCountry(string | Closure | null $countryCode): static
{
$this->byCountry = $countryCode;

return $this;
}

public function getByCountry(): string | null
{
return $this->evaluate($this->byCountry);
}

public function byRegion(Region | int | Closure | null $region): static
{
$this->byRegion = $region;

return $this;
}

public function getByRegion(): Region | int | null
{
return $this->evaluate($this->byRegion);
}

protected function listTimezonesByCountry($countryCode)
{
return DateTimeZone::listIdentifiers(
timezoneGroup: DateTimeZone::PER_COUNTRY,
countryCode: $countryCode,
);
}

protected function listTimezonesByRegion($region)
{
return DateTimeZone::listIdentifiers(
timezoneGroup: $region?->value ?? $region,
);
}

protected function listAllTimezones()
{
return DateTimeZone::listIdentifiers(DateTimeZone::ALL);
}
}
25 changes: 25 additions & 0 deletions src/Enums/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tapp\FilamentTimezoneField\Enums;

use DateTimeZone;
use Filament\Support\Contracts\HasLabel;

enum Region: int implements HasLabel
{
case Africa = DateTimeZone::AFRICA;
case America = DateTimeZone::AMERICA;
case Antarctica = DateTimeZone::ANTARCTICA;
case Arctic = DateTimeZone::ARCTIC;
case Asia = DateTimeZone::ASIA;
case Atlantic = DateTimeZone::ATLANTIC;
case Australia = DateTimeZone::AUSTRALIA;
case Europe = DateTimeZone::EUROPE;
case Indian = DateTimeZone::INDIAN;
case Pacific = DateTimeZone::PACIFIC;

public function getLabel(): ?string
{
return $this->name;
}
}

0 comments on commit 77044f6

Please sign in to comment.