Skip to content

Commit

Permalink
Merge pull request #53 from pvdptje/patch-2
Browse files Browse the repository at this point in the history
Added a method to dynamicly resolve a field from the field type
  • Loading branch information
joaopaulolndev authored Nov 2, 2024
2 parents 013b291 + ebb6727 commit 45f63c8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Livewire/CustomFieldsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Joaopaulolndev\FilamentEditProfile\Concerns\HasUser;

use Throwable;
class CustomFieldsForm extends BaseProfileForm
{
use HasUser;
Expand Down Expand Up @@ -54,7 +54,7 @@ public function form(Form $form): Form
->statePath('data');
}

private static function createField(string $fieldKey, array $field): ?Forms\Components\Component
private static function createField(string $fieldKey, array | Closure $field): ?Forms\Components\Component
{
switch ($field['type']) {
case 'text':
Expand All @@ -70,10 +70,26 @@ private static function createField(string $fieldKey, array $field): ?Forms\Comp
case 'datetime':
return self::createDateTimePicker($fieldKey, $field);
default:
return null;
return self::createFieldFromString($fieldKey, $field);
}
}

private static function createFieldFromString(string $fieldKey, array $field): ?Forms\Components\Component
{
try {

$class = \Illuminate\Support\Str::camel($field['type']);
$class = "Filament\Forms\Components\\{$class}";

return $class::make($fieldKey)
->label(__($field['label']))
->placeholder(__($field['placeholder']))
->required($field['required'])
->rules($field['rules']);

} catch (Throwable $exception) {}
}

private static function createTextInput(string $fieldKey, array $field): TextInput
{
return TextInput::make($fieldKey)
Expand Down

0 comments on commit 45f63c8

Please sign in to comment.