Skip to content

Commit

Permalink
Merge pull request #13 from TappNetwork/add_multiple_countries
Browse files Browse the repository at this point in the history
Add option to pass multiple country codes or regions
  • Loading branch information
andreia authored May 29, 2024
2 parents e727249 + 950ab72 commit 74917c8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
51 changes: 36 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,25 @@ public static function form(Form $form): Form
}
```

##### List timezones by country

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):
You can also pass an array with more than one country code:

```php
TimezoneSelect::make('timezone')
->byCountry(['US', 'AU'])
```

##### List timezones by region

To list the timezones for a region use the `->byRegion()` method. You can specify a region with a [Region enum value](src/Enums/Region.php):

```php
use Tapp\FilamentTimezoneField\Enums\Region;
Expand All @@ -83,25 +94,35 @@ 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:
It's also possible to pass an array with more than one region:

```php
use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect;
use Tapp\FilamentTimezoneField\Enums\Region;

public static function form(Form $form): Form
{
return $form
->schema([
// ...
TimezoneSelect::make('timezone')
->searchable()
->required(),
// ...
]);
}
TimezoneSelect::make('timezone')
->byRegion([Region::Australia, Region::America])
```

Optionally hide either timezone offsets or timezone names, depending on your use case:
> [!TIP]
> 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;
>
> public static function form(Form $form): Form
> {
> return $form
> ->schema([
> // ...
> TimezoneSelect::make('timezone')
> ->searchable()
> ->required(),
> // ...
> ]);
> }
> ```
Optionally, hide either timezone offsets or timezone names, depending on your use case:
![Filament Timezone Display Options](https://raw.githubusercontent.com/TappNetwork/filament-timezone-field/main/docs/hide-timezone-offset.png)
Expand Down
47 changes: 32 additions & 15 deletions src/Concerns/HasTimezoneOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use DateTime;
use DateTimeZone;
use Illuminate\Support\Arr;
use Tapp\FilamentTimezoneField\Enums\Region;

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

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

public function getOptions(): array
{
Expand Down Expand Up @@ -50,43 +51,59 @@ public function getTimezones(): array
return $data;
}

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

return $this;
}

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

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

return $this;
}

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

protected function listTimezonesByCountry($countryCode)
protected function listTimezonesByCountry(array|string $countryCodes)
{
return DateTimeZone::listIdentifiers(
timezoneGroup: DateTimeZone::PER_COUNTRY,
countryCode: $countryCode,
);
$countryCodes = Arr::wrap($countryCodes);

$timezones = [];

foreach ($countryCodes as $countryCode) {
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers(
timezoneGroup: DateTimeZone::PER_COUNTRY,
countryCode: $countryCode,
));
}

return $timezones;
}

protected function listTimezonesByRegion($region)
protected function listTimezonesByRegion(array|Region|int $regions)
{
return DateTimeZone::listIdentifiers(
timezoneGroup: $region?->value ?? $region,
);
$regions = Arr::wrap($regions);

$timezones = [];

foreach ($regions as $region) {
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers(
timezoneGroup: $region?->value ?? $region,
));
}

return $timezones;
}

protected function listAllTimezones()
Expand Down

0 comments on commit 74917c8

Please sign in to comment.