diff --git a/resources/views/components/table/collapsed-columns.blade.php b/resources/views/components/table/collapsed-columns.blade.php index 1db467ce1..e3ccbbd0a 100644 --- a/resources/views/components/table/collapsed-columns.blade.php +++ b/resources/views/components/table/collapsed-columns.blade.php @@ -1,39 +1,16 @@ @aware([ 'tableName', 'primaryKey','isTailwind','isBootstrap']) @props(['row', 'rowIndex']) -@php - $customAttributes = $this->getTrAttributes($row, $rowIndex); -@endphp - -@if ($this->collapsingColumnsAreEnabled() && $this->hasCollapsedColumns()) - @php - $colspan = $this->getColspanCount(); - $columns = collect(); - - if($this->shouldCollapseAlways()) - { - $columns->push($this->getCollapsedAlwaysColumns()); - } - if ($this->shouldCollapseOnMobile() && $this->shouldCollapseOnTablet()) { - $columns->push($this->getCollapsedMobileColumns()); - $columns->push($this->getCollapsedTabletColumns()); - } elseif ($this->shouldCollapseOnTablet() && ! $this->shouldCollapseOnMobile()) { - $columns->push($this->getCollapsedTabletColumns()); - } elseif ($this->shouldCollapseOnMobile() && ! $this->shouldCollapseOnTablet()) { - $columns->push($this->getCollapsedMobileColumns()); - } - - $columns = $columns->collapse(); - @endphp - - collapsingColumnsAreEnabled && $this->hasCollapsedColumns) + @php($customAttributes = $this->getTrAttributes($row, $rowIndex)) + merge($customAttributes) + $attributes->merge([ + 'wire:loading.class.delay' => 'opacity-50 dark:bg-gray-900 dark:opacity-60', + 'wire:key' => $tableName.'-row-'.$row->{$primaryKey}.'-collapsed-contents', + ]) + ->merge($customAttributes) ->class([ 'hidden bg-white dark:bg-gray-700 dark:text-white rappasoft-striped-row' => ($isTailwind && ($customAttributes['default'] ?? true) && $rowIndex % 2 === 0), 'hidden bg-gray-50 dark:bg-gray-800 dark:text-white rappasoft-striped-row' => ($isTailwind && ($customAttributes['default'] ?? true) && $rowIndex % 2 !== 0), @@ -42,36 +19,31 @@ ]) ->except(['default','default-styling','default-colors']) }} - > - $isTailwind, 'text-start pt-3 p-2' => $isBootstrap, - ]) - colspan="{{ $colspan }}" - > + ])>
- @foreach($columns as $colIndex => $column) - @continue($column->isHidden()) - @continue($this->columnSelectIsEnabled() && ! $this->columnSelectIsEnabledForColumn($column)) - -

$isTailwind && $column->shouldCollapseAlways(), - 'block mb-2 sm:hidden' => $isTailwind && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && !$column->shouldCollapseOnMobile(), - 'block mb-2 md:hidden' => $isTailwind && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && $column->shouldCollapseOnMobile(), - 'block mb-2 lg:hidden' => $isTailwind && !$column->shouldCollapseAlways() && ($column->shouldCollapseOnTablet() || $column->shouldCollapseOnMobile()), - - 'd-block mb-2' => $isBootstrap && $column->shouldCollapseAlways(), - 'd-block mb-2 d-sm-none' => $isBootstrap && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && !$column->shouldCollapseOnMobile(), - 'd-block mb-2 d-md-none' => $isBootstrap && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && $column->shouldCollapseOnMobile(), - 'd-block mb-2 d-lg-none' => $isBootstrap && !$column->shouldCollapseAlways() && ($column->shouldCollapseOnTablet() || $column->shouldCollapseOnMobile()), - - ]) - > - {{ $column->getTitle() }}: {{ $column->renderContents($row) }} + @foreach($this->getCollapsedColumnsForContent as $colIndex => $column) + +

$isTailwind, + 'sm:block' => $isTailwind && $column->shouldCollapseAlways(), + 'sm:block md:hidden' => $isTailwind && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && $column->shouldCollapseOnMobile(), + 'sm:block lg:hidden' => $isTailwind && !$column->shouldCollapseAlways() && ($column->shouldCollapseOnTablet() || $column->shouldCollapseOnMobile()), + + 'd-block mb-2' => $isBootstrap, + 'd-sm-none' => $isBootstrap && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && !$column->shouldCollapseOnMobile(), + 'd-md-none' => $isBootstrap && !$column->shouldCollapseAlways() && !$column->shouldCollapseOnTablet() && $column->shouldCollapseOnMobile(), + 'd-lg-none' => $isBootstrap && !$column->shouldCollapseAlways() && ($column->shouldCollapseOnTablet() || $column->shouldCollapseOnMobile()), + ])> + {{ $column->getTitle() }}: + @if($column->isHtml()) + {!! $column->setIndexes($rowIndex, $colIndex)->renderContents($row) !!} + @else + {{ $column->setIndexes($rowIndex, $colIndex)->renderContents($row) }} + @endif

