Skip to content

Commit

Permalink
Merge pull request #6 from cornelisonc/main
Browse files Browse the repository at this point in the history
Additional display options for select options in form field
  • Loading branch information
andreia authored Mar 18, 2024
2 parents 78973aa + 544f95b commit 847598d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ public static function form(Form $form): Form
}
```

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)

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

public static function form(Form $form): Form
{
return $form
->schema([
// ...
TimezoneSelect::make('timezone')
->hideNames(),
// ...
]);
}
```

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

public static function form(Form $form): Form
{
return $form
->schema([
// ...
TimezoneSelect::make('timezone')
->hideOffset(),
// ...
]);
}
```

### Table Column

```php
Expand Down
Binary file added docs/hide-timezone-offset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/Concerns/HasDisplayOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tapp\FilamentTimezoneField\Concerns;

trait HasDisplayOptions
{
protected bool $displayOffset = true;
protected bool $displayNames = true;

public function hideOffset(): static
{
$this->displayOffset = false;
$this->displayNames = true;

return $this;
}

public function hideNames(): static
{
$this->displayOffset = true;
$this->displayNames = false;

return $this;
}

public function getDisplayOffset(): bool
{
return $this->displayOffset;
}

public function getDisplayNames(): bool
{
return $this->displayNames;
}
}
10 changes: 8 additions & 2 deletions src/Concerns/HasTimezoneOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ public function getTimezones(): array

foreach ($timezones as $timezone) {
$offsets[] = $offset = $now->setTimezone(new DateTimeZone($timezone))->getOffset();

$data[$timezone] = $this->getFormattedOffsetAndTimezone($offset, $timezone);

if ($this->getDisplayOffset() && $this->getDisplayNames()) {
$data[$timezone] = $this->getFormattedOffsetAndTimezone($offset, $timezone);
} elseif ($this->getDisplayOffset()) {
$data[$timezone] = $this->getFormattedOffset($offset);
} else {
$data[$timezone] = $this->getFormattedTimezoneName($timezone);
}
}

array_multisort($offsets, $data);
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/Components/TimezoneSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Filament\Forms\Components\Select;
use Tapp\FilamentTimezoneField\Concerns\CanFormatTimezone;
use Tapp\FilamentTimezoneField\Concerns\HasDisplayOptions;
use Tapp\FilamentTimezoneField\Concerns\HasTimezoneOptions;
use Tapp\FilamentTimezoneField\Concerns\HasTimezoneType;

class TimezoneSelect extends Select
{
use CanFormatTimezone;
use HasDisplayOptions;
use HasTimezoneOptions;
use HasTimezoneType;
}

0 comments on commit 847598d

Please sign in to comment.