Skip to content

Commit

Permalink
Fix components (#24)
Browse files Browse the repository at this point in the history
* fix `UpdatedAtTest`

* fix `CreatedAtTest`

* fix `TimestampsTest`

* fix `Sidebar`

* update docs

* create UPGRADE.md

* formatting

* update README.md

* WIP

* Update README.md

---------

Co-authored-by: Ralph J. Smit <[email protected]>
  • Loading branch information
alexmanase and ralphjsmit authored Sep 12, 2023
1 parent 431373d commit cee5683
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 46 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ You can use the `Sidebar` component to split the form into two distinct sections
```php
use RalphJSmit\Filament\Components\Forms\Sidebar;

Sidebar::make()->schema([
Sidebar::make([
// Components for the main section here
],[
// Components for the sidebar section here
])->getSchema()[0]
])
```

If you're using it in the Admin panel, you can directly return the `Sidebar` component from the `form()` in your resource:
If you're using it in the Admin panel, you can use the `Sidebar` in your `form()` method:

```php
use Filament\Forms\Components\Card;
Expand All @@ -50,18 +50,20 @@ use RalphJSmit\Filament\Components\Forms\Sidebar;

public static function form(Form $form): Form
{
return Sidebar::make($form)->schema([
Card::make([
TextInput::make('title')->label('Title'),
return $form->schema([
Sidebar::make([
Card::make([
TextInput::make('title')->label('Title'),
// ...
]),
// ...
]),
// ...
], [
Card::make([
...Timestamps::make(),
], [
Card::make([
...Timestamps::make(),
// ...
]),
// ...
]),
// ...
]);
}
```
Expand Down
25 changes: 25 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Upgrade

## From 1.x to 2.x
The `Sidebar::make()` method's signature was changed. Previously it accepted a `$form` parameter and returned a form. Now you can just use the `Sidebar::make()` method inside any form schema:

```diff
-public static function form(Form $form): Form
-{
- return Sidebar::make($form)->schema([
- // Main components
- ], [
- // Sidebar components
- ]);
-}
+public static function form(Form $form): Form
+{
+ return $form->schema([
+ Sidebar::make([
+ // Main components
+ ], [
+ // Sidebar components
+ ]),
+ ]);
+}
28 changes: 7 additions & 21 deletions src/Forms/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,21 @@

namespace RalphJSmit\Filament\Components\Forms;

use Closure;
use Filament\Forms\Components\Grid;
use Filament\Forms\Form;
use Filament\Resources\Form as FormV2;

class Sidebar
{
public function __construct(
public FormV2|Form $form,
public array|Closure $mainComponents,
public array|Closure $sidebarComponents
) {}

public static function make(FormV2|Form|null $form = null): static
public static function make(array|Closure $mainComponents, array|Closure $sidebarComponents): Grid
{
if ( ! $form ) {
$form = match ( true ) {
class_exists(FormV2::class) => FormV2::make(),
default => Form::make(),
};
}

return new static(form: $form);
}

public function schema(array $mainComponents, array $sidebarComponents): FormV2|Form
{
return $this->form->schema([
Grid::make(['sm' => 3])->schema([
return Grid::make(['sm' => 3])->schema([
Grid::make()->schema($mainComponents)->columnSpan(['sm' => 2]),
Grid::make()->schema($sidebarComponents)->columnSpan(['sm' => 1]),
]),
]);
]);
}
}
}
6 changes: 3 additions & 3 deletions tests/Forms/CreatedAtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
testTime()->freeze();

$component = Livewire::test(TestableForm::class, [
'record' => $record = Record::factory()->make(['created_at' => null]),
'record' => $record = Record::factory()->create(['created_at' => null]),
]);

$component
Expand All @@ -48,10 +48,10 @@
testTime()->freeze();

$component = Livewire::test(TestableForm::class, [
'record' => $record = Record::factory()->make(['created_at' => now()->subMinutes(10)]),
'record' => $record = Record::factory()->create(['created_at' => now()->subMinutes(10)]),
]);

$component
->assertSet('record.created_at', fn (Carbon $value) => $value !== null && $value->gt(now()->subMinutes(10)->subSecond()))
->assertSee('10 minutes ago');
});
});
6 changes: 3 additions & 3 deletions tests/Forms/SidebarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

it('can create a sidebar', function () {
TestableForm::$formSchema = [
Sidebar::make()->schema(
Sidebar::make(
mainComponents: [
\Filament\Forms\Components\Placeholder::make('dummy_placeholder'),
\Filament\Forms\Components\Placeholder::make('dummy_placeholder_2'),
Expand All @@ -17,7 +17,7 @@
Filament\Forms\Components\TextInput::make('name')->label('Test label for sidebar component'),
Filament\Forms\Components\TextInput::make('name')->label('Test label 2 for sidebar component'),
]
)->getSchema()[0],
),
];

testTime()->freeze();
Expand All @@ -29,4 +29,4 @@
->assertSee('dummy_placeholder_2')
->assertSee('Test label for sidebar component')
->assertSee('Test label 2 for sidebar component');
});
});
4 changes: 2 additions & 2 deletions tests/Forms/TimestampsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
testTime()->freeze();

$component = Livewire::test(TestableForm::class, [
'record' => $record = Record::factory()->make(['created_at' => now()->subMinutes(10), 'updated_at' => now()->subMinutes(15)]),
'record' => $record = Record::factory()->create(['created_at' => now()->subMinutes(10), 'updated_at' => now()->subMinutes(15)]),
]);

$component
->assertSet('record', $record)
->assertSee('10 minutes ago')
->assertSee('15 minutes ago');
});
});
6 changes: 3 additions & 3 deletions tests/Forms/UpdatedAtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
testTime()->freeze();

$component = Livewire::test(TestableForm::class, [
'record' => $record = Record::factory()->make(['updated_at' => null]),
'record' => $record = Record::factory()->create(['updated_at' => null]),
]);

$component
Expand All @@ -48,10 +48,10 @@
testTime()->freeze();

$component = Livewire::test(TestableForm::class, [
'record' => $record = Record::factory()->make(['updated_at' => now()->subMinutes(10)]),
'record' => $record = Record::factory()->create(['updated_at' => now()->subMinutes(10)]),
]);

$component
->assertSet('record.updated_at', fn (Carbon $value) => $value !== null && $value->gt(now()->subMinutes(10)->subSecond()))
->assertSee('10 minutes ago');
});
});
20 changes: 18 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Filament\Forms\FormsServiceProvider;
use Filament\Support\SupportServiceProvider;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use RalphJSmit\Filament\Components\FilamentComponentsServiceProvider;
Expand All @@ -13,6 +15,8 @@ class TestCase extends Orchestra
protected function setUp(): void
{
parent::setUp();

$this->setUpDatabase();
}

protected function getPackageProviders(mixed $app): array
Expand All @@ -27,6 +31,18 @@ protected function getPackageProviders(mixed $app): array

public function getEnvironmentSetUp($app): void
{
//
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

protected function setUpDatabase()
{
$this->app->get('db')->connection()->getSchemaBuilder()->create('records', function (Blueprint $table) {
$table->timestamps();
});
}
}
}

0 comments on commit cee5683

Please sign in to comment.