Skip to content

Instantly share code, notes, and snippets.

@JackWH
Forked from awcodes/DropInAction.php
Last active February 5, 2023 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackWH/01f2586c4aec43ac7400021d42192f1c to your computer and use it in GitHub Desktop.
Save JackWH/01f2586c4aec43ac7400021d42192f1c to your computer and use it in GitHub Desktop.
<x-forms::field-wrapper
:id="$getId()"
:label="$getLabel()"
:label-sr-only="$isLabelHidden()"
:helper-text="$getHelperText()"
:hint="$getHint()"
:hint-icon="$getHintIcon()"
:required="$isRequired()"
:state-path="$getStatePath()"
class="{{ $isLabelHidden() && !$hasInlineLabel() ? 'w-full h-full flex items-center justify-between' : null }}"
>
<div class="{{ $isLabelHidden() && ! $hasInlineLabel() ? 'relative bottom-0' : null }}">
@foreach ($getExecutableActions() as $executableAction)
<x-forms::actions.action
:action="$executableAction"
class="flex items-center"
component="forms::button">
@if (!$executableAction->isLabelHidden())
{{ $executableAction->getLabel() }}
@endif
</x-forms::actions.action>
@endforeach
</div>
</x-forms::field-wrapper>
<?php
namespace App\Forms\Components;
use Closure;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Field;
use Illuminate\Support\Arr;
/** @see https://gist.github.com/awcodes/2d54ac60139fbb66acb34fc10bbb9fc5 */
class DropInAction extends Field
{
/** @var array<Action|Closure> */
protected array $actions = [];
private array $evaluatedActions = [];
protected string $view = 'forms.components.drop-in-action';
protected function setUp(): void
{
$this->dehydrated(false);
}
public function execute(array|Action|Closure|null $actions): static
{
foreach (Arr::wrap($actions) as $action) {
$this->actions[] = $action;
}
return $this;
}
/** @return array<Action|Closure> */
public function getExecutableActions(bool $reevaluate = false): array
{
if ((! $reevaluate) && $this->evaluatedActions) {
return $this->evaluatedActions;
}
$this->evaluatedActions = [];
foreach ($this->actions as $action) {
$this->evaluatedActions[] = $this->evaluate($action)?->component($this);
}
return $this->evaluatedActions;
}
public function getActions(): array
{
$actions = collect($this->getExecutableActions())
->mapWithKeys(fn($action) => [$action->getName() => $action->component($this)])
->toArray();
return array_merge(parent::getActions(), $actions);
}
}
<?php
DropInAction::make('test')
->disableLabel()
->execute(function (Closure $get, Closure $set) {
return Action::make('geolocate')
->icon('heroicon-o-search')
->label('Geocode')
->action(function () use ($get, $set) {
if (blank($get('street')) || blank($get('city')) || blank($get('state')) || blank($get('zip'))) {
Filament::notify('danger', 'Please provide the location\'s complete address before geocoding.');
return;
}
$address = "{$get('street')} {$get('unit')} {$get('city')} {$get('state')} {$get('zip')}";
try {
$mapsData = Http::get(config('services.google_maps.url') . '?key=' . config('services.google_maps.key') . '&address=' . $address . '&sensor=true')
->throw()
->json();
} catch (RequestException $e) {
Filament::notify('danger', 'Unable to geocode for this address.');
return;
}
$set('latitude', $mapsData['results'][0]['geometry']['location']['lat'] ?? $get('latitude'));
$set('longitude', $mapsData['results'][0]['geometry']['location']['lng'] ?? $get('longitude'));
});
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment