Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add style and css class for TD #2729

Merged
merged 3 commits into from
Nov 8, 2023
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
7 changes: 5 additions & 2 deletions resources/views/partials/layouts/td.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<td class="text-{{$align}} @if(!$width) text-truncate @endif"
<td class="text-{{$align}} @if(!$width) text-truncate @endif {{ $class }}"
data-column="{{ $slug }}" colspan="{{ $colspan }}"
@empty(!$width)style="min-width:{{ is_numeric($width) ? $width . 'px' : $width }};"@endempty
@style([
"min-width:$width;" => $width,
"$style" => $style,
])
>
<div>
@isset($render)
Expand Down
34 changes: 32 additions & 2 deletions src/Screen/TD.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class TD extends Cell
* @var string|null|int
*/
protected $width;
/**
* @var string|null
*/
protected $style;
/**
* @var string|null
*/
protected $class;
/**
* @var string
*/
Expand Down Expand Up @@ -93,6 +101,26 @@ public function width($width): self
return $this;
}

/**
* @param string $style
*/
public function style($style): self
{
$this->style = $style;

return $this;
}

/**
* @param string $class
*/
public function class($class): self
{
$this->class = $class;

return $this;
}

/**
* @param string|\Orchid\Screen\Field $filter
*/
Expand Down Expand Up @@ -194,7 +222,7 @@ public function colspan(int $colspan): self
public function buildTh()
{
return view('platform::partials.layouts.th', [
'width' => $this->width,
'width' => is_numeric($this->width) ? $this->width.'px' : $this->width,
'align' => $this->align,
'sort' => $this->sort,
'sortUrl' => $this->buildSortUrl(),
Expand Down Expand Up @@ -262,7 +290,9 @@ public function buildTd($repository, object $loop = null)
'value' => $value,
'render' => $this->render,
'slug' => $this->sluggable(),
'width' => $this->width,
'width' => is_numeric($this->width) ? $this->width.'px' : $this->width,
'style' => $this->style,
'class' => $this->class,
'colspan' => $this->colspan,
]);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Screen/TDForTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ public function testTdWidth(): void
$this->assertStringContainsString('style="min-width:'.$width.'', $view);
}

public function testTdStyle(): void
{
$style = 'border-color: red;';

$view = TD::make('name')->style($style)->buildTd(new Repository(['name' => 'value']));

$this->assertStringContainsString('style="'.$style.'', $view);
}

public function testTdClass(): void
{
$class = 'my-custom-class';

$view = TD::make('name')->class($class)->buildTd(new Repository(['name' => 'value']));

$this->assertStringContainsString('text-start text-truncate '.$class.'', $view);
}

public function testTdWidthWithCustomStyle(): void
{
$width = '100px';
$style = 'border-color: red;';

$view = TD::make('name')->width($width)->style($style)->buildTd(new Repository(['name' => 'value']));

$this->assertStringContainsString('style="min-width:'.$width.'; '.$style.'', $view);
}

public function testTdWithoutWidth(): void
{
$view = TD::make('name')->buildTd(new Repository(['name' => 'value']));
Expand Down