Skip to content

Commit 2808c85

Browse files
Lui Wing LeungLui Wing Leung
authored andcommitted
Create universal PanZoom component for all Filament contexts
- Unified PanZoom component that works in Forms, Infolists, Tables, etc. - Single API: PanZoom::make() works everywhere - Removed redundant separate components - Updated README with universal usage examples - Simplified view with universal wrapper detection - No need for separate Form/Infolist components anymore
1 parent 87319c6 commit 2808c85

3 files changed

Lines changed: 68 additions & 59 deletions

File tree

README.md

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -79,88 +79,73 @@ You can use the component directly in any Blade view:
7979
])
8080
```
8181

82-
### In Filament Forms
82+
### Universal Usage
8383

84-
Use the simple PanZoom component in your Filament forms:
84+
The PanZoom component works everywhere in Filament - Forms, Infolists, Tables, etc. One simple API:
8585

8686
```php
8787
use SolutionForest\FilamentPanzoom\Components\PanZoom;
8888

89+
// Works in Forms, Infolists, Tables - anywhere!
8990
PanZoom::make('image_viewer')
9091
->label('Receipt Image')
9192
->imageUrl(fn ($record) => $record?->image_url ?? '/placeholder.jpg')
9293
->imageId(fn ($record) => 'receipt-' . ($record?->id ?? 'new'))
9394
->columnSpanFull(),
9495
```
9596

96-
### In Filament Resources
97-
98-
Complete example in a Filament Resource:
97+
### Usage Examples
9998

99+
**In Forms:**
100100
```php
101-
<?php
102-
103-
namespace App\Filament\Resources;
101+
public static function form(Form $form): Form
102+
{
103+
return $form->schema([
104+
PanZoom::make('image_preview')
105+
->imageUrl(fn ($record) => $record?->image_url)
106+
->columnSpanFull(),
107+
]);
108+
}
109+
```
104110

105-
use Filament\Forms;
106-
use Filament\Forms\Form;
107-
use Filament\Resources\Resource;
108-
use SolutionForest\FilamentPanzoom\Components\PanZoom;
111+
**In Infolists:**
112+
```php
113+
public function infolist(Infolist $infolist): Infolist
114+
{
115+
return $infolist->schema([
116+
PanZoom::make('receipt_image')
117+
->imageUrl(fn ($record) => asset('storage/' . $record->image_path))
118+
->imageId(fn ($record) => 'receipt-' . $record->id),
119+
]);
120+
}
121+
```
109122

110-
class ReceiptResource extends Resource
123+
**In Table Actions:**
124+
```php
125+
public static function table(Table $table): Table
111126
{
112-
public static function form(Form $form): Form
113-
{
114-
return $form
115-
->schema([
116-
Forms\Components\TextInput::make('title')
117-
->required(),
118-
119-
Forms\Components\FileUpload::make('image')
120-
->image()
121-
->required(),
122-
123-
// Simple Pan & Zoom component
127+
return $table->actions([
128+
Action::make('viewImage')
129+
->form([
124130
PanZoom::make('image_viewer')
125-
->label('Image Preview')
126-
->imageUrl(fn ($record) => $record?->image_url ?? '/placeholder.jpg')
127-
->imageId(fn ($record) => 'receipt-' . ($record?->id ?? 'new'))
128-
->columnSpanFull()
129-
->hidden(fn ($record) => !$record?->image_url),
130-
]);
131-
}
131+
->imageUrl(fn ($record) => $record->image_url),
132+
])
133+
]);
132134
}
133135
```
134136

135-
### In Filament Pages
136-
137-
For custom Filament pages:
138-
137+
**In Custom Pages:**
139138
```php
140-
// In your Filament Page class
141-
protected function getViewData(): array
139+
protected function getFormSchema(): array
142140
{
143141
return [
144-
'imageUrl' => 'path/to/your/image.jpg',
145-
'imageId' => 'my-image-viewer',
142+
PanZoom::make('document_viewer')
143+
->imageUrl($this->imageUrl)
144+
->columnSpanFull(),
146145
];
147146
}
148147
```
149148

150-
Then in your page view:
151-
```blade
152-
<x-filament-panels::page>
153-
<div class="space-y-6">
154-
<h2>Document Viewer</h2>
155-
156-
@include('filament-panzoom::filament-panzoom', [
157-
'imageUrl' => $imageUrl,
158-
'imageId' => $imageId
159-
])
160-
</div>
161-
</x-filament-panels::page>
162-
```
163-
164149
## Component Properties
165150

166151
The component accepts the following properties:

resources/views/components/pan-zoom.blade.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
1+
@php
2+
// Universal wrapper - works for forms, infolists, and other contexts
3+
$wrapperComponent = null;
4+
$wrapperData = [];
5+
6+
if (method_exists($this, 'getFieldWrapperView')) {
7+
// Forms context
8+
$wrapperComponent = $this->getFieldWrapperView();
9+
$wrapperData = ['field' => $field ?? $this];
10+
} elseif (method_exists($this, 'getEntryWrapperView')) {
11+
// Infolist context
12+
$wrapperComponent = $this->getEntryWrapperView();
13+
$wrapperData = ['entry' => $entry ?? $this];
14+
}
15+
@endphp
16+
17+
@if($wrapperComponent)
18+
<x-dynamic-component :component="$wrapperComponent" :field="$wrapperData['field'] ?? null" :entry="$wrapperData['entry'] ?? null">
19+
@endif
220
<div
321
x-data="interactiveImage({{ json_encode($getImageUrl()) }}, '{{ $getImageId() }}')"
422
class="relative bg-gray-50 rounded-lg border border-gray-200 overflow-hidden w-full"
@@ -65,4 +83,7 @@ class="w-8 h-8 flex items-center justify-center bg-white hover:bg-gray-50 border
6583
<span x-text="`${Math.round(scale * 100)}%`"></span>
6684
</div>
6785
</div>
68-
</x-dynamic-component>
86+
87+
@if($wrapperComponent)
88+
</x-dynamic-component>
89+
@endif

src/Components/PanZoom.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace SolutionForest\FilamentPanzoom\Components;
44

5-
use Filament\Forms\Components\Field;
5+
use Filament\Forms\Components\Component;
6+
use Filament\Support\Concerns\EvaluatesClosures;
67

7-
class PanZoom extends Field
8+
class PanZoom extends Component
89
{
10+
use EvaluatesClosures;
11+
912
protected string $view = 'filament-panzoom::components.pan-zoom';
1013

1114
protected string | \Closure | null $imageUrl = null;
1215

1316
protected string | \Closure | null $imageId = null;
1417

15-
public static function make(string $name): static
18+
public static function make(string $name = 'panzoom'): static
1619
{
1720
$static = app(static::class, ['name' => $name]);
1821
$static->configure();

0 commit comments

Comments
 (0)