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
34 changes: 34 additions & 0 deletions resources/views/forms/record-select-input.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
$selectedIds = is_array($state) ? array_filter($state) : [];
$initialRecords = $getRecordsByIds($selectedIds);
$initialOptions = $getInitialOptions();
$fallbackIcon = $getFallbackIcon();
@endphp

<x-dynamic-component
Expand Down Expand Up @@ -466,6 +467,17 @@ class="h-5 w-5 object-cover shrink-0"
alt=""
/>
</template>
@if ($fallbackIcon)
<template x-if="! selectedRecords[0].avatar">
<span
data-record-icon
:class="selectedRecords[0].avatarShape || 'rounded-full'"
class="flex h-5 w-5 shrink-0 items-center justify-center bg-gray-100 text-gray-600 ring-1 ring-gray-950/5 dark:bg-white/5 dark:text-gray-300 dark:ring-white/10"
>
<x-filament::icon :icon="$fallbackIcon" class="h-3 w-3" />
</span>
</template>
@endif
<span class="text-sm text-gray-950 dark:text-white truncate" x-text="selectedRecords[0].label"></span>
</div>
</template>
Expand Down Expand Up @@ -532,6 +544,17 @@ class="h-5 w-5 object-cover"
alt=""
/>
</template>
@if ($fallbackIcon)
<template x-if="! record.avatar">
<span
data-record-icon
:class="record.avatarShape || 'rounded-full'"
class="flex h-5 w-5 shrink-0 items-center justify-center bg-gray-100 text-gray-600 ring-1 ring-gray-950/5 dark:bg-white/5 dark:text-gray-300 dark:ring-white/10"
>
<x-filament::icon :icon="$fallbackIcon" class="h-3 w-3" />
</span>
</template>
@endif
<span x-text="record.label" class="truncate max-w-[100px]"></span>
<button
type="button"
Expand Down Expand Up @@ -629,6 +652,17 @@ class="h-5 w-5 object-cover shrink-0"
alt=""
/>
</template>
@if ($fallbackIcon)
<template x-if="! record.avatar">
<span
data-record-icon
:class="record.avatarShape || 'rounded-full'"
class="flex h-5 w-5 shrink-0 items-center justify-center bg-gray-100 text-gray-600 ring-1 ring-gray-950/5 dark:bg-white/5 dark:text-gray-300 dark:ring-white/10"
>
<x-filament::icon :icon="$fallbackIcon" class="h-3 w-3" />
</span>
</template>
@endif

{{-- Label --}}
<span
Expand Down
14 changes: 14 additions & 0 deletions resources/views/tables/columns/partials/record-avatar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@if ($record['avatarUrl'])
<img
src="{{ $record['avatarUrl'] }}"
alt=""
class="h-5 w-5 {{ $record['avatarShape'] }} object-cover shrink-0"
/>
@elseif ($record['icon'])
<span
data-record-icon
class="flex h-5 w-5 {{ $record['avatarShape'] }} shrink-0 items-center justify-center bg-gray-100 text-gray-600 ring-1 ring-gray-950/5 dark:bg-white/5 dark:text-gray-300 dark:ring-white/10"
>
<x-filament::icon :icon="$record['icon']" class="h-3 w-3" />
</span>
@endif
50 changes: 7 additions & 43 deletions resources/views/tables/columns/record-column.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
x-on:keydown.enter.prevent="togglePanel()"
x-on:keydown.space.prevent="togglePanel()"
@endif
class="relative {{ $hiddenCount > 0 ? 'cursor-pointer' : '' }}"
class="fi-ta-text relative {{ $hiddenCount > 0 ? 'cursor-pointer' : '' }}"
>
@if (empty($records))
{{-- Empty: no value --}}
Expand All @@ -44,24 +44,12 @@ class="relative {{ $hiddenCount > 0 ? 'cursor-pointer' : '' }}"
x-on:click.stop
class="inline-flex items-center gap-2 py-0.5 px-1 -mx-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors shrink-0"
>
@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 truncate max-w-[120px]">{{ $record['name'] }}</span>
</a>
@else
<span class="inline-flex items-center gap-2 py-0.5 px-1 shrink-0">
@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 truncate max-w-[120px]">{{ $record['name'] }}</span>
</span>
@endif
Expand Down Expand Up @@ -97,24 +85,12 @@ class="px-2 py-1 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors"
x-on:click.stop
class="inline-flex items-center gap-2 py-0.5 px-1 -mx-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors w-full"
>
@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 truncate">{{ $record['name'] }}</span>
</a>
@else
<div class="inline-flex items-center gap-2 py-0.5 px-1 -mx-1 w-full">
@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 truncate">{{ $record['name'] }}</span>
</div>
@endif
Expand All @@ -134,24 +110,12 @@ class="h-5 w-5 {{ $record['avatarShape'] }} object-cover shrink-0"
x-on:click.stop
class="inline-flex items-center gap-2 py-0.5 px-1 -mx-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
>
@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 truncate">{{ $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 truncate">{{ $record['name'] }}</span>
</div>
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ public function getEntityConfiguration(): ?EntityConfigurationData
return Entities::getEntity($lookupType);
}

