Skip to content

Instantly share code, notes, and snippets.

@awcodes
Last active November 7, 2024 13:40
Show Gist options
  • Save awcodes/2d54ac60139fbb66acb34fc10bbb9fc5 to your computer and use it in GitHub Desktop.
Save awcodes/2d54ac60139fbb66acb34fc10bbb9fc5 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() ? 'h-full flex items-end' : null }}"
>
<div class="{{ $isLabelHidden() && ! $hasInlineLabel() ? 'relative bottom-0' : null }}">
<x-forms::actions.action
:action="$getExecutableAction()"
component="forms::button"
>
@if (! $getExecutableAction()->isLabelHidden())
{{ $getExecutableAction()->getLabel() }}
@endif
</x-forms::actions.action>
</div>
</x-forms::field-wrapper>
<?php
namespace App\Forms\Components;
use Closure;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Field;
class DropInAction extends Field
{
protected Action | Closure | null $action = null;
protected string $view = 'forms.components.drop-in-action';
protected function setUp(): void
{
$this->dehydrated(false);
}
public function execute(Action | Closure | null $action): static
{
$this->action = $action;
return $this;
}
public function getExecutableAction()
{
return $this->evaluate($this->action)?->component($this);
}
public function getActions(): array
{
$executableAction = $this->getExecutableAction();
return array_merge(
parent::getActions(),
$executableAction ? [$executableAction->getName() => $executableAction->component($this)] : [],
);
}
}
<?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