Skip to content

Commit

Permalink
Add style and css class for TD (#2729)
Browse files Browse the repository at this point in the history
* Add tests for class and style in TD

* add style and class for td.blade

* Add style and class for TD
  • Loading branch information
grigoriy-ivanov authored Nov 8, 2023
1 parent dc56e5c commit 8c0b15b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
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

0 comments on commit 8c0b15b

Please sign in to comment.