Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andreia committed May 6, 2022
0 parents commit a5eafd8
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
68 changes: 68 additions & 0 deletions README.md
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(),
// ...
]);
}
```
30 changes: 30 additions & 0 deletions composer.json
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
}
Binary file added docs/filament-timezone-field.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions src/Forms/Components/TimezoneSelect.php
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
);
}
}

0 comments on commit a5eafd8

Please sign in to comment.