-
-
Save WaleedAlrashed/731536c097cad62adc7d5b7141df666d to your computer and use it in GitHub Desktop.
Filament - zeus bolt (dynamic repeater form)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Zeus\Fields; | |
use Filament\Forms\Components\Grid; | |
use Filament\Forms\Components\Hidden; | |
use Filament\Forms\Components\Repeater; | |
use Filament\Forms\Components\TextInput; | |
use Filament\Tables\Columns\TextColumn; | |
use LaraZeus\Accordion\Forms\Accordion; | |
use LaraZeus\Accordion\Forms\Accordions; | |
use LaraZeus\Bolt\Facades\Bolt; | |
use LaraZeus\Bolt\Fields\FieldsContract; | |
use LaraZeus\Bolt\Models\Field; | |
use LaraZeus\Bolt\Models\FieldResponse; | |
use LaraZeus\Bolt\Models\Response; | |
class FilamentFormsNameComponentsRepeater extends FieldsContract | |
{ | |
public string $renderClass = \Filament\Forms\Components\Repeater::class; | |
public int $sort = 103; | |
public function title(): string | |
{ | |
return __('Dynamic Name'); | |
} | |
public function icon(): string | |
{ | |
return 'tabler-text-wrap'; | |
} | |
public function description(): string | |
{ | |
return __('add multiple text values'); | |
} | |
public static function getOptions(): array | |
{ | |
return [ | |
Accordions::make('check-list-options') | |
->columns() | |
->accordions([ | |
Accordion::make('general-options') | |
->label(__('General Options')) | |
->icon('iconpark-checklist-o') | |
->schema([ | |
// add a repeater to define the fields | |
TextInput::make('options.minItems')->default(1)->numeric(), | |
TextInput::make('options.maxItems')->default(1)->numeric(), | |
TextInput::make('options.defaultItems')->default(1)->numeric(), | |
self::required(), | |
self::htmlID(), | |
]), | |
]), | |
]; | |
} | |
public static function getOptionsHidden(): array | |
{ | |
return [ | |
self::hiddenHtmlID(), | |
self::hiddenRequired(), | |
Hidden::make('options.minItems'), | |
Hidden::make('options.maxItems'), | |
Hidden::make('options.defaultItems'), | |
]; | |
} | |
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false) | |
{ | |
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility); | |
$fieldOptions = $zeusField['options']; | |
if (filled($fieldOptions['minItems'])) { | |
$component->minItems($fieldOptions['minItems']); | |
} | |
if (filled($fieldOptions['maxItems'])) { | |
$component->maxItems(1); | |
} | |
if (filled($fieldOptions['defaultItems'])) { | |
$component->defaultItems($fieldOptions['defaultItems']); | |
} | |
$repeatedField = Repeater::make('member') | |
->schema([ | |
TextInput::make('name')->string(), | |
TextInput::make('email')->email(), | |
]) | |
->columns()->addable(false); | |
// $repeatedField = TextInput::make('name')->string(); | |
if (filled($fieldOptions['is_required'])) { | |
$repeatedField->required()->default(''); | |
} | |
$component | |
->simple($repeatedField) | |
->reorderableWithDragAndDrop(false) | |
->reorderableWithButtons() | |
->cloneable() | |
->addActionLabel(__('Add')) | |
->default(['']); | |
return $component; | |
} | |
/** | |
* @throws \Throwable | |
*/ | |
public function getResponse(Field $field, FieldResponse $resp): string | |
{ | |
$responseValue = (filled($resp->response) && Bolt::isJson($resp->response)) ? json_decode($resp->response) : [$resp->response]; | |
return json_encode($responseValue); | |
} | |
public function TableColumn(Field $field): ?\Filament\Tables\Columns\Column | |
{ | |
return TextColumn::make('zeusData.' . $field->id) | |
->label($field->name) | |
->badge() | |
->getStateUsing(function (Response $record) use ($field) { | |
return json_decode($record->fieldsResponses->where('field_id', $field->id)->first()->response, false); | |
}) | |
->toggleable(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment