Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions database/migrations/add_schema_to_filament_form_fields.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('filament_form_fields', function (Blueprint $table) {
$table->text('hint')->nullable()->change();
$table->boolean('required')->default(false)->change();
$table->json('schema')->nullable()->after('options');
});
}
Comment on lines +8 to +19
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work with an existing project do we need to do anything to publish this migration? If we publish migrations from this package, will it duplicate them?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new migration on the project with this in it for now.


/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('filament_form_fields', function (Blueprint $table) {
$table->string('hint')->nullable()->change();
$table->boolean('required')->change();
$table->dropColumn('schema');
});
}
};
18 changes: 18 additions & 0 deletions resources/views/forms/components/heading.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@php
$heading = $getLabel();
$subheading = $getHint();
@endphp

<div class="space-y-2">
@if ($heading)
<h2 class="text-2xl font-bold tracking-tight">
{{ $heading }}
</h2>
@endif

@if ($subheading)
<p class="text-gray-500">
{{ $subheading }}
</p>
@endif
</div>
2 changes: 1 addition & 1 deletion resources/views/livewire/filament-form-user/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="flex flex-row justify-center pt-16 filament-form-builder">
<div class="pt-16 filament-form-builder">
<div class="fb-form-user-container border rounded-xl py-4 px-8">
{{ $this->entryInfoList }}
</div>
Expand Down
10 changes: 10 additions & 0 deletions src/Enums/FilamentFieldTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ enum FilamentFieldTypeEnum implements HasLabel
case MARKDOWN_EDITOR;
case COLOR_PICKER;
case FILE_UPLOAD;
case REPEATER;
case HEADING;

public static function fromString(string $type): ?self
{
Expand Down Expand Up @@ -54,6 +56,8 @@ public function fieldName(): string
self::MARKDOWN_EDITOR => 'Markdown Editor',
self::COLOR_PICKER => 'Color Picker',
self::FILE_UPLOAD => 'File Upload',
self::REPEATER => 'Repeater',
self::HEADING => 'Heading',
};
}

Expand All @@ -74,6 +78,8 @@ public function className(): string
self::MARKDOWN_EDITOR => 'Filament\Forms\Components\MarkdownEditor',
self::COLOR_PICKER => 'Filament\Forms\Components\ColorPicker',
self::FILE_UPLOAD => 'Filament\Forms\Components\SpatieMediaLibraryFileUpload',
self::REPEATER => 'Filament\Forms\Components\Repeater',
self::HEADING => 'Tapp\FilamentFormBuilder\Filament\Forms\Components\Heading',
};
}

Expand All @@ -94,6 +100,8 @@ public function hasOptions(): bool
self::MARKDOWN_EDITOR => false,
self::COLOR_PICKER => false,
self::FILE_UPLOAD => false,
self::REPEATER => false,
self::HEADING => false,
};
}

Expand All @@ -114,6 +122,8 @@ public function isBool(): bool
self::MARKDOWN_EDITOR => false,
self::COLOR_PICKER => false,
self::FILE_UPLOAD => false,
self::REPEATER => false,
self::HEADING => false,
};
}
}
34 changes: 25 additions & 9 deletions src/Exports/FilamentFormUsersExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@
$entry->updated_at,
];

foreach ($this->form->filamentFormFields as $field) {
/** @phpstan-ignore-next-line */
$entriesFieldKey = array_search($field->id, array_column($entry->entry, 'field_id'));
// Get all headings after the first three default ones
$fieldHeadings = array_slice($this->headings(), 3);

if ($entriesFieldKey === false) {
array_push($mapping, '');
// Create a lookup array of field labels to answers for this entry
$entryAnswers = collect($entry->entry)->mapWithKeys(function ($fieldEntry) {
return [$fieldEntry['field'] => $fieldEntry['answer']];
});

// For each field heading, add the corresponding answer or empty string
foreach ($fieldHeadings as $heading) {
$answer = $entryAnswers->get($heading);

if (is_array($answer)) {
array_push($mapping, json_encode($answer));
} else {
array_push($mapping, $entry->entry[$entriesFieldKey]['answer']);
array_push($mapping, $answer ?? '');
}
}

Expand All @@ -58,11 +66,19 @@
'updated_at',
];

foreach ($this->form->filamentFormFields as $field) {
/** @phpstan-ignore-next-line */
array_push($headings, $field->label);
// Get all unique field labels from all entries
$allFieldLabels = collect();
foreach ($this->entries as $entry) {
foreach ($entry->entry as $fieldEntry) {

Check failure on line 72 in src/Exports/FilamentFormUsersExport.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Database\Eloquent\Model::$entry.
if (isset($fieldEntry['field'])) {
$allFieldLabels->push($fieldEntry['field']);
}
}
}

// Add unique field labels to headings
$headings = array_merge($headings, $allFieldLabels->unique()->values()->toArray());

return $headings;
}
}
17 changes: 17 additions & 0 deletions src/Filament/Forms/Components/Heading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tapp\FilamentFormBuilder\Filament\Forms\Components;

use Filament\Forms\Components\Field;

class Heading extends Field
{
protected string $view = 'filament-form-builder::forms.components.heading';

Check failure on line 9 in src/Filament/Forms/Components/Heading.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Tapp\FilamentFormBuilder\Filament\Forms\Components\Heading::$view (view-string) does not accept default value of type string.

protected function setUp(): void
{
parent::setUp();

$this->disabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\RelationManagers;

use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
Expand All @@ -30,13 +32,21 @@ public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('label')
->required()
->maxLength(255),
Select::make('type')
->options(FilamentFieldTypeEnum::class)
->options(function () {
return collect(FilamentFieldTypeEnum::cases())
->mapWithKeys(fn ($type) => [$type->name => $type->fieldName()])
->sortBy(fn ($label, $key) => $label)
->toArray();
})
->required()
->live(),
TextInput::make('label')
->required()
->maxLength(255)
->label(function (Get $get) {
return $get('type') === FilamentFieldTypeEnum::HEADING->name ? 'Heading' : 'Label';
}),
TagsInput::make('options')
->placeholder('Add options')
->hint('Press enter after inputting each option')
Expand All @@ -47,16 +57,65 @@ public function form(Form $form): Form

return false;
}),
TextInput::make('hint'),
Textarea::make('hint')
->label(function (Get $get) {
return $get('type') === FilamentFieldTypeEnum::HEADING->name ? 'Subheading' : 'Hint';
}),
TagsInput::make('rules')
->placeholder('Add rules')
->hint('view list of available rules here, https://laravel.com/docs/11.x/validation#available-validation-rules'),
->hint('view list of available rules here, https://laravel.com/docs/11.x/validation#available-validation-rules')
->visible(function (Get $get) {
return $get('type') !== FilamentFieldTypeEnum::REPEATER->name
&& $get('type') !== FilamentFieldTypeEnum::HEADING->name;
}),
TextInput::make('order')
->default(function () {
return $this->getOwnerRecord()->filamentFormFields()->count() + 1;
})
->numeric(),
Toggle::make('required'),
Toggle::make('required')
->visible(function (Get $get) {
return $get('type') !== FilamentFieldTypeEnum::REPEATER->name
&& $get('type') !== FilamentFieldTypeEnum::HEADING->name;
}),
Repeater::make('schema')
->label('Fields')
->schema([
TextInput::make('label')
->required()
->maxLength(255),
Select::make('type')
->options(function () {
$options = collect(FilamentFieldTypeEnum::cases())
->filter(fn ($type) => $type !== FilamentFieldTypeEnum::REPEATER)
->mapWithKeys(fn ($type) => [$type->name => $type->fieldName()])
->toArray();

return $options;
})
->required()
->live(),
TagsInput::make('options')
->placeholder('Add options')
->hint('Press enter after inputting each option')
->visible(function (Get $get) {
if ($get('type')) {
return FilamentFieldTypeEnum::fromString($get('type'))->hasOptions();
}

return false;
}),
Textarea::make('hint'),
TagsInput::make('rules')
->placeholder('Add rules')
->hint('view list of available rules here, https://laravel.com/docs/11.x/validation#available-validation-rules'),
Toggle::make('required'),
])
->columns(2)
->columnSpanFull()
->visible(function (Get $get) {
return $get('type') === FilamentFieldTypeEnum::REPEATER->name;
}),
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/FilamentFormBuilderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function configurePackage(Package $package): void
{
$package->name('filament-form-builder')
->hasMigration('create_dynamic_filament_form_tables')
->hasMigration('add_schema_to_filament_form_fields')
->hasConfigFile('filament-form-builder')
->hasRoute('routes')
->hasViews('filament-form-builder');
Expand Down
Loading
Loading