Power-Components / livewire-powergrid

⚡ PowerGrid generates modern, powerful and easy-to-customize data tables using Laravel Livewire.
https://livewire-powergrid.com
MIT License
1.41k stars 206 forks source link

[ENHANCEMENT/FIX] Fix `hideToggleable, add Action Rules `editOnClick()`, `toggleable()`, `unless()` condition and others... #1559

Closed dansysanalyst closed 1 month ago

dansysanalyst commented 1 month ago

⚡ PowerGrid - Pull Request

Welcome and thank you for your interest in contributing to our project!. You must use this template to submit a Pull Request or it will not be accepted.


Motivation

Description

This Pull Request fixes the bug of hideToggleable not being applied and adds new Action Rules as documented below.

As a minor detail, I have also linked the README screenshot to the "Demo Dish" table.

New Rules

Screenshot

CleanShot 2024-05-20 at 00 36 28@2x

Example using alternating() to produce a zebra-striped table, Rule::editOnClick('name')->enable() forces inline editing on dish ID # 10, Rule::rows()->hideToggleable() hiding the toggleable on dishes ID # 5,6,10

📜 New Rules Action Rules Summary

🚀 ACTION RULES
ROW RULE
Method Description
setAttribute(string $attribute = null, string $value = null)
Sets a value to the Rule target attribute.
detailView(string $detailView = null, array $options = []) Sets a view for the Detail Row
showToggleable() / hideToggleable()
Show / Hide toggleable switches in the row
enableEditOnClick() /disableEditOnClick() Show / Hide edit on click in the row
showToggleDetail() / hideToggleDetail() Show / Hide detail row toggleable in the row
loop() Interact with the row loop
firstOnPage() / lastOnPage() / alternating() Alias to the loop modifier. Each alias will override the loop modifier, so combining them is not possible.
CHECKBOX RULE
Method Description
setAttribute(string $attribute = null, string $value = null) Sets a value to the Rule target attribute.
hide() Hide the checkboxes
disable() Disable the checkboxes
applyRowClasses(string $attrClass = '') Apply Classes in a row
RADIO BUTTON RULE
Method Description
setAttribute(string $attribute = null, string $value = null) Sets a value to the Rule target attribute.
hide() Hide the radio buttons
disable() Disable the radio buttons
applyRowClasses(string $attrClass = '') Apply Classes in a row
BUTTON RULES
Method Description
dispatch(string $event = '', array $params = []) Sets the event to be emitted by the Rule target
dispatchTo(string $to = '', string $event = '', array $params = []) Sets the target component and the event name to be emitted by the Rule target
setAttribute(string $attribute = null, string|array $value = null) Sets a value to the Rule target attribute. Buttons accept multiple usages of this modifier.
show() / hide() Show or hide a button
enable() / disable() Enable or disable a button
redirect(Closure $closure = null, string $target = '_blank') Sets Rule target's redirect URL
slot(string $slot) Sets the Rule target slot content
bladeComponent(string $component, array $params) Changes the Blade Component in the Rule target.
EDIT ON CLICK RULE
enable() / disable() Forcefully enable or disable a edit on click, regardless of column configuration or its permission.
TOGGLEABLE RULE
show() / hide() Forcefully show or hide a toggleable switch, regardless of column configuration or its permission.
Usage/Examples

Specific target rules have precedence over column and row rules. That is, if a column is configured to NOT have permission to display a toggleable switch, but a rule is targeting that column with the show() modifier, this toggle will be rendered.

In the next example, the column doesn't have permission to show toggleables. However, the dish with ID 1 will forcefully display a toggle, while all other rows will have the fallback badge.


            Column::make('Active', 'active')
                ->toggleable(hasPermission: false),

            Rule::toggleable('active')
                ->when(fn ($dish) => $dish->id == 1)
                ->show(),

In a similar case, now the column active has permission to display toggles. In this column, we will hide toggles when active is false. So, when a user switches it to false, he cannot switch to true again. However, for the dish #1, the checkbox will always be shown.

            Column::make('Active', 'active')
                ->toggleable(hasPermission: true),

            Rule::rows()
                ->when(fn ($dish) => $dish->active == false)
                ->hideToggleable()

            Rule::toggleable('active')
                ->when(fn ($dish) => $dish->id == 1)
                ->show(),

The alternating() modifier is a convenient way to produce a zebra-stripped table.

           Rule::rows()
                ->alternating()
                ->setAttribute('class', '!bg-indigo-100'),

For more details, see the tests associated with the tests/Concerns/Components/ComponentsForTestRule.php component.

POSSIBLE BREAKING CHANGES

The following views were altered and will cause breaking changes in users who published/customized the views.

Related Issue(s): https://github.com/Power-Components/livewire-powergrid/issues/1539

Documentation

This PR requires Documentation update?

luanfreitasdev commented 1 month ago

Thanks @dansysanalyst ! I believe this refactoring was very well done.