Created
October 9, 2024 10:19
-
-
Save atmonshi/fb65dce9833bcd114f07b1938f1f877c to your computer and use it in GitHub Desktop.
adding a Repeater to Zeus Bolt
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 LaraZeus\BoltPro\Fields; | |
use Filament\Forms\Components\Hidden; | |
use Filament\Forms\Components\TextInput; | |
use Filament\Tables\Columns\Column; | |
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 Repeater extends FieldsContract | |
{ | |
public string $renderClass = \Filament\Forms\Components\Repeater::class; | |
public int $sort = 103; | |
public function title(): string | |
{ | |
return __('Dynamic Textbox'); | |
} | |
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([ | |
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(), | |
]), | |
Bolt::getCustomSchema('field', resolve(static::class)) ?? [], | |
]), | |
]; | |
} | |
public static function getOptionsHidden(): array | |
{ | |
return [ | |
...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], | |
self::hiddenHtmlID(), | |
self::hiddenRequired(), | |
Hidden::make('options.minItems'), | |
Hidden::make('options.maxItems'), | |
Hidden::make('options.defaultItems'), | |
]; | |
} | |
// @phpstan-ignore-next-line | |
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false) | |
{ | |
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility); | |
$feildoptions = $zeusField['options']; | |
if (filled($feildoptions['minItems'])) { | |
$component->minItems($feildoptions['minItems']); | |
} | |
if (filled($feildoptions['maxItems'])) { | |
$component->maxItems($feildoptions['maxItems']); | |
} | |
if (filled($feildoptions['defaultItems'])) { | |
$component->defaultItems($feildoptions['defaultItems']); | |
} | |
$repeatedField = TextInput::make('title'); | |
if (filled($feildoptions['is_required'])) { | |
$repeatedField->required()->default(''); | |
} | |
$component | |
->simple($repeatedField) | |
->reorderableWithDragAndDrop(false) | |
->reorderableWithButtons() | |
->cloneable() | |
->addActionLabel(__('Add')) | |
->default(['']); | |
return $component; | |
} | |
public function getResponse(Field $field, FieldResponse $resp): string | |
{ | |
$responseValue = (filled($resp->response) && Bolt::isJson($resp->response)) ? json_decode($resp->response) : [$resp->response]; | |
return view('zeus::filament.fields.repeater') | |
->with('resp', $resp) | |
->with('responseValue', $responseValue) | |
->with('field', $field) | |
->render(); | |
} | |
public function TableColumn(Field $field): ?Column | |
{ | |
return TextColumn::make('zeusData.' . $field->id) | |
->sortable(false) | |
->label($field->name) | |
->badge() | |
->getStateUsing(function (Response $record) use ($field) { | |
return json_decode($record->fieldsResponses->where('field_id', $field->id)->first()->response); | |
}) | |
->toggleable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment