ahmedabdelaal/filament-json-preview
Installation:
composer require ahmedabdelaal/filament-json-preview
php artisan vendor:publish --tag=jsoneditor
vendor:publish step ensures SVG icons (e.g., for the editor toolbar) are available.First Use Case:
Add the JsonPreview component to an Infolist (for read-only display) or Form (for editable JSON):
use AhmedAbdel3al\FilamentJsonPreview\Widgets\JsonPreview;
public static function form(Form $form): Form
{
return $form->schema([
JsonPreview::make('json_data')
->columnSpanFull(), // Optional: Full-width layout
]);
}
data() method:
JsonPreview::make('logs')->data($yourJsonArrayOrString)
Where to Look First:
Widgets/JsonPreview.php for method signatures and defaults.Read-Only JSON Display (Infolist):
JsonPreview::make('config')
->label('Application Settings')
->columnSpan('full')
->disabled(); // Disable editing if needed
Editable JSON (Form):
JsonPreview::make('metadata')
->required()
->rules(['json']) // Validate JSON format
->columnSpan('full');
Textarea) is bound to a JSON string or array.Dynamic Data Binding: Use closures for runtime data:
JsonPreview::make('dynamic_data')
->data(fn () => $this->getJsonDataFromApi())
->reactive(); // Update on parent changes
Integration with Filament Tables: Add a JSON column to a table for inline preview:
Table::make(MyModel::class)
->columns([
Tables\Columns\JsonColumn::make('attributes')
->label('JSON Attributes')
->content(fn ($record) => JsonPreview::make('preview')
->data($record->attributes)
->disabled()
->columnSpan('full')),
]);
Theming:
Leverage JSONEditor’s built-in themes (e.g., dark, light):
JsonPreview::make('theme_example')
->theme('dark')
->mode('code'); // or 'tree', 'view'
JsonPreview::make('settings')->default([
'key' => 'default_value',
]);
JsonPreview::make('conditional_json')
->visible(fn ($record) => $record->hasJsonData)
->hiddenLabel();
Data Format Mismatch:
->rules(['json']) or pre-process data:
JsonPreview::make('data')->data(json_encode($array, JSON_PRETTY_PRINT));
Icon Publishing:
vendor:publish is skipped.php artisan vendor:publish --tag=jsoneditor after installation.Performance with Large JSON:
->mode('code') for large payloads or lazy-load data.Reactivity Quirks:
reactive() may not trigger updates if the parent component doesn’t emit events.->livewire() for Livewire components or ->reactive() with Alpine.js.Console Errors:
Uncaught TypeError if JSONEditor fails to load. Ensure:
jsoneditor CDN is accessible (or use local assets via mix).JsonPreview::make('debug_json')->debug(true);
Data Binding:
dd(json_decode($yourData, true)); // Should not return false
Custom Modes: Extend JSONEditor’s modes by passing a custom config:
JsonPreview::make('custom')
->options([
'modes' => ['code', 'tree', 'view', 'form'],
]);
Event Listeners: Listen to JSON changes in forms:
JsonPreview::make('live_updates')
->onChange(fn ($value) => $this->handleJsonChange($value));
Localization: Override JSONEditor’s labels (e.g., for translations):
JsonPreview::make('i18n_json')
->options([
'labels' => [
'add_item' => __('filament-json-preview::add_item'),
],
]);
// config/filament-json-preview.php
'default_options' => [
'mode' => 'tree',
'theme' => 'light',
],
jsoneditor is included in your build:
// resources/js/app.js
import 'jsoneditor/dist/jsoneditor.min.css';
import 'jsoneditor';
How can I help you explore Laravel packages today?