Skip to content

Commit d0d146c

Browse files
Copilotleek
authored andcommitted
feat(Table): add extraRecordLinkAttributes() method
refs #18284
1 parent 8caa7c6 commit d0d146c

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

packages/tables/resources/views/index.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ class="fi-ta-record-checkbox fi-checkbox-input"
11211121
@if ($recordUrl)
11221122
<a
11231123
{{ \Filament\Support\generate_href_html($recordUrl, $openRecordUrlInNewTab, hasNestedClickEventHandler: true) }}
1124-
class="fi-ta-record-content"
1124+
{{ $getExtraRecordLinkAttributeBag($record)->class(['fi-ta-record-content']) }}
11251125
>
11261126
@foreach ($columnsLayout as $columnsLayoutComponent)
11271127
{{
@@ -1979,6 +1979,9 @@ class="fi-ta-record-checkbox fi-checkbox-input"
19791979
<{{ $columnWrapperTag }}
19801980
@if ($columnWrapperTag === 'a')
19811981
{{ \Filament\Support\generate_href_html($columnUrl ?: $recordUrl, $columnUrl ? $column->shouldOpenUrlInNewTab() : $openRecordUrlInNewTab, hasNestedClickEventHandler: true) }}
1982+
@if (blank($columnUrl) && filled($recordUrl))
1983+
{{ $getExtraRecordLinkAttributeBag($record) }}
1984+
@endif
19821985
@elseif ($columnWrapperTag === 'button')
19831986
type="button"
19841987
wire:click.prevent.stop="{{ $columnWireClickAction }}"

packages/tables/src/Table/Concerns/HasRecordUrl.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\View\ComponentAttributeBag;
78

89
trait HasRecordUrl
910
{
1011
protected bool | Closure $shouldOpenRecordUrlInNewTab = false;
1112

1213
protected string | Closure | null $recordUrl = null;
1314

15+
/**
16+
* @var array<array<mixed> | Closure>
17+
*/
18+
protected array $extraRecordLinkAttributes = [];
19+
1420
public function openRecordUrlInNewTab(bool | Closure $condition = true): static
1521
{
1622
$this->shouldOpenRecordUrlInNewTab = $condition;
@@ -59,4 +65,53 @@ public function shouldOpenRecordUrlInNewTab(Model | array $record): bool
5965
] : [],
6066
);
6167
}
68+
69+
/**
70+
* @param array<mixed> | Closure $attributes
71+
*/
72+
public function extraRecordLinkAttributes(array | Closure $attributes, bool $merge = false): static
73+
{
74+
if ($merge) {
75+
$this->extraRecordLinkAttributes[] = $attributes;
76+
} else {
77+
$this->extraRecordLinkAttributes = [$attributes];
78+
}
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* @param Model | array<string, mixed> $record
85+
* @return array<mixed>
86+
*/
87+
public function getExtraRecordLinkAttributes(Model | array $record): array
88+
{
89+
$temporaryAttributeBag = new ComponentAttributeBag;
90+
91+
foreach ($this->extraRecordLinkAttributes as $extraAttributes) {
92+
$temporaryAttributeBag = $temporaryAttributeBag->merge(
93+
$this->evaluate(
94+
$extraAttributes,
95+
namedInjections: [
96+
'record' => $record,
97+
],
98+
typedInjections: ($record instanceof Model) ? [
99+
Model::class => $record,
100+
$record::class => $record,
101+
] : [],
102+
),
103+
escape: false,
104+
);
105+
}
106+
107+
return $temporaryAttributeBag->getAttributes();
108+
}
109+
110+
/**
111+
* @param Model | array<string, mixed> $record
112+
*/
113+
public function getExtraRecordLinkAttributeBag(Model | array $record): ComponentAttributeBag
114+
{
115+
return new ComponentAttributeBag($this->getExtraRecordLinkAttributes($record));
116+
}
62117
}

tests/src/Tables/RecordTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,37 @@
1414
livewire(PostsTable::class)
1515
->assertCanSeeTableRecords($posts);
1616
});
17+
18+
it('can set extra record link attributes', function (): void {
19+
$table = livewire(PostsTable::class)->instance()->getTable();
20+
$table->extraRecordLinkAttributes(['data-test' => 'value']);
21+
22+
$post = Post::factory()->create();
23+
$attributes = $table->getExtraRecordLinkAttributeBag($post);
24+
25+
expect($attributes->get('data-test'))->toBe('value');
26+
});
27+
28+
it('can set dynamic extra record link attributes', function (): void {
29+
$table = livewire(PostsTable::class)->instance()->getTable();
30+
$table->extraRecordLinkAttributes(fn ($record) => [
31+
'data-id' => (string) $record->getKey(),
32+
]);
33+
34+
$post = Post::factory()->create();
35+
$attributes = $table->getExtraRecordLinkAttributeBag($post);
36+
37+
expect($attributes->get('data-id'))->toBe((string) $post->getKey());
38+
});
39+
40+
it('can merge extra record link attributes', function (): void {
41+
$table = livewire(PostsTable::class)->instance()->getTable();
42+
$table->extraRecordLinkAttributes(['data-test' => 'value1']);
43+
$table->extraRecordLinkAttributes(['data-test2' => 'value2'], merge: true);
44+
45+
$post = Post::factory()->create();
46+
$attributes = $table->getExtraRecordLinkAttributeBag($post);
47+
48+
expect($attributes->get('data-test'))->toBe('value1')
49+
->and($attributes->get('data-test2'))->toBe('value2');
50+
});

0 commit comments

Comments
 (0)