public function getFallbackIcon(): ?string
{
$entity = $this->getEntityConfiguration();

if (! $entity?->getAvatarConfiguration()?->hasAttribute()) {
return null;
}

return $entity->getIcon();
}

/**
* Prepare entity query with common attributes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private function formatRecord(Model $record): array
'name' => $name,
'avatarUrl' => $avatarUrl,
'avatarShape' => $shapeClass,
'icon' => blank($avatarUrl) && $this->avatarConfig?->hasAttribute() ? $this->entity?->getIcon() : null,
'url' => $url,
];
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Filament/Components/RecordSelectSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,26 @@
->assertSee('minSearchLength: 3', escape: false)
->assertSee('Type at least 3 characters to search', escape: false);
});

it('offers the entity icon in place of a missing image when the entity has one', function (bool $hasImageSetting): void {
registerLookupEntity(Post::class, primaryAttribute: 'title', avatarAttribute: $hasImageSetting ? 'cover_url' : null, icon: 'heroicon-o-newspaper');

$section = CustomFieldSection::factory()->forEntityType(Post::class)->create();

CustomField::factory()->create([
'code' => 'related_post',
'name' => 'Related Post',
'type' => 'record',
'entity_type' => Post::class,
'lookup_type' => Post::class,
'custom_field_section_id' => $section->getKey(),
]);

$html = livewire(EditPost::class, ['record' => makeLookupRecord('Acme Industries')->getRouteKey()])->html();

expect(str_contains($html, 'data-record-icon'))->toBe($hasImageSetting);
})->with([
'with an image setting' => [true],
'without an image setting' => [false],
]);
});
37 changes: 37 additions & 0 deletions tests/Feature/Integration/Resources/Pages/ListRecordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,40 @@
->assertCanNotSeeTableRecords([$archived]);
});
});

describe('Record column avatars', 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,
'settings' => new CustomFieldSettingsData(visible_in_list: true, list_toggleable_hidden: false),
]);
});

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]);
Post::factory()->create()->saveCustomFieldValue($this->field, $pictured->getKey());
Post::factory()->create()->saveCustomFieldValue($this->field, $unpictured->getKey());

$html = livewire(ListPosts::class)->html();

expect(substr_count($html, 'src="https://example.test/cover.png"'))->toBe(1)
->and(substr_count($html, 'data-record-icon'))->toBe(1);
});

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::factory()->create()->saveCustomFieldValue($this->field, $linked->getKey());

expect(livewire(ListPosts::class)->html())->not->toContain('data-record-icon');
});
});
4 changes: 3 additions & 1 deletion tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @param array<int, string> $searchAttributes
*/
function registerLookupEntity(string $modelClass, string $primaryAttribute, array $searchAttributes = []): void
function registerLookupEntity(string $modelClass, string $primaryAttribute, array $searchAttributes = [], ?string $avatarAttribute = null, string $icon = 'heroicon-o-document'): void
{
config()->set('custom-fields.entity_configuration',
EntityConfigurator::configure()
Expand All @@ -25,9 +25,11 @@ function registerLookupEntity(string $modelClass, string $primaryAttribute, arra
->models([
EntityModel::configure(
modelClass: $modelClass,
icon: $icon,
primaryAttribute: $primaryAttribute,
searchAttributes: $searchAttributes,
features: [EntityFeature::CUSTOM_FIELDS, EntityFeature::LOOKUP_SOURCE],
avatarConfiguration: $avatarAttribute === null ? null : EntityModel::avatar(attribute: $avatarAttribute),
),
])
);
Expand Down
1 change: 1 addition & 0 deletions tests/database/migrations/create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up(): void
$table->unsignedTinyInteger('rating')->default(0);
$table->json('tags')->nullable();
$table->string('title');
$table->string('cover_url')->nullable();
$table->json('json')->nullable();
$table->json('json_array_of_objects')->nullable();
$table->string('string_backed_enum')->nullable();
Expand Down
Loading