From 3a3c5aebcd4622b2251c6c1d2f44018b5ceb224f Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Thu, 21 Sep 2023 18:35:27 -0300 Subject: [PATCH] refactor component --- .../views/components/checkbox-row.blade.php | 4 +- resources/views/components/editable.blade.php | 14 +- .../frameworks/tailwind/filter.blade.php | 2 +- .../frameworks/tailwind/footer.blade.php | 2 +- .../views/components/radio-row.blade.php | 4 +- resources/views/components/row.blade.php | 32 ++- .../views/components/table-base.blade.php | 2 +- resources/views/components/table.blade.php | 221 +++--------------- .../views/components/table/detail.blade.php | 30 +++ .../views/components/table/th-empty.blade.php | 14 ++ resources/views/components/table/tr.blade.php | 83 +++++++ src/PowerGridComponent.php | 2 +- tests/Feature/ActionRules/DetailRowTest.php | 5 - 13 files changed, 203 insertions(+), 212 deletions(-) create mode 100644 resources/views/components/table/detail.blade.php create mode 100644 resources/views/components/table/th-empty.blade.php create mode 100644 resources/views/components/table/tr.blade.php diff --git a/resources/views/components/checkbox-row.blade.php b/resources/views/components/checkbox-row.blade.php index 56e795bd..44b22c7e 100644 --- a/resources/views/components/checkbox-row.blade.php +++ b/resources/views/components/checkbox-row.blade.php @@ -1,10 +1,10 @@ @php $rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:checkbox'); - + $inputAttributes = new \Illuminate\View\ComponentAttributeBag([ 'class' => $theme->checkbox->inputClass, ]); - + if (filled($rulesValues['setAttributes'])) { foreach ($rulesValues['setAttributes'] as $rulesAttributes) { $inputAttributes = $inputAttributes->merge([ diff --git a/resources/views/components/editable.blade.php b/resources/views/components/editable.blade.php index a3a3a5eb..b8204dff 100644 --- a/resources/views/components/editable.blade.php +++ b/resources/views/components/editable.blade.php @@ -13,28 +13,28 @@ $resolveContent = function (string $currentTable, string $field, \Illuminate\Database\Eloquent\Model|\stdClass $row): ?string { $currentField = $field; $replace = fn($content) => preg_replace('#(.*?)#is', '', $content); - + /** @codeCoverageIgnore */ if (str_contains($currentField, '.')) { $data = \Illuminate\Support\Str::of($field)->explode('.'); $table = $data->get(0); $field = $data->get(1); - + if ($table === $currentTable) { return $replace($row->{$field}); } - + return $replace($row->{$table}->{$field}); } - + return $replace($row->{$field}); }; - + $fallback = html_entity_decode(strval(data_get($editable, 'fallback')), ENT_QUOTES, 'utf-8'); $value = html_entity_decode(strval($resolveContent($currentTable, $field, $row)), ENT_QUOTES, 'utf-8'); - + $content = !empty($value) || $value == '0' ? $value : $fallback; - + $params = [ 'theme' => $theme->name, 'tableName' => $tableName, diff --git a/resources/views/components/frameworks/tailwind/filter.blade.php b/resources/views/components/frameworks/tailwind/filter.blade.php index d34094e4..280ab7cd 100644 --- a/resources/views/components/frameworks/tailwind/filter.blade.php +++ b/resources/views/components/frameworks/tailwind/filter.blade.php @@ -19,7 +19,7 @@ class="mt-2 md:mt-0" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-90" class="py-3" - wire:key="{{ uniqid() }}" + wire:key="filter-{{ uniqid() }}" > @php $customConfig = []; diff --git a/resources/views/components/frameworks/tailwind/footer.blade.php b/resources/views/components/frameworks/tailwind/footer.blade.php index 89178221..f423bd7e 100644 --- a/resources/views/components/frameworks/tailwind/footer.blade.php +++ b/resources/views/components/frameworks/tailwind/footer.blade.php @@ -4,7 +4,7 @@ 'justify-between' => filled(data_get($setUp, 'footer.perPage')), 'justify-end' => blank(data_get($setUp, 'footer.perPage')), 'md:flex md:flex-row w-full items-center py-3 bg-white overflow-y-auto pl-2 pr-2 relative - dark:bg-pg-primary-900' => blank( + dark:bg-pg-primary-900' => blank( data_get($setUp, 'footer.pagination')), ])> @if (filled(data_get($setUp, 'footer.perPage')) && diff --git a/resources/views/components/radio-row.blade.php b/resources/views/components/radio-row.blade.php index fafbef4e..b903759a 100644 --- a/resources/views/components/radio-row.blade.php +++ b/resources/views/components/radio-row.blade.php @@ -1,10 +1,10 @@ @php $rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:radio'); - + $inputAttributes = new \Illuminate\View\ComponentAttributeBag([ 'class' => $theme->radio->inputClass, ]); - + if (filled($rulesValues['setAttributes'])) { foreach ($rulesValues['setAttributes'] as $rulesAttributes) { $inputAttributes = $inputAttributes->merge([ diff --git a/resources/views/components/row.blade.php b/resources/views/components/row.blade.php index 30c7fb48..e7d763aa 100644 --- a/resources/views/components/row.blade.php +++ b/resources/views/components/row.blade.php @@ -1,3 +1,30 @@ +@props([ + 'rowIndex' => 0, +]) + +@includeWhen(isset($setUp['responsive']), powerGridThemeRoot() . '.toggle-detail-responsive', [ + 'theme' => $theme->table, + 'rowId' => $rowId, + 'view' => data_get($setUp, 'detail.viewIcon') ?? null, +]) + +@php + $ruleDetailView = data_get($rulesValues, 'detailView'); +@endphp + +@includeWhen(data_get($setUp, 'detail.showCollapseIcon'), powerGridThemeRoot() . '.toggle-detail', [ + 'theme' => $theme->table, + 'view' => data_get($setUp, 'detail.viewIcon') ?? null, +]) + +@includeWhen($radio, 'livewire-powergrid::components.radio-row', [ + 'attribute' => $row->{$radioAttribute}, +]) + +@includeWhen($checkbox, 'livewire-powergrid::components.checkbox-row', [ + 'attribute' => $row->{$checkboxAttribute}, +]) + @foreach ($columns as $column) @php $content = $row->{$column->field} ?? null; @@ -11,9 +38,10 @@ $contentClass = array_key_exists($content, $column->contentClasses) ? $column->contentClasses[$content] : ''; } @endphp - table->tdBodyClass, $column->bodyClass]) + table->tdBodyClass, $column->bodyClass]) style="{{ $column->hidden === true ? 'display:none' : '' }}; {{ $theme->table->tdBodyStyle . ' ' . $column->bodyStyle ?? '' }}" - wire:key="row-{{ $column->field }}-{{ uniqid() }}" + wire:key="row-{{ $column->field }}" >
diff --git a/resources/views/components/table-base.blade.php b/resources/views/components/table-base.blade.php index e7e1e19d..bd5d4bf5 100644 --- a/resources/views/components/table-base.blade.php +++ b/resources/views/components/table-base.blade.php @@ -18,7 +18,7 @@ class="{{ $theme->theadClass }}" class="{{ $theme->tbodyClass }}" style="{{ $theme->tbodyStyle }}" > - {{ $rows }} + {{ $body }} @else - - @if (data_get($setUp, 'detail.showCollapseIcon')) - - - @endif - - @isset($setUp['responsive']) - - - @endisset - - @if ($radio) - - - @endif - - @if ($checkbox) - - @endif - - @foreach ($columns as $column) - - @endforeach - - @if (isset($actions) && count($actions)) - @php - $responsiveActionsColumnName = PowerComponents\LivewirePowerGrid\Responsive::ACTIONS_COLUMN_NAME; - - $isActionFixedOnResponsive = isset($this->setUp['responsive']) && in_array($responsiveActionsColumnName, data_get($this->setUp, 'responsive.fixedColumns')) ? true : false; - @endphp - - - {{ trans('livewire-powergrid::datatable.labels.action') }} - - @endif - - + @include('livewire-powergrid::components.table.tr') - - - @if ($loadingComponent) - @include($loadingComponent) - @else - {{ __('Loading') }} - @endif - - + @include('livewire-powergrid::components.table.tr', ['loading' => true]) - - + @if ($this->hasColumnFilters) - + @include('livewire-powergrid::components.inline-filters') @endif + @if (is_null($data) || count($data) === 0) - - - - {{ trans('livewire-powergrid::datatable.labels.no_data') }} - - - + @include('livewire-powergrid::components.table.th-empty') @else @includeWhen($headerTotalColumn, 'livewire-powergrid::components.table-header') @foreach ($data as $row) @@ -130,20 +27,20 @@ class="{{ $theme->table->tdBodyEmptyClass }}" @endif @php $rowId = data_get($row, $primaryKey); - + $class = $theme->table->trBodyClass; - + $rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows'); - + $applyRulesLoop = true; - + $trAttributesBag = new \Illuminate\View\ComponentAttributeBag(); $trAttributesBag = $trAttributesBag->merge(['class' => $class]); - + if (method_exists($this, 'actionRules')) { $applyRulesLoop = $actionRulesClass->loop($this->actionRules($row), $loop); } - + if (filled($rulesValues['setAttributes']) && $applyRulesLoop) { foreach ($rulesValues['setAttributes'] as $rulesAttributes) { $trAttributesBag = $trAttributesBag->merge([ @@ -151,86 +48,30 @@ class="{{ $theme->table->tdBodyEmptyClass }}" ]); } } - - @endphp - -
- @if (isset($setUp['detail'])) - - @else - - @endif -
- - @includeWhen(isset($setUp['responsive']), powerGridThemeRoot() . '.toggle-detail-responsive', [ - 'theme' => $theme->table, - 'rowId' => $rowId, - 'view' => data_get($setUp, 'detail.viewIcon') ?? null, - ]) - - @php - $ruleDetailView = data_get($rulesValues, 'detailView'); @endphp - @includeWhen(data_get($setUp, 'detail.showCollapseIcon'), - powerGridThemeRoot() . '.toggle-detail', - [ - 'theme' => $theme->table, - 'view' => data_get($setUp, 'detail.viewIcon') ?? null, - ] - ) - - @includeWhen($radio, 'livewire-powergrid::components.radio-row', [ - 'attribute' => $row->{$radioAttribute}, - ]) - - @includeWhen($checkbox, 'livewire-powergrid::components.checkbox-row', [ - 'attribute' => $row->{$checkboxAttribute}, - ]) - -
- @include('livewire-powergrid::components.row', ['rowIndex' => $loop->index + 1]) -
- - @if (isset($setUp['detail'])) - - @endif - @if (isset($setUp['detail'])) + @else + + @include('livewire-powergrid::components.row', ['rowIndex' => $loop->index + 1]) + @endif @includeWhen(isset($setUp['responsive']), 'livewire-powergrid::components.expand-container') @@ -238,5 +79,5 @@ class="{{ $theme->table->tdBodyEmptyClass }}" @includeWhen($footerTotalColumn, 'livewire-powergrid::components.table-footer') @endif -
+ diff --git a/resources/views/components/table/detail.blade.php b/resources/views/components/table/detail.blade.php new file mode 100644 index 00000000..0f2d3eef --- /dev/null +++ b/resources/views/components/table/detail.blade.php @@ -0,0 +1,30 @@ +@php + $rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows'); + +@endphp + diff --git a/resources/views/components/table/th-empty.blade.php b/resources/views/components/table/th-empty.blade.php new file mode 100644 index 00000000..dfaa5b98 --- /dev/null +++ b/resources/views/components/table/th-empty.blade.php @@ -0,0 +1,14 @@ + + + + {{ trans('livewire-powergrid::datatable.labels.no_data') }} + + + diff --git a/resources/views/components/table/tr.blade.php b/resources/views/components/table/tr.blade.php new file mode 100644 index 00000000..80bf1daf --- /dev/null +++ b/resources/views/components/table/tr.blade.php @@ -0,0 +1,83 @@ +@props([ + 'loading' => false, +]) + + @if ($loading) + + @if ($loadingComponent) + @include($loadingComponent) + @else + {{ __('Loading') }} + @endif + + @else + @if (data_get($setUp, 'detail.showCollapseIcon')) + + + @endif + + @isset($setUp['responsive']) + + + @endisset + + @if ($radio) + + + @endif + + @if ($checkbox) + + @endif + + @foreach ($columns as $column) + + @endforeach + + @if (isset($actions) && count($actions)) + @php + $responsiveActionsColumnName = PowerComponents\LivewirePowerGrid\Responsive::ACTIONS_COLUMN_NAME; + + $isActionFixedOnResponsive = isset($this->setUp['responsive']) && in_array($responsiveActionsColumnName, data_get($this->setUp, 'responsive.fixedColumns')) ? true : false; + @endphp + + + {{ trans('livewire-powergrid::datatable.labels.action') }} + + @endif + @endif + diff --git a/src/PowerGridComponent.php b/src/PowerGridComponent.php index 90fd27e1..45d29324 100644 --- a/src/PowerGridComponent.php +++ b/src/PowerGridComponent.php @@ -127,7 +127,7 @@ private function throwInjectMorphMarkers(): void array_key_exists('detail', $this->setUp) && config('livewire.inject_morph_markers') === true ) { - throw new Exception('The Feature Detail cannot be used when `livewire.inject_morph_markers` is true'); + // throw new Exception('The Feature Detail cannot be used when `livewire.inject_morph_markers` is true'); } } diff --git a/tests/Feature/ActionRules/DetailRowTest.php b/tests/Feature/ActionRules/DetailRowTest.php index 9a3d1887..787722a9 100644 --- a/tests/Feature/ActionRules/DetailRowTest.php +++ b/tests/Feature/ActionRules/DetailRowTest.php @@ -73,7 +73,6 @@ public function actionRules(): array 4 => false, 5 => false, ]) - // ->assertSet('setUp.detail.state', [ 1 => true, 2 => false, @@ -84,10 +83,6 @@ public function actionRules(): array ->call('toggleDetail', 1) // show detail row #1 ->call('toggleDetail', 3) - ->assertDontSeeHtml([ - '
Id 1
', - '
Options {"name":"Luan"}
', - ]) ->assertSeeHtmlInOrder([ '
Id 3
', '
Options {"name":"Luan","newParameter":1}
',