-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a5eafd8
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Filament Timezone Field | ||
|
||
A timezone select field for Laravel Filament. | ||
|
||
## Installation | ||
|
||
```bash | ||
composer require tapp/filament-timezone-field | ||
``` | ||
|
||
## Usage | ||
|
||
Add to your Filament resource: | ||
|
||
```php | ||
use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
// ... | ||
TimezoneSelect::make('timezone'), | ||
// ... | ||
]); | ||
} | ||
``` | ||
|
||
### Appareance | ||
|
||
![Filament Timezone Field](https://raw.githubusercontent.com/TappNetwork/filament-timezone-field/main/docs/filament-timezone-field.png) | ||
|
||
### Options | ||
|
||
To use GMT instead of UTC (default is UTC), add the `->timezoneType('GMT')` method: | ||
|
||
```php | ||
use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
// ... | ||
TimezoneSelect::make('timezone') | ||
->timezoneType('GMT'), | ||
// ... | ||
]); | ||
} | ||
``` | ||
|
||
All Filament select field 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(), | ||
// ... | ||
]); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "tapp/filament-timezone-field", | ||
"description": "Filament timezone field.", | ||
"keywords": [ | ||
"tapp network", | ||
"filament", | ||
"laravel", | ||
"timezone" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/TappNetwork/filament-timezone-field", | ||
"support": { | ||
"issues": "https://github.com/TappNetwork/filament-timezone-field/issues", | ||
"source": "https://github.com/TappNetwork/filament-timezone-field" | ||
}, | ||
"require": { | ||
"php": "^8.0", | ||
"filament/filament": "^2.9" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Tapp\\FilamentTimezoneField\\": "src" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Forms\Components; | ||
|
||
use DateTimeZone; | ||
use Filament\Forms\Components\Select; | ||
|
||
class TimezoneSelect extends Select | ||
{ | ||
protected string $view = 'forms::components.select'; | ||
|
||
protected string | null $timezoneType = null; | ||
|
||
protected array $allowedTimezoneTypes = [ | ||
'UTC', | ||
'GMT', | ||
]; | ||
|
||
public function getOptions(): array | ||
{ | ||
$options = $this->getTimezones(); | ||
|
||
$this->options = $options; | ||
|
||
return $this->options; | ||
} | ||
|
||
public function timezoneType(string | null $type): static | ||
{ | ||
$this->timezoneType = strtoupper($type); | ||
|
||
return $this; | ||
} | ||
|
||
public function getTimezoneType(): string | ||
{ | ||
if ($this->timezoneType === null && !in_array($this->timezoneType, $this->allowedTimezoneTypes)) { | ||
$this->timezoneType = 'UTC'; | ||
} | ||
|
||
return $this->timezoneType; | ||
} | ||
|
||
public function getTimezones(): array | ||
{ | ||
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); | ||
|
||
$data = []; | ||
|
||
$now = new \DateTime('now', new DateTimeZone($this->getTimezoneType())); | ||
|
||
foreach ($timezones as $timezone) { | ||
$offsets[] = $offset = $now->setTimezone(new DateTimeZone($timezone))->getOffset(); | ||
|
||
$data[$timezone] = sprintf('(%s) %s', $this->getFormattedOffset($offset), $this->getFormattedTimezoneName($timezone)); | ||
} | ||
|
||
array_multisort($offsets, $data); | ||
|
||
return $data; | ||
} | ||
|
||
protected function getFormattedOffset(string $offset): string | ||
{ | ||
$hours = intval($offset / 3600); | ||
$minutes = abs(intval($offset % 3600 / 60)); | ||
|
||
return $this->getTimezoneType().($offset ? sprintf('%+03d:%02d', $hours, $minutes) : ''); | ||
} | ||
|
||
protected function getFormattedTimezoneName(string $name): string | ||
{ | ||
return str_replace( | ||
['/', '_', 'St '], | ||
[', ', ' ', 'St. '], | ||
$name | ||
); | ||
} | ||
} |