@endforeach
diff --git a/resources/views/components/table/td/collapsed-columns.blade.php b/resources/views/components/table/td/collapsed-columns.blade.php index 63294f432..078c1e39b 100644 --- a/resources/views/components/table/td/collapsed-columns.blade.php +++ b/resources/views/components/table/td/collapsed-columns.blade.php @@ -1,87 +1,52 @@ @aware([ 'tableName','isTailwind','isBootstrap']) @props(['rowIndex', 'hidden' => false]) -@if ($this->collapsingColumnsAreEnabled() && $this->hasCollapsedColumns()) - @if ($isTailwind) - merge() - ->class([ - 'p-3 table-cell text-center', - 'sm:hidden' => !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet(), - 'md:hidden' => !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet() && $this->shouldCollapseOnMobile(), - 'lg:hidden' => !$this->shouldCollapseAlways() && ($this->shouldCollapseOnTablet() || $this->shouldCollapseOnMobile()), - ]) - }} - :class="currentlyReorderingStatus ? 'laravel-livewire-tables-reorderingMinimised' : ''" - > - @if (! $hidden) - - @endif - - @elseif ($isBootstrap) - class([ - 'd-sm-none' => !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet(), - 'd-md-none' => !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet() && $this->shouldCollapseOnMobile(), - 'd-lg-none' => !$this->shouldCollapseAlways() && ($this->shouldCollapseOnTablet() || $this->shouldCollapseOnMobile()), - ]) - }} - :class="currentlyReorderingStatus ? 'laravel-livewire-tables-reorderingMinimised' : ''" - > - @if (! $hidden) - - @endif - - @endif +@if ($this->collapsingColumnsAreEnabled && $this->hasCollapsedColumns) + merge() + ->class([ + 'p-3 table-cell text-center' => $isTailwind, + 'sm:hidden' => $isTailwind && !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet(), + 'md:hidden' => $isTailwind && !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet() && $this->shouldCollapseOnMobile(), + 'lg:hidden' => $isTailwind && !$this->shouldCollapseAlways() && ($this->shouldCollapseOnTablet() || $this->shouldCollapseOnMobile()), + 'd-sm-none' => $isBootstrap && !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet(), + 'd-md-none' => $isBootstrap && !$this->shouldCollapseAlways() && !$this->shouldCollapseOnTablet() && $this->shouldCollapseOnMobile(), + 'd-lg-none' => $isBootstrap && !$this->shouldCollapseAlways() && ($this->shouldCollapseOnTablet() || $this->shouldCollapseOnMobile()), + ]) + }} + :class="currentlyReorderingStatus ? 'laravel-livewire-tables-reorderingMinimised' : ''" + > + @if (! $hidden) + + @endif + @endif diff --git a/src/LaravelLivewireTablesServiceProvider.php b/src/LaravelLivewireTablesServiceProvider.php index 18babd7a1..2fa063768 100644 --- a/src/LaravelLivewireTablesServiceProvider.php +++ b/src/LaravelLivewireTablesServiceProvider.php @@ -3,6 +3,7 @@ namespace Rappasoft\LaravelLivewireTables; use Illuminate\Foundation\Console\AboutCommand; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; use Livewire\ComponentHookRegistry; use Rappasoft\LaravelLivewireTables\Commands\MakeCommand; @@ -39,6 +40,7 @@ public function boot(): void } else { $this->loadTranslationsFrom(__DIR__.'/../resources/lang/php', 'livewire-tables'); } + $this->addBladeLoopDirective(); $this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables'); @@ -81,6 +83,18 @@ public function consoleCommands(): void } } + public function addBladeLoopDirective(): void + { + Blade::directive('tableloop', function ($expression) { + return ""; + }); + + Blade::directive('endtableloop', function ($expression) { + return ''; + }); + + } + public function register(): void { $this->mergeConfigFrom( diff --git a/src/Traits/Configuration/CollapsingColumnConfiguration.php b/src/Traits/Configuration/CollapsingColumnConfiguration.php index 88e883e7d..dada8b991 100644 --- a/src/Traits/Configuration/CollapsingColumnConfiguration.php +++ b/src/Traits/Configuration/CollapsingColumnConfiguration.php @@ -24,4 +24,11 @@ public function setCollapsingColumnsDisabled(): self return $this; } + + public function unsetCollapsedStatuses(): void + { + unset($this->shouldAlwaysCollapse); + unset($this->shouldMobileCollapse); + unset($this->shouldTabletCollapse); + } } diff --git a/src/Traits/Configuration/ColumnConfiguration.php b/src/Traits/Configuration/ColumnConfiguration.php index 57de0b9f4..141ca918a 100644 --- a/src/Traits/Configuration/ColumnConfiguration.php +++ b/src/Traits/Configuration/ColumnConfiguration.php @@ -15,11 +15,4 @@ public function setAppendedColumns(array $appendedColumns): void $this->appendedColumns = collect($appendedColumns); $this->hasRunColumnSetup = false; } - - public function unsetCollapsedStatuses(): void - { - unset($this->shouldAlwaysCollapse); - unset($this->shouldMobileCollapse); - unset($this->shouldTabletCollapse); - } } diff --git a/src/Traits/Helpers/CollapsingColumnHelpers.php b/src/Traits/Helpers/CollapsingColumnHelpers.php index e80e9da64..a7eb43dc1 100644 --- a/src/Traits/Helpers/CollapsingColumnHelpers.php +++ b/src/Traits/Helpers/CollapsingColumnHelpers.php @@ -2,7 +2,9 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Helpers; +use Illuminate\Support\Collection; use Livewire\Attributes\Computed; +use Rappasoft\LaravelLivewireTables\Views\Column; trait CollapsingColumnHelpers { @@ -23,6 +25,7 @@ public function collapsingColumnsAreEnabled(): bool return $this->getCollapsingColumnsStatus() === true; } + #[Computed] public function collapsingColumnsAreDisabled(): bool { return $this->getCollapsingColumnsStatus() === false; @@ -33,4 +36,123 @@ public function showCollapsingColumnSections(): bool { return $this->collapsingColumnsAreEnabled() && $this->hasCollapsedColumns(); } + + #[Computed] + public function hasCollapsedColumns(): bool + { + return $this->hasCollapsingColumns() && ($this->shouldCollapseOnMobile() || $this->shouldCollapseOnTablet() || $this->shouldCollapseAlways()); + } + + #[Computed] + public function shouldCollapseOnMobile(): bool + { + + if (! isset($this->shouldMobileCollapse)) { + $this->shouldMobileCollapse = ($this->getCollapsedMobileColumnsCount() > 0); + } + + return $this->shouldMobileCollapse; + + } + + public function getCollapsedMobileColumns(): Collection + { + return $this->getColumns() + ->reject(fn (Column $column) => ($column->isHidden() || ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column)))) + ->filter(fn (Column $column) => $column->shouldCollapseOnMobile()) + ->values(); + } + + public function getCollapsedMobileColumnsCount(): int + { + return $this->getCollapsedMobileColumns()->count(); + } + + public function getVisibleMobileColumns(): Collection + { + return $this->getColumns() + ->reject(fn (Column $column) => $column->shouldCollapseOnMobile()) + ->values(); + } + + public function getVisibleMobileColumnsCount(): int + { + return $this->getVisibleMobileColumns()->count(); + } + + #[Computed] + public function shouldCollapseOnTablet(): bool + { + if (! isset($this->shouldTabletCollapse)) { + $this->shouldTabletCollapse = ($this->getCollapsedTabletColumnsCount() > 0); + } + + return $this->shouldTabletCollapse; + + } + + public function getCollapsedTabletColumns(): Collection + { + return $this->getColumns() + ->reject(fn (Column $column) => ($column->isHidden() || ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column)))) + ->filter(fn (Column $column) => $column->shouldCollapseOnTablet()) + ->values(); + } + + public function getCollapsedTabletColumnsCount(): int + { + return $this->getCollapsedTabletColumns()->count(); + } + + public function getVisibleTabletColumns(): Collection + { + return $this->getColumns() + ->reject(fn (Column $column) => $column->shouldCollapseOnTablet()) + ->values(); + } + + public function getVisibleTabletColumnsCount(): int + { + return $this->getVisibleTabletColumns()->count(); + } + + public function getCollapsedAlwaysColumns(): Collection + { + return $this->getColumns() + ->reject(fn (Column $column) => ($column->isHidden() || ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column)))) + ->filter(fn (Column $column) => $column->shouldCollapseAlways()) + ->values(); + } + + public function getCollapsedAlwaysColumnsCount(): int + { + return $this->getCollapsedAlwaysColumns()->count(); + } + + #[Computed] + public function shouldCollapseAlways(): bool + { + if (! isset($this->shouldAlwaysCollapse)) { + $this->shouldAlwaysCollapse = ($this->getCollapsedAlwaysColumnsCount() > 0); + } + + return $this->shouldAlwaysCollapse; + } + + #[Computed] + public function getColspanCount(): int + { + return 100; + } + + #[Computed] + public function getCollapsedColumnsForContent(): Collection + { + $colspan = $this->getColspanCount(); + $columns = $this->getColumns() + ->reject(fn (Column $column) => ($column->isHidden() || ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column)))) + ->reject(fn (Column $column) => $column->shouldNeverCollapse()); + + return $columns; + } } diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 61f011a7e..beb4e2439 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -125,96 +125,6 @@ public function getColumnCount(): int return $this->getColumns()->count(); } - #[Computed] - public function hasCollapsedColumns(): bool - { - if ($this->shouldCollapseOnMobile() || $this->shouldCollapseOnTablet() || $this->shouldCollapseAlways()) { - return true; - } - - return false; - } - - #[Computed] - public function shouldCollapseOnMobile(): bool - { - - if (! isset($this->shouldMobileCollapse)) { - $this->shouldMobileCollapse = ($this->getCollapsedMobileColumnsCount() > 0); - } - - return $this->shouldMobileCollapse; - - } - - public function getCollapsedMobileColumns(): Collection - { - return $this->getColumns() - ->reject(fn (Column $column) => $column->isHidden()) - ->reject(fn (Column $column) => ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column))) - ->filter(fn (Column $column) => $column->shouldCollapseOnMobile()) - ->values(); - } - - public function getCollapsedMobileColumnsCount(): int - { - return $this->getCollapsedMobileColumns()->count(); - } - - public function getVisibleMobileColumns(): Collection - { - return $this->getColumns() - ->reject(fn (Column $column) => $column->shouldCollapseOnMobile()) - ->values(); - } - - public function getVisibleMobileColumnsCount(): int - { - return $this->getVisibleMobileColumns()->count(); - } - - #[Computed] - public function shouldCollapseOnTablet(): bool - { - if (! isset($this->shouldTabletCollapse)) { - $this->shouldTabletCollapse = ($this->getCollapsedTabletColumnsCount() > 0); - } - - return $this->shouldTabletCollapse; - - } - - public function getCollapsedTabletColumns(): Collection - { - return $this->getColumns() - ->reject(fn (Column $column) => $column->isHidden()) - ->reject(fn (Column $column) => ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column))) - ->filter(fn (Column $column) => $column->shouldCollapseOnTablet()) - ->values(); - } - - public function getCollapsedTabletColumnsCount(): int - { - return $this->getCollapsedTabletColumns()->count(); - } - - public function getVisibleTabletColumns(): Collection - { - return $this->getColumns() - ->reject(fn (Column $column) => $column->shouldCollapseOnTablet()) - ->values(); - } - - public function getVisibleTabletColumnsCount(): int - { - return $this->getVisibleTabletColumns()->count(); - } - - public function getColspanCount(): int - { - return 100; - } - public function getPrependedColumns(): Collection { return $this->prependedColumns ?? collect($this->prependColumns()); @@ -225,30 +135,6 @@ public function getAppendedColumns(): Collection return $this->appendedColumns ?? collect($this->appendColumns()); } - public function getCollapsedAlwaysColumns(): Collection - { - return $this->getColumns() - ->reject(fn (Column $column) => $column->isHidden()) - ->reject(fn (Column $column) => ($column->isSelectable() && ! $this->columnSelectIsEnabledForColumn($column))) - ->filter(fn (Column $column) => $column->shouldCollapseAlways()) - ->values(); - } - - public function getCollapsedAlwaysColumnsCount(): int - { - return $this->getCollapsedAlwaysColumns()->count(); - } - - #[Computed] - public function shouldCollapseAlways(): bool - { - if (! isset($this->shouldAlwaysCollapse)) { - $this->shouldAlwaysCollapse = ($this->getCollapsedAlwaysColumnsCount() > 0); - } - - return $this->shouldAlwaysCollapse; - } - /** * Prepend columns. */ diff --git a/src/Traits/Styling/Columns/HasCollapsingColumnsStyling.php b/src/Traits/Styling/Columns/HasCollapsingColumnsStyling.php new file mode 100644 index 000000000..d8b9b0ef7 --- /dev/null +++ b/src/Traits/Styling/Columns/HasCollapsingColumnsStyling.php @@ -0,0 +1,58 @@ + true, 'default-colors' => true]; + + protected array $collapsingColumnButtonExpandAttributes = ['default-styling' => true, 'default-colors' => true]; + + /** + * Used to set attributes for the Collapsed Column Collapse Button + * + * @param array $collapsingColumnButtonCollapseAttributes + */ + public function setCollapsingColumnButtonCollapseAttributes(array $collapsingColumnButtonCollapseAttributes): self + { + $this->collapsingColumnButtonCollapseAttributes = [...['default-colors' => false, 'default-styling' => false], ...$collapsingColumnButtonCollapseAttributes]; + + return $this; + } + + /** + * Used to set attributes for the Collapsed Column Expand Button + * + * @param array $collapsingColumnButtonExpandAttributes + */ + public function setCollapsingColumnButtonExpandAttributes(array $collapsingColumnButtonExpandAttributes): self + { + $this->collapsingColumnButtonExpandAttributes = [...['default-colors' => false, 'default-styling' => false], ...$collapsingColumnButtonExpandAttributes]; + + return $this; + } + + /** + * Retrieves attributes for the Collapsed Column Collapse Button + * + * @return array + */ + #[Computed] + public function getCollapsingColumnButtonCollapseAttributes(): array + { + return [...['default-styling' => true, 'default-colors' => true], ...$this->collapsingColumnButtonCollapseAttributes]; + } + + /** + * Retrieves attributes for the Collapsed Column Expand Button + * + * @return array + */ + #[Computed] + public function getCollapsingColumnButtonExpandAttributes(): array + { + return [...['default-styling' => true, 'default-colors' => true], ...$this->collapsingColumnButtonExpandAttributes]; + } +} diff --git a/src/Traits/Styling/Configuration/CollapsingColumnsStylingConfiguration.php b/src/Traits/Styling/Configuration/CollapsingColumnsStylingConfiguration.php deleted file mode 100644 index 9ab487907..000000000 --- a/src/Traits/Styling/Configuration/CollapsingColumnsStylingConfiguration.php +++ /dev/null @@ -1,30 +0,0 @@ - $collapsingColumnButtonCollapseAttributes - */ - public function setCollapsingColumnButtonCollapseAttributes(array $collapsingColumnButtonCollapseAttributes): self - { - $this->collapsingColumnButtonCollapseAttributes = [...['default-colors' => false, 'default-styling' => false], ...$collapsingColumnButtonCollapseAttributes]; - - return $this; - } - - /** - * Used to set attributes for the Collapsed Column Expand Button - * - * @param array $collapsingColumnButtonExpandAttributes - */ - public function setCollapsingColumnButtonExpandAttributes(array $collapsingColumnButtonExpandAttributes): self - { - $this->collapsingColumnButtonExpandAttributes = [...['default-colors' => false, 'default-styling' => false], ...$collapsingColumnButtonExpandAttributes]; - - return $this; - } -} diff --git a/src/Traits/Styling/HasCollapsingColumnsStyling.php b/src/Traits/Styling/HasCollapsingColumnsStyling.php deleted file mode 100644 index 080130f78..000000000 --- a/src/Traits/Styling/HasCollapsingColumnsStyling.php +++ /dev/null @@ -1,16 +0,0 @@ - true, 'default-colors' => true]; - - protected array $collapsingColumnButtonExpandAttributes = ['default-styling' => true, 'default-colors' => true]; -} diff --git a/src/Traits/Styling/Helpers/CollapsingColumnsStylingHelpers.php b/src/Traits/Styling/Helpers/CollapsingColumnsStylingHelpers.php deleted file mode 100644 index 416c79444..000000000 --- a/src/Traits/Styling/Helpers/CollapsingColumnsStylingHelpers.php +++ /dev/null @@ -1,30 +0,0 @@ - - */ - #[Computed] - public function getCollapsingColumnButtonCollapseAttributes(): array - { - return [...['default-styling' => true, 'default-colors' => true], ...$this->collapsingColumnButtonCollapseAttributes]; - } - - /** - * Retrieves attributes for the Collapsed Column Expand Button - * - * @return array - */ - #[Computed] - public function getCollapsingColumnButtonExpandAttributes(): array - { - return [...['default-styling' => true, 'default-colors' => true], ...$this->collapsingColumnButtonExpandAttributes]; - } -} diff --git a/src/Traits/WithCollapsingColumns.php b/src/Traits/WithCollapsingColumns.php index 37fef32d1..733f707a4 100644 --- a/src/Traits/WithCollapsingColumns.php +++ b/src/Traits/WithCollapsingColumns.php @@ -4,7 +4,7 @@ use Rappasoft\LaravelLivewireTables\Traits\Configuration\CollapsingColumnConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\CollapsingColumnHelpers; -use Rappasoft\LaravelLivewireTables\Traits\Styling\HasCollapsingColumnsStyling; +use Rappasoft\LaravelLivewireTables\Traits\Styling\Columns\HasCollapsingColumnsStyling; trait WithCollapsingColumns { @@ -13,4 +13,10 @@ trait WithCollapsingColumns HasCollapsingColumnsStyling; protected bool $collapsingColumnsStatus = true; + + protected ?bool $shouldAlwaysCollapse; + + protected ?bool $shouldMobileCollapse; + + protected ?bool $shouldTabletCollapse; } diff --git a/src/Traits/WithColumns.php b/src/Traits/WithColumns.php index 1e565ffe7..118e2a302 100644 --- a/src/Traits/WithColumns.php +++ b/src/Traits/WithColumns.php @@ -18,12 +18,6 @@ trait WithColumns protected ?Collection $appendedColumns; - protected ?bool $shouldAlwaysCollapse; - - protected ?bool $shouldMobileCollapse; - - protected ?bool $shouldTabletCollapse; - protected bool $hasRunColumnSetup = false; /** diff --git a/src/Views/Traits/Columns/IsCollapsible.php b/src/Views/Traits/Columns/IsCollapsible.php index 2dc3439f2..6aebcf522 100644 --- a/src/Views/Traits/Columns/IsCollapsible.php +++ b/src/Views/Traits/Columns/IsCollapsible.php @@ -10,9 +10,12 @@ trait IsCollapsible protected bool $collapseAlways = false; + protected bool $collapseSometimes = false; + public function collapseOnMobile(): self { $this->collapseOnMobile = true; + $this->collapseSometimes = true; return $this; } @@ -28,6 +31,7 @@ public function shouldCollapseOnMobile(): bool public function collapseOnTablet(): self { $this->collapseOnTablet = true; + $this->collapseSometimes = true; return $this; } @@ -40,6 +44,7 @@ public function shouldCollapseOnTablet(): bool public function collapseAlways(): self { $this->collapseAlways = true; + $this->collapseSometimes = true; return $this; } @@ -48,4 +53,19 @@ public function shouldCollapseAlways(): bool { return $this->collapseAlways; } + + public function shouldCollapseSometimes(): bool + { + return $this->collapseSometimes; + } + + public function shouldNeverCollapse(): bool + { + return $this->collapseSometimes === false; + } + + public function shouldCollapseNever(): bool + { + return ($this->shouldCollapseOnMobile() === false) && ($this->shouldCollapseOnTablet() === false) && ($this->shouldCollapseAlways() === false); + } } diff --git a/tests/Unit/Traits/Helpers/CollapsingColumnsHelpersTest.php b/tests/Unit/Traits/Helpers/CollapsingColumnsHelpersTest.php new file mode 100644 index 000000000..7412e58f4 --- /dev/null +++ b/tests/Unit/Traits/Helpers/CollapsingColumnsHelpersTest.php @@ -0,0 +1,217 @@ +assertFalse($this->basicTable->hasCollapsedColumns()); + + $this->assertFalse($this->basicTable->getColumnBySelectName('id')->shouldCollapseOnMobile()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertTrue($this->basicTable->getColumnBySelectName('id')->shouldCollapseOnMobile()); + + $this->assertTrue($this->basicTable->hasCollapsedColumns()); + } + + public function test_can_tell_if_columns_should_collapse_on_mobile(): void + { + $this->assertFalse($this->basicTable->shouldCollapseOnMobile()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertTrue($this->basicTable->shouldCollapseOnMobile()); + } + + public function test_can_get_collapsed_mobile_columns(): void + { + $this->assertCount(0, $this->basicTable->getCollapsedMobileColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(2, $this->basicTable->getCollapsedMobileColumns()); + $this->assertSame('ID', $this->basicTable->getCollapsedMobileColumns()[0]->getTitle()); + $this->assertSame('Name', $this->basicTable->getCollapsedMobileColumns()[1]->getTitle()); + } + + public function test_can_get_collapsed_mobile_columns_count(): void + { + $this->assertSame(0, $this->basicTable->getCollapsedMobileColumnsCount()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertSame(2, $this->basicTable->getCollapsedMobileColumnsCount()); + } + + public function test_can_get_visible_mobile_columns(): void + { + $this->assertCount(9, $this->basicTable->getVisibleMobileColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(7, $this->basicTable->getVisibleMobileColumns()); + $this->assertSame('Sort', $this->basicTable->getVisibleMobileColumns()->values()[0]->getTitle()); + $this->assertSame('Age', $this->basicTable->getVisibleMobileColumns()->values()[1]->getTitle()); + $this->assertSame('Breed', $this->basicTable->getVisibleMobileColumns()->values()[2]->getTitle()); + $this->assertSame('Other', $this->basicTable->getVisibleMobileColumns()->values()[3]->getTitle()); + } + + public function test_can_get_visible_mobile_columns_count(): void + { + $this->assertSame(9, $this->basicTable->getVisibleMobileColumnsCount()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); + + $this->assertSame(7, $this->basicTable->getVisibleMobileColumnsCount()); + } + + public function test_can_tell_if_columns_should_collapse_on_tablet(): void + { + $this->assertFalse($this->basicTable->shouldCollapseOnTablet()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertTrue($this->basicTable->shouldCollapseOnTablet()); + } + + public function test_can_get_collapsed_tablet_columns(): void + { + $this->assertCount(0, $this->basicTable->getCollapsedTabletColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); + $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(2, $this->basicTable->getCollapsedTabletColumns()); + $this->assertSame('ID', $this->basicTable->getCollapsedTabletColumns()[0]->getTitle()); + $this->assertSame('Name', $this->basicTable->getCollapsedTabletColumns()[1]->getTitle()); + } + + public function test_can_get_collapsed_tablet_columns_count(): void + { + $this->assertSame(0, $this->basicTable->getCollapsedTabletColumnsCount()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); + $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertSame(2, $this->basicTable->getCollapsedTabletColumnsCount()); + } + + public function test_can_get_visible_tablet_columns(): void + { + $this->assertCount(9, $this->basicTable->getVisibleTabletColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); + $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(7, $this->basicTable->getVisibleTabletColumns()); + $this->assertSame('Sort', $this->basicTable->getVisibleTabletColumns()->values()[0]->getTitle()); + $this->assertSame('Age', $this->basicTable->getVisibleTabletColumns()->values()[1]->getTitle()); + $this->assertSame('Breed', $this->basicTable->getVisibleTabletColumns()->values()[2]->getTitle()); + $this->assertSame('Other', $this->basicTable->getVisibleTabletColumns()->values()[3]->getTitle()); + } + + public function test_can_get_visible_tablet_columns_count(): void + { + $this->assertSame(9, $this->basicTable->getVisibleTabletColumnsCount()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); + $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertSame(7, $this->basicTable->getVisibleTabletColumnsCount()); + } + + // / *** ** // + + public function test_can_tell_if_columns_should_collapse_always(): void + { + $this->assertFalse($this->basicTable->shouldCollapseAlways()); + + $this->basicTable->getColumnBySelectName('id')->collapseAlways(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertTrue($this->basicTable->shouldCollapseAlways()); + } + + public function test_can_get_always_collapsed_columns(): void + { + $this->assertCount(0, $this->basicTable->getCollapsedAlwaysColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseAlways(); + $this->basicTable->getColumnBySelectName('name')->collapseAlways(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(2, $this->basicTable->getCollapsedAlwaysColumns()); + $this->assertSame('ID', $this->basicTable->getCollapsedAlwaysColumns()[0]->getTitle()); + $this->assertSame('Name', $this->basicTable->getCollapsedAlwaysColumns()[1]->getTitle()); + } + + public function test_can_get_always_collapsed_columns_count(): void + { + $this->assertSame(0, $this->basicTable->getCollapsedAlwaysColumnsCount()); + + $this->basicTable->getColumnBySelectName('id')->collapseAlways(); + $this->basicTable->getColumnBySelectName('name')->collapseAlways(); + + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertSame(2, $this->basicTable->getCollapsedAlwaysColumnsCount()); + } + + public function test_can_get_collapsed_columns_for_content(): void + { + $this->assertCount(0, $this->basicTable->getCollapsedMobileColumns()); + $this->assertCount(0, $this->basicTable->getCollapsedTabletColumns()); + + $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); + $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); + $this->basicTable->getColumnBySelectName('age')->collapseAlways(); + $this->basicTable->unsetCollapsedStatuses(); + + $this->assertCount(1, $this->basicTable->getCollapsedMobileColumns()); + $this->assertCount(1, $this->basicTable->getCollapsedTabletColumns()); + $this->assertCount(1, $this->basicTable->getCollapsedAlwaysColumns()); + + $this->assertSame(3, $this->basicTable->getCollapsedColumnsForContent()->count()); + + $this->assertSame('ID', $this->basicTable->getCollapsedColumnsForContent()->first()->getTitle()); + $this->assertSame('Age', $this->basicTable->getCollapsedColumnsForContent()->last()->getTitle()); + $this->assertSame('Name', $this->basicTable->getCollapsedColumnsForContent()->slice(1)->first()->getTitle()); + + } +} diff --git a/tests/Unit/Traits/Helpers/ColumnHelpersTest.php b/tests/Unit/Traits/Helpers/ColumnHelpersTest.php index 0b786109f..f7f6eb55b 100644 --- a/tests/Unit/Traits/Helpers/ColumnHelpersTest.php +++ b/tests/Unit/Traits/Helpers/ColumnHelpersTest.php @@ -62,188 +62,6 @@ public function test_can_get_column_count(): void $this->assertSame(9, $this->basicTable->getColumnCount()); } - public function test_can_tell_if_there_are_collapsable_columns(): void - { - $this->assertFalse($this->basicTable->hasCollapsedColumns()); - - $this->assertFalse($this->basicTable->getColumnBySelectName('id')->shouldCollapseOnMobile()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertTrue($this->basicTable->getColumnBySelectName('id')->shouldCollapseOnMobile()); - - $this->assertTrue($this->basicTable->hasCollapsedColumns()); - } - - public function test_can_tell_if_columns_should_collapse_on_mobile(): void - { - $this->assertFalse($this->basicTable->shouldCollapseOnMobile()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertTrue($this->basicTable->shouldCollapseOnMobile()); - } - - public function test_can_get_collapsed_mobile_columns(): void - { - $this->assertCount(0, $this->basicTable->getCollapsedMobileColumns()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertCount(2, $this->basicTable->getCollapsedMobileColumns()); - $this->assertSame('ID', $this->basicTable->getCollapsedMobileColumns()[0]->getTitle()); - $this->assertSame('Name', $this->basicTable->getCollapsedMobileColumns()[1]->getTitle()); - } - - public function test_can_get_collapsed_mobile_columns_count(): void - { - $this->assertSame(0, $this->basicTable->getCollapsedMobileColumnsCount()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertSame(2, $this->basicTable->getCollapsedMobileColumnsCount()); - } - - public function test_can_get_visible_mobile_columns(): void - { - $this->assertCount(9, $this->basicTable->getVisibleMobileColumns()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertCount(7, $this->basicTable->getVisibleMobileColumns()); - $this->assertSame('Sort', $this->basicTable->getVisibleMobileColumns()->values()[0]->getTitle()); - $this->assertSame('Age', $this->basicTable->getVisibleMobileColumns()->values()[1]->getTitle()); - $this->assertSame('Breed', $this->basicTable->getVisibleMobileColumns()->values()[2]->getTitle()); - $this->assertSame('Other', $this->basicTable->getVisibleMobileColumns()->values()[3]->getTitle()); - } - - public function test_can_get_visible_mobile_columns_count(): void - { - $this->assertSame(9, $this->basicTable->getVisibleMobileColumnsCount()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnMobile(); - $this->basicTable->getColumnBySelectName('name')->collapseOnMobile(); - - $this->assertSame(7, $this->basicTable->getVisibleMobileColumnsCount()); - } - - public function test_can_tell_if_columns_should_collapse_on_tablet(): void - { - $this->assertFalse($this->basicTable->shouldCollapseOnTablet()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertTrue($this->basicTable->shouldCollapseOnTablet()); - } - - public function test_can_get_collapsed_tablet_columns(): void - { - $this->assertCount(0, $this->basicTable->getCollapsedTabletColumns()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); - $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertCount(2, $this->basicTable->getCollapsedTabletColumns()); - $this->assertSame('ID', $this->basicTable->getCollapsedTabletColumns()[0]->getTitle()); - $this->assertSame('Name', $this->basicTable->getCollapsedTabletColumns()[1]->getTitle()); - } - - public function test_can_get_collapsed_tablet_columns_count(): void - { - $this->assertSame(0, $this->basicTable->getCollapsedTabletColumnsCount()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); - $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertSame(2, $this->basicTable->getCollapsedTabletColumnsCount()); - } - - public function test_can_get_visible_tablet_columns(): void - { - $this->assertCount(9, $this->basicTable->getVisibleTabletColumns()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); - $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertCount(7, $this->basicTable->getVisibleTabletColumns()); - $this->assertSame('Sort', $this->basicTable->getVisibleTabletColumns()->values()[0]->getTitle()); - $this->assertSame('Age', $this->basicTable->getVisibleTabletColumns()->values()[1]->getTitle()); - $this->assertSame('Breed', $this->basicTable->getVisibleTabletColumns()->values()[2]->getTitle()); - $this->assertSame('Other', $this->basicTable->getVisibleTabletColumns()->values()[3]->getTitle()); - } - - public function test_can_get_visible_tablet_columns_count(): void - { - $this->assertSame(9, $this->basicTable->getVisibleTabletColumnsCount()); - - $this->basicTable->getColumnBySelectName('id')->collapseOnTablet(); - $this->basicTable->getColumnBySelectName('name')->collapseOnTablet(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertSame(7, $this->basicTable->getVisibleTabletColumnsCount()); - } - - // / *** ** // - - public function test_can_tell_if_columns_should_collapse_always(): void - { - $this->assertFalse($this->basicTable->shouldCollapseAlways()); - - $this->basicTable->getColumnBySelectName('id')->collapseAlways(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertTrue($this->basicTable->shouldCollapseAlways()); - } - - public function test_can_get_always_collapsed_columns(): void - { - $this->assertCount(0, $this->basicTable->getCollapsedAlwaysColumns()); - - $this->basicTable->getColumnBySelectName('id')->collapseAlways(); - $this->basicTable->getColumnBySelectName('name')->collapseAlways(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertCount(2, $this->basicTable->getCollapsedAlwaysColumns()); - $this->assertSame('ID', $this->basicTable->getCollapsedAlwaysColumns()[0]->getTitle()); - $this->assertSame('Name', $this->basicTable->getCollapsedAlwaysColumns()[1]->getTitle()); - } - - public function test_can_get_always_collapsed_columns_count(): void - { - $this->assertSame(0, $this->basicTable->getCollapsedAlwaysColumnsCount()); - - $this->basicTable->getColumnBySelectName('id')->collapseAlways(); - $this->basicTable->getColumnBySelectName('name')->collapseAlways(); - - $this->basicTable->unsetCollapsedStatuses(); - - $this->assertSame(2, $this->basicTable->getCollapsedAlwaysColumnsCount()); - } - /*public function test_can_get_selectable_columns(): void { $selectable = $this->basicTable->getSelectableColumns() diff --git a/tests/Unit/Views/Traits/Helpers/ColumnCollapseHelpersTest.php b/tests/Unit/Views/Traits/Helpers/ColumnCollapseHelpersTest.php new file mode 100644 index 000000000..3bdfad549 --- /dev/null +++ b/tests/Unit/Views/Traits/Helpers/ColumnCollapseHelpersTest.php @@ -0,0 +1,65 @@ +assertFalse($column->shouldCollapseOnMobile()); + $this->assertFalse($column->shouldCollapseSometimes()); + $this->assertTrue($column->shouldNeverCollapse()); + $this->assertTrue($column->shouldCollapseNever()); + + $column->collapseOnMobile(); + + $this->assertTrue($column->shouldCollapseOnMobile()); + $this->assertTrue($column->shouldCollapseSometimes()); + $this->assertFalse($column->shouldNeverCollapse()); + $this->assertFalse($column->shouldCollapseNever()); + + } + + public function test_can_check_if_column_should_collapse_on_tablet(): void + { + $column = Column::make('My Title'); + + $this->assertFalse($column->shouldCollapseOnTablet()); + $this->assertFalse($column->shouldCollapseSometimes()); + $this->assertTrue($column->shouldNeverCollapse()); + $this->assertTrue($column->shouldCollapseNever()); + + $column->collapseOnTablet(); + + $this->assertTrue($column->shouldCollapseOnTablet()); + $this->assertTrue($column->shouldCollapseSometimes()); + $this->assertFalse($column->shouldNeverCollapse()); + $this->assertFalse($column->shouldCollapseNever()); + + } + + public function test_can_check_if_column_should_collapse_always(): void + { + $column = Column::make('My Title'); + + $this->assertFalse($column->shouldCollapseAlways()); + $this->assertFalse($column->shouldCollapseSometimes()); + $this->assertTrue($column->shouldNeverCollapse()); + $this->assertTrue($column->shouldCollapseNever()); + + $column->collapseAlways(); + + $this->assertTrue($column->shouldCollapseAlways()); + $this->assertTrue($column->shouldCollapseSometimes()); + $this->assertFalse($column->shouldNeverCollapse()); + $this->assertFalse($column->shouldCollapseNever()); + + } +} diff --git a/tests/Unit/Views/Traits/Helpers/ColumnHelpersTest.php b/tests/Unit/Views/Traits/Helpers/ColumnHelpersTest.php index 1e91f776e..6a8e3f09f 100644 --- a/tests/Unit/Views/Traits/Helpers/ColumnHelpersTest.php +++ b/tests/Unit/Views/Traits/Helpers/ColumnHelpersTest.php @@ -75,28 +75,6 @@ public function test_can_check_if_column_is_label(): void $this->assertTrue($column->isLabel()); } - public function test_can_check_if_column_should_collapse_on_mobile(): void - { - $column = Column::make('My Title'); - - $this->assertFalse($column->shouldCollapseOnMobile()); - - $column->collapseOnMobile(); - - $this->assertTrue($column->shouldCollapseOnMobile()); - } - - public function test_can_check_if_column_should_collapse_on_tablet(): void - { - $column = Column::make('My Title'); - - $this->assertFalse($column->shouldCollapseOnTablet()); - - $column->collapseOnTablet(); - - $this->assertTrue($column->shouldCollapseOnTablet()); - } - public function test_can_set_custom_sorting_pill_title(): void { $column = Column::make('My Title');