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 functionality tr class in table #149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<tbody>
@foreach ($items as $item)
<tr class="border-b border-gray-200 text-sm" wire:key="{{ $item->getKey() }}">
<tr class="border-b border-gray-200 text-sm {{$view->trClass($item)}}" wire:key="{{ $item->getKey() }}">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this approach, I think we are forcing all the components to have this method defined, in that case, there should be a validation to execute this only if it's defined, or maybe adding a default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Gustavinho this functionality is to be able to give a css class to a tr, for example

public funtion trClass($model)
{
   if($model->status == 'VA')
   {
     return 'bg-green';
    }
  
    return '';
}

Copy link
Contributor Author

@joserojasrodriguez joserojasrodriguez Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tables are only in the table component so I think the other components would not be affected.

Although that method returns an empty string anyway

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @joserojasrodriguez , I see what you're trying to do, it's cool, by "components" I meant "views" or Livewire components, I think if we don't define that trClass() method in our table view files, it will throw an error, even if we don't need to customize the tr class, this method will have to be there, I saw you added it in the pub file but what happens with the current projects and files already created?, we will have to add that method to all of our files, even returning an empty string, you had to do it with the mocked table views for testing, this is for retro compatibility with views already created, what I'm trying to say is that this method shouldn't be mandatory.

@if ($this->hasBulkActions)
<td class="pl-3">
<span class="flex items-center justify-center">
Expand Down
10 changes: 10 additions & 0 deletions stubs/table-view.stub
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ class DummyClass extends TableView
{
return [];
}

/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to remove this from the stub file and add it to the documentation

* Set de class for tr
*
* @param $model Current model for each row
*/
public function trClass($model): string
{
return '';
}
}
8 changes: 8 additions & 0 deletions tests/Feature/TableViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function testSeeAllDataOnTheTable()
->assertSeeUsers($users);
}

public function testSeeTrClassOnTable()
{
$users = factory(UserTest::class, 7)->create();

Livewire::test(MockTableView::class)
->assertSeeHtml('<tr class="border-b border-gray-200 text-sm class-tr" wire:key="1">');
}

public function testSeeAllDataSettingAModelClass()
{
$users = factory(UserTest::class, 7)->create();
Expand Down
5 changes: 5 additions & 0 deletions tests/Mock/MockReviewTableViewWithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public function row(ReviewTest $review)
$review->user->email,
];
}

public function trClass(ReviewTest $review)
{
return '';
}
}
5 changes: 5 additions & 0 deletions tests/Mock/MockTableView.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function row(UserTest $user)
$user->email
];
}

public function trClass(UserTest $user)
{
return 'class-tr';
}
}
5 changes: 5 additions & 0 deletions tests/Mock/MockTableViewWithModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function row(UserTest $user)
$user->email
];
}

public function trClass(UserTest $user)
{
return '';
}
}