PHP code example of hadyfayed / filament-react-wrapper
1. Go to this page and download the library: Download hadyfayed/filament-react-wrapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hadyfayed / filament-react-wrapper example snippets
// app/Providers/Filament/AdminPanelProvider.php
use HadyFayed\ReactWrapper\FilamentReactWrapperPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
FilamentReactWrapperPlugin::make(),
]);
}
use HadyFayed\ReactWrapper\Components\ReactComponent;
class EditUser extends EditRecord
{
protected function getHeaderActions(): array
{
return [
ReactComponent::make('UserProfileEditor')
->props([
'userId' => $this->record->id,
'editable' => true
])
->statePath('userProfile')
];
}
}
namespace App\Filament\Resources;
use Filament\Resources\Resource;
use HadyFayed\ReactWrapper\Services\ReactComponentRegistry;
class UserResource extends Resource
{
public static function boot()
{
parent::boot();
// Register resource-specific components
app(ReactComponentRegistry::class)->register([
'name' => 'UserResourceChart',
'component_path' => 'resources/js/filament/UserResourceChart.tsx',
'default_props' => [
'resource' => 'users',
'metric' => 'registrations'
]
]);
}
public static function form(Form $form): Form
{
return $form->schema([
ReactField::make('user_analytics')
->component('UserResourceChart')
->props(['userId' => fn($record) => $record?->id])
->visible(fn($record) => $record !== null)
]);
}
}
namespace App\Filament\Components;
use HadyFayed\ReactWrapper\Components\ReactComponent;
class UserDashboard extends ReactComponent
{
protected string $component = 'UserDashboard';
public static function make(string $component = null): static
{
return new static($component ?? static::$component);
}
public function userId(int $userId): static
{
$this->props(['userId' => $userId]);
return $this;
}
public function editable(bool $editable = true): static
{
$this->props(['editable' => $editable]);
return $this;
}
}
use App\Filament\Components\UserDashboard;
class Dashboard extends Page
{
protected static string $view = 'filament.pages.dashboard';
protected function getHeaderWidgets(): array
{
return [
UserDashboard::make()
->userId(auth()->id())
->editable(true)
->statePath('dashboard.user')
];
}
}
use HadyFayed\ReactWrapper\Forms\Components\ReactField;
class UserForm extends Form
{
public function form(Form $form): Form
{
return $form->schema([
ReactField::make('profile_editor')
->component('UserProfileEditor')
->props([
'allowImageUpload' => true,
'maxImageSize' => '2MB'
])
->statePath('profile')
->reactive()
->afterStateUpdated(function ($state) {
// Handle state updates
$this->user->update($state);
})
]);
}
}
namespace HadyFayed\ReactWrapper\Services;
interface ReactComponentRegistryInterface
{
public function register(array $definition): void;
public function registerMany(array $definitions): void;
public function get(string $name): ?array;
public function has(string $name): bool;
public function unregister(string $name): bool;
public function clear(): void;
public function getRegistered(): array;
public function getStats(): array;
public function scan(string $directory): array;
}