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
8 changes: 2 additions & 6 deletions resources/views/infolists/record-entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
<a href="{{ $record['url'] }}"
x-on:click.stop
class="inline-flex items-center gap-2 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors py-0.5 px-1 -mx-1">
@if ($record['avatarUrl'])
<img src="{{ $record['avatarUrl'] }}" alt="" class="h-5 w-5 {{ $record['avatarShape'] }} object-cover shrink-0" />
@endif
@include('custom-fields::tables.columns.partials.record-avatar', ['record' => $record])
<span class="text-sm text-gray-950 dark:text-white underline decoration-gray-300 dark:decoration-gray-600 underline-offset-2">{{ $record['name'] }}</span>
</a>
@else
<div class="inline-flex items-center gap-2">
@if ($record['avatarUrl'])
<img src="{{ $record['avatarUrl'] }}" alt="" class="h-5 w-5 {{ $record['avatarShape'] }} object-cover shrink-0" />
@endif
@include('custom-fields::tables.columns.partials.record-avatar', ['record' => $record])
<span class="text-sm text-gray-950 dark:text-white">{{ $record['name'] }}</span>
</div>
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private function formatRecord(
'name' => $name,
'avatarUrl' => $avatarUrl,
'avatarShape' => $shapeClass,
'icon' => blank($avatarUrl) && $avatarConfig?->hasAttribute() ? $entity->getIcon() : null,
'url' => $url,
];
}
Expand Down
80 changes: 80 additions & 0 deletions tests/Feature/Filament/Components/RecordEntryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Schemas\Schema;
use Livewire\Component;
use Relaticle\CustomFields\Facades\CustomFields;
use Relaticle\CustomFields\Filament\Integration\Components\Infolists\RecordEntry;
use Relaticle\CustomFields\Models\CustomField;
use Relaticle\CustomFields\Models\CustomFieldSection;
use Relaticle\CustomFields\Tests\Fixtures\Models\Post;

mutates(RecordEntry::class);

final class ViewPostCustomFields extends Component implements HasSchemas
{
use InteractsWithSchemas;

public Post $post;

public function infolist(Schema $schema): Schema
{
return $schema
->record($this->post)
->components([CustomFields::infolist()->forModel($this->post)->build()]);
}

public function render(): string
{
return '<div>{{ $this->infolist }}</div>';
}
}

describe('RecordEntry', function (): void {
beforeEach(function (): void {
$this->field = CustomField::factory()->create([
'custom_field_section_id' => CustomFieldSection::factory()->forEntityType(Post::class)->create(['active' => true])->getKey(),
'name' => 'Related Post',
'code' => 'related_post',
'type' => 'record',
'entity_type' => Post::class,
'lookup_type' => Post::class,
]);
});

it('shows the entity icon for a linked record without an image and the image for one with it', function (): void {
registerLookupEntity(Post::class, primaryAttribute: 'title', avatarAttribute: 'cover_url', icon: 'heroicon-o-newspaper');

$pictured = Post::factory()->create(['cover_url' => 'https://example.test/cover.png']);
$unpictured = Post::factory()->create(['cover_url' => null]);

$withImage = Post::factory()->create();
$withImage->saveCustomFieldValue($this->field, $pictured->getKey());

$withoutImage = Post::factory()->create();
$withoutImage->saveCustomFieldValue($this->field, $unpictured->getKey());

livewire(ViewPostCustomFields::class, ['post' => $withImage])
->assertSeeHtml('src="https://example.test/cover.png"')
->assertDontSeeHtml('data-record-icon');

livewire(ViewPostCustomFields::class, ['post' => $withoutImage])
->assertSee($unpictured->title)
->assertSeeHtml('data-record-icon');
});

it('shows no icon for a linked record whose entity has no image', function (): void {
registerLookupEntity(Post::class, primaryAttribute: 'title');

$linked = Post::factory()->create();
$post = Post::factory()->create();
$post->saveCustomFieldValue($this->field, $linked->getKey());

livewire(ViewPostCustomFields::class, ['post' => $post])
->assertSee($linked->title)
->assertDontSeeHtml('data-record-icon');
});
});
Loading