Skip to content

Commit cee5683

Browse files
Fix components (#24)
* 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]>
1 parent 431373d commit cee5683

File tree

8 files changed

+75
-46
lines changed

8 files changed

+75
-46
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ You can use the `Sidebar` component to split the form into two distinct sections
3232
```php
3333
use RalphJSmit\Filament\Components\Forms\Sidebar;
3434

35-
Sidebar::make()->schema([
35+
Sidebar::make([
3636
// Components for the main section here
3737
],[
3838
// Components for the sidebar section here
39-
])->getSchema()[0]
39+
])
4040
```
4141

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

4444
```php
4545
use Filament\Forms\Components\Card;
@@ -50,18 +50,20 @@ use RalphJSmit\Filament\Components\Forms\Sidebar;
5050

5151
public static function form(Form $form): Form
5252
{
53-
return Sidebar::make($form)->schema([
54-
Card::make([
55-
TextInput::make('title')->label('Title'),
53+
return $form->schema([
54+
Sidebar::make([
55+
Card::make([
56+
TextInput::make('title')->label('Title'),
57+
// ...
58+
]),
5659
// ...
57-
]),
58-
// ...
59-
], [
60-
Card::make([
61-
...Timestamps::make(),
60+
], [
61+
Card::make([
62+
...Timestamps::make(),
63+
// ...
64+
]),
6265
// ...
6366
]),
64-
// ...
6567
]);
6668
}
6769
```

UPGRADE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Upgrade
2+
3+
## From 1.x to 2.x
4+
5+
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:
6+
7+
```diff
8+
-public static function form(Form $form): Form
9+
-{
10+
- return Sidebar::make($form)->schema([
11+
- // Main components
12+
- ], [
13+
- // Sidebar components
14+
- ]);
15+
-}
16+
+public static function form(Form $form): Form
17+
+{
18+
+ return $form->schema([
19+
+ Sidebar::make([
20+
+ // Main components
21+
+ ], [
22+
+ // Sidebar components
23+
+ ]),
24+
+ ]);
25+
+}

src/Forms/Sidebar.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,21 @@
22

33
namespace RalphJSmit\Filament\Components\Forms;
44

5+
use Closure;
56
use Filament\Forms\Components\Grid;
6-
use Filament\Forms\Form;
7-
use Filament\Resources\Form as FormV2;
87

98
class Sidebar
109
{
1110
public function __construct(
12-
public FormV2|Form $form,
11+
public array|Closure $mainComponents,
12+
public array|Closure $sidebarComponents
1313
) {}
1414

15-
public static function make(FormV2|Form|null $form = null): static
15+
public static function make(array|Closure $mainComponents, array|Closure $sidebarComponents): Grid
1616
{
17-
if ( ! $form ) {
18-
$form = match ( true ) {
19-
class_exists(FormV2::class) => FormV2::make(),
20-
default => Form::make(),
21-
};
22-
}
23-
24-
return new static(form: $form);
25-
}
26-
27-
public function schema(array $mainComponents, array $sidebarComponents): FormV2|Form
28-
{
29-
return $this->form->schema([
30-
Grid::make(['sm' => 3])->schema([
17+
return Grid::make(['sm' => 3])->schema([
3118
Grid::make()->schema($mainComponents)->columnSpan(['sm' => 2]),
3219
Grid::make()->schema($sidebarComponents)->columnSpan(['sm' => 1]),
33-
]),
34-
]);
20+
]);
3521
}
36-
}
22+
}

tests/Forms/CreatedAtTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
testTime()->freeze();
3333

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

3838
$component
@@ -48,10 +48,10 @@
4848
testTime()->freeze();
4949

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

5454
$component
5555
->assertSet('record.created_at', fn (Carbon $value) => $value !== null && $value->gt(now()->subMinutes(10)->subSecond()))
5656
->assertSee('10 minutes ago');
57-
});
57+
});

tests/Forms/SidebarTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
it('can create a sidebar', function () {
1010
TestableForm::$formSchema = [
11-
Sidebar::make()->schema(
11+
Sidebar::make(
1212
mainComponents: [
1313
\Filament\Forms\Components\Placeholder::make('dummy_placeholder'),
1414
\Filament\Forms\Components\Placeholder::make('dummy_placeholder_2'),
@@ -17,7 +17,7 @@
1717
Filament\Forms\Components\TextInput::make('name')->label('Test label for sidebar component'),
1818
Filament\Forms\Components\TextInput::make('name')->label('Test label 2 for sidebar component'),
1919
]
20-
)->getSchema()[0],
20+
),
2121
];
2222

2323
testTime()->freeze();
@@ -29,4 +29,4 @@
2929
->assertSee('dummy_placeholder_2')
3030
->assertSee('Test label for sidebar component')
3131
->assertSee('Test label 2 for sidebar component');
32-
});
32+
});

tests/Forms/TimestampsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
testTime()->freeze();
1616

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

2121
$component
2222
->assertSet('record', $record)
2323
->assertSee('10 minutes ago')
2424
->assertSee('15 minutes ago');
25-
});
25+
});

tests/Forms/UpdatedAtTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
testTime()->freeze();
3333

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

3838
$component
@@ -48,10 +48,10 @@
4848
testTime()->freeze();
4949

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

5454
$component
5555
->assertSet('record.updated_at', fn (Carbon $value) => $value !== null && $value->gt(now()->subMinutes(10)->subSecond()))
5656
->assertSee('10 minutes ago');
57-
});
57+
});

tests/TestCase.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Filament\Forms\FormsServiceProvider;
66
use Filament\Support\SupportServiceProvider;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
79
use Livewire\LivewireServiceProvider;
810
use Orchestra\Testbench\TestCase as Orchestra;
911
use RalphJSmit\Filament\Components\FilamentComponentsServiceProvider;
@@ -13,6 +15,8 @@ class TestCase extends Orchestra
1315
protected function setUp(): void
1416
{
1517
parent::setUp();
18+
19+
$this->setUpDatabase();
1620
}
1721

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

2832
public function getEnvironmentSetUp($app): void
2933
{
30-
//
34+
$app['config']->set('database.default', 'sqlite');
35+
$app['config']->set('database.connections.sqlite', [
36+
'driver' => 'sqlite',
37+
'database' => ':memory:',
38+
'prefix' => '',
39+
]);
40+
}
41+
42+
protected function setUpDatabase()
43+
{
44+
$this->app->get('db')->connection()->getSchemaBuilder()->create('records', function (Blueprint $table) {
45+
$table->timestamps();
46+
});
3147
}
32-
}
48+
}

0 commit comments

Comments
 (0)