Skip to content

Commit 86d04fc

Browse files
committed
feat: support component instance when using addView()
1 parent c9a01e2 commit 86d04fc

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Html/Layout.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Yajra\DataTables\Html;
66

77
use Illuminate\Contracts\Support\Renderable;
8+
use Illuminate\Support\Facades\Blade;
89
use Illuminate\Support\Fluent;
910
use Illuminate\Support\Traits\Macroable;
11+
use Illuminate\View\Component;
1012
use InvalidArgumentException;
1113
use Livewire\Livewire;
1214
use Throwable;
@@ -97,17 +99,16 @@ public function bottomEndView(string $selector, ?int $order = null): static
9799
* @throws Throwable
98100
*/
99101
public function addView(
100-
Renderable|string $view,
102+
Component|Renderable|string $view,
101103
LayoutPosition $layoutPosition,
102104
?int $order = null
103105
): static {
104-
if ($view instanceof Renderable) {
105-
$html = $view->render();
106-
} else {
107-
// Support for inline component views
108-
$html = strip_tags($view) !== $view ? $view : view($view)->render();
106+
if ($view instanceof Component) {
107+
$view = Blade::renderComponent($view);
109108
}
110109

110+
$html = $view instanceof Renderable ? $view->render() : Blade::render($view);
111+
111112
$element = json_encode($html);
112113

113114
if ($element === false) {

tests/LayoutTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ public function it_can_accept_view_instance_or_string_for_layout_content(): void
193193
$view = view('test-view');
194194

195195
$builder->layout(fn (Layout $layout) => $layout
196+
->addView(
197+
view: new TestView(),
198+
layoutPosition: LayoutPosition::Top,
199+
)
200+
->addView(
201+
view: new TestInlineView(),
202+
layoutPosition: LayoutPosition::Bottom,
203+
)
196204
->addView(
197205
view: $view,
198206
layoutPosition: LayoutPosition::TopStart,
@@ -216,6 +224,19 @@ public function it_can_accept_view_instance_or_string_for_layout_content(): void
216224
);
217225

218226
$this->assertArrayHasKey('layout', $builder->getAttributes());
227+
$this->assertCount(6, $builder->getAttributes()['layout']);
228+
229+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
230+
$this->assertEquals(
231+
'function() { return '.json_encode($view->render()).'; }',
232+
$builder->getAttributes()['layout']['top']
233+
);
234+
235+
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
236+
$this->assertEquals(
237+
'function() { return '.json_encode('<p>Test Inline View</p>').'; }',
238+
$builder->getAttributes()['layout']['bottom']
239+
);
219240

220241
$this->assertArrayHasKey('top1Start', $builder->getAttributes()['layout']);
221242
$this->assertEquals(

0 commit comments

Comments
 (0)