Repository navigation
Expand file tree
/
Copy pathTable.php
More file actions
2316 lines (1971 loc) · 76.6 KB
/
Copy pathTable.php
File metadata and controls
2316 lines (1971 loc) · 76.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace NyonCode\WireTable;
use Closure;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use NyonCode\WireCore\Actions\Action;
use NyonCode\WireCore\Actions\ActionGroup;
use NyonCode\WireCore\Actions\Support\MenuDividers;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Core\Query\Search\SearchConfig;
use NyonCode\WireCore\Core\Support\Trans;
use NyonCode\WireCore\Foundation\Concerns\HasColor;
use NyonCode\WireCore\Foundation\Concerns\HasSheetOnMobile;
use NyonCode\WireCore\Foundation\Icons\Icon;
use NyonCode\WireCore\Foundation\Icons\IconManager;
use NyonCode\WireCore\Foundation\Preferences\Contracts\PreferenceDriver;
use NyonCode\WireCore\Foundation\Routing\ClientNavigation;
use NyonCode\WireCore\Foundation\Support\IslandViewScope;
use NyonCode\WireCore\Foundation\ValueObjects\ShortcutHint;
use NyonCode\WireCore\Foundation\View\Skeleton;
use NyonCode\WireCore\Notifications\Contracts\NotificationDriver;
use NyonCode\WireTable\Actions\TableActionClickResolver;
use NyonCode\WireTable\Columns\Column;
use NyonCode\WireTable\Concerns\CanSelectRecords;
use NyonCode\WireTable\Concerns\HasSqlDebug;
use NyonCode\WireTable\Concerns\WithTable;
use NyonCode\WireTable\Exceptions\TableConfigurationException;
use NyonCode\WireTable\Exceptions\TableHasNoHostException;
use NyonCode\WireTable\Exceptions\TableIntrospectionException;
use NyonCode\WireTable\Filters\Filter;
use NyonCode\WireTable\Filters\SelectFilter;
use NyonCode\WireTable\Preferences\TableViewPayload;
use NyonCode\WireTable\Services\TableIntrospector;
use NyonCode\WireTable\Support\ColumnSet;
use NyonCode\WireTable\Support\TableShortcutLegend;
use Throwable;
/** @phpstan-consistent-constructor */
#[\AllowDynamicProperties]
class Table implements Htmlable
{
use Concerns\CollapsesActionsOnMobile;
use Concerns\HasColumnFilterLayout;
use Concerns\HasColumnLayout;
use Concerns\HasDataSource;
use Concerns\HasFiltersPanel;
use Concerns\HasGestures;
use Concerns\HasGrouping;
use Concerns\HasInactiveRecords;
use Concerns\HasPolling;
use Concerns\HasRecordActions;
use Concerns\HasSubRows;
use Concerns\HasTableActions;
use Concerns\StacksOnMobile;
use HasSheetOnMobile;
use HasSqlDebug;
use Macroable;
protected ?ColumnSet $columns = null;
/** @var array<int, Filter> */
protected array $filters = [];
/** Resolved lazily from `config('wire-table.defaults.per_page')`. */
protected ?int $perPage = null;
/**
* Resolved lazily from `config('wire-table.defaults.per_page_options')`.
*
* @var array<int, int>|null
*/
protected ?array $perPageOptions = null;
/** Whether the footer draws the page-size control. See {@see perPageSelector()}. */
protected bool $showPerPageSelector = true;
/** Resolved lazily from `config('wire-table.defaults.searchable')`. */
protected ?bool $searchable = null;
protected ?SearchConfig $searchConfig = null;
protected bool $queryString = false;
protected string $queryStringPrefix = '';
/** Resolved lazily from `config('wire-table.defaults.sortable')`. */
protected ?bool $sortable = null;
protected bool $paginated = true;
protected bool $selectable = false;
/**
* Pin the bulk bar to the bottom edge on a phone. Null defers to
* `wire-table.defaults.bulk_bar_at_bottom_on_mobile`. See {@see bulkBarAtBottomOnMobile()}.
*/
protected ?bool $bulkBarAtBottomOnMobile = null;
/**
* The selection cell's compiled markup — {@see getSelectionCellSkeleton()} —
* keyed by its one shape beyond the record key: live or inert.
*
* @var array<string, Skeleton>
*/
protected array $selectionCellSkeletons = [];
/** The context-menu panel's compiled markup — {@see getRowContextMenuSkeleton()}. */
protected ?Skeleton $rowContextMenuSkeleton = null;
/**
* The record link's compiled markup, one per navigate flag — {@see getRecordLinkSkeleton()}.
*
* @var array<int, Skeleton>
*/
protected array $recordLinkSkeletons = [];
/**
* The sub-row expander cell, one compiled shape per state.
*
* @var array<string, Skeleton>
*/
protected array $subRowCellSkeletons = [];
/**
* The group header row's compiled markup, one per shape.
*
* {@see getGroupHeaderRow()}
*
* @var array<string, Skeleton>
*/
protected array $groupHeaderSkeletons = [];
// Policy-based authorization
protected bool $usePolicy = false;
protected bool|Closure|null $authorizeCreate = null;
protected bool|Closure|null $authorizeUpdate = null;
protected bool|Closure|null $authorizeDelete = null;
protected bool|Closure|null $authorizeView = null;
protected ?string $defaultSort = null;
protected string $defaultSortDirection = 'asc';
protected ?string $emptyStateHeading = null;
protected ?string $emptyStateDescription = null;
protected ?string $emptyStateIcon = null;
/** Resolved lazily from `config('wire-table.defaults.striped')`. */
protected ?bool $striped = null;
/** Resolved lazily from `config('wire-table.defaults.hoverable')`. */
protected ?bool $hoverable = null;
protected ?string $recordUrl = null;
protected ?Closure $recordUrlCallback = null;
/** The record links' own say in `wire:navigate`, or null to follow `wire-core.navigate`. */
protected ?bool $recordUrlNavigate = null;
protected mixed $livewireComponent = null;
protected string $primaryKey = 'id';
// Table styling
protected bool $compact = false;
protected bool $bordered = false;
protected bool $stickyHeader = false;
/** @var ?string A CSS length capping the scroll region the header pins in; null pins it to the page. */
protected ?string $stickyHeaderMaxHeight = null;
protected ?string $tableClass = null;
protected ?string $headerClass = null;
/** @var string|Closure|null Extra row class(es); a Closure receives the record. */
protected string|Closure|null $rowClass = null;
/** @var string|Closure|null Semantic/hue color tint for a whole row; a Closure receives the record. */
protected string|Closure|null $rowColor = null;
// Lazy loading
protected bool $lazy = false;
protected ?string $lazyPlaceholder = null;
// Pagination mode: 'standard' | 'simple' | 'cursor'
protected string $paginationMode = 'standard';
// Query caching
protected ?int $queryCacheTtl = null;
protected ?string $queryCacheKey = null;
// Notification driver
protected ?NotificationDriver $notificationDriver = null;
// Per-user column preferences: stable key (null = disabled) + optional driver.
protected ?string $rememberColumnsKey = null;
/** Saved-views key; null = off, '' = opted in with nothing to key on. */
protected ?string $savedViewsKey = null;
protected ?PreferenceDriver $preferenceDriver = null;
/** @var array<int, Action|ActionGroup> Dedicated actions for the row right-click menu. */
/** Extra class(es) for the keyboard-active row (null keeps the built-in active style). */
protected ?string $activeRowClass = null;
// Also send a notification (toast) when an inline edit hits an optimistic-lock
// conflict. Off by default — the conflict is always shown inline on the cell,
// so this needs no notification setup; opt in for a more prominent toast.
protected bool $notifyEditConflicts = false;
// Re-render the table in the response that writes an inline edit, so summaries,
// rollups and anything derived from the edited value move with it.
protected bool $refreshAfterEdit = true;
/** Opt-in row-granular rendering — {@see rowPartials()}. */
protected bool $rowPartials = false;
// Excel-style fill handle on editable cells. Opt-in: dragging it overwrites
// rows, which would be a silent behaviour change for every existing table
// with an editable column.
protected bool $fillHandle = false;
// Ceiling on the rows one fill may write. A vertical drag can only reach
// rendered rows, so this normally sits far above any real fill — it is a
// bound on a forged request, not a UX limit.
protected int $fillMaxRecords = 500;
// Ceiling on the rows one bulk action may load at once. "Select all matching"
// can mean a hundred thousand records; materialising those into models is an
// out-of-memory error, so past this the action refuses out loud instead.
// null lifts the cap for a table whose actions are known to stream.
protected ?int $bulkMaxRecords = 1000;
public static function make(): static
{
return new static;
}
/**
* Get raw SQL and bindings separately.
*
* @return array{sql: string, bindings: array<int, mixed>}
*/
public function toRawSql(): array
{
$query = $this->getQuery();
return [
'sql' => $query->toSql(),
'bindings' => $query->getBindings(),
];
}
/**
* Get the SQL query string with bindings replaced.
* Useful for debugging.
*
* @return string The SQL query with values
*/
public function toSql(): string
{
return static::builderToSql($this->getQuery());
}
/**
* Dump the query and continue execution.
*/
public function dump(): static
{
dump([
'sql' => $this->toSql(),
'raw_sql' => $this->getQuery()->toSql(),
'bindings' => $this->getQuery()->getBindings(),
]);
return $this;
}
/**
* Dump the query and stop execution.
*/
public function dd(): never
{
dd([
'sql' => $this->toSql(),
'raw_sql' => $this->getQuery()->toSql(),
'bindings' => $this->getQuery()->getBindings(),
]);
}
/**
* Get column names only.
*
* @return array<int, string>
*/
public function getColumnNames(): array
{
return $this->columnSet()->names();
}
/**
* Dump all columns info and continue.
*/
public function dumpColumns(): static
{
dump([
'defined_columns' => $this->getColumnsInfo(),
'database_columns' => $this->getDatabaseColumns(),
]);
return $this;
}
/**
* Get all defined table columns with their configuration.
*
* @return array<string, array<string, mixed>>
*/
public function getColumnsInfo(): array
{
return app(TableIntrospector::class)->columns($this);
}
public function isSortable(): bool
{
return $this->sortable ?? (bool) config('wire-table.defaults.sortable', true);
}
public function isSearchable(): bool
{
return $this->searchable ?? (bool) config('wire-table.defaults.searchable', true);
}
/**
* Get database table columns (from the model's table).
*/
/**
* @return array<int, string>
*/
public function getDatabaseColumns(): array
{
return app(TableIntrospector::class)->databaseColumns($this);
}
/**
* Dump all columns info and stop.
*/
public function ddColumns(): never
{
dd([
'defined_columns' => $this->getColumnsInfo(),
'database_columns' => $this->getDatabaseColumns(),
'database_columns_info' => $this->getDatabaseColumnsInfo(),
]);
}
/**
* Get detailed database column information.
*/
/**
* @return array<string, array{name: string, type: string}>
*/
public function getDatabaseColumnsInfo(): array
{
return app(TableIntrospector::class)->databaseColumnTypes($this);
}
/**
* Get debug information about the table configuration.
*
* @return array<string, mixed>
*/
public function debug(): array
{
return app(TableIntrospector::class)->configuration($this);
}
/**
* Debug the QueryPlan for the current table configuration.
*
* Shows the planned joins, filters, search, sorting, eager loads, and aggregates
* that the QueryPlanner would produce, for a simulated request.
*
* @param string|null $search Simulated search term
* @param array<string, mixed> $filterValues Simulated filter values
* @param string|null $sortColumn Simulated sort column
* @param string $sortDirection Simulated sort direction
* @return array<string, mixed> QueryPlan debug info
*
* @throws TableIntrospectionException When the query service produced no plan
* to report on — see that class; it cannot
* happen with the bundled service.
*/
public function debugQueryPlan(
?string $search = null,
array $filterValues = [],
?string $sortColumn = null,
string $sortDirection = 'asc',
): array {
return app(TableIntrospector::class)->queryPlan(
$this,
$search,
$filterValues,
$sortColumn,
$sortDirection,
);
}
/**
* Declare the table's columns.
*
* @param array<int, Column> $columns
*/
public function columns(array $columns): static
{
$this->columns = new ColumnSet($columns);
// The row-level inactive rule (if any) has to reach these columns too,
// and it may have been declared before them — see
// {@see Concerns\HasInactiveRecords::shareInactiveStateWithColumns()}.
$this->inactiveStatePending = true;
return $this;
}
/**
* @return array<int, Column>
*/
public function getColumns(): array
{
return $this->columnSet()->all();
}
/**
* The column with this name, or null. The canonical lookup — the Livewire
* host and the fill writer both resolve a client-supplied column name here
* rather than scanning getColumns() themselves.
*/
public function findColumn(string $name): ?Column
{
return $this->columnSet()->find($name);
}
/** This table's columns as a set, empty until {@see columns()} says otherwise. */
protected function columnSet(): ColumnSet
{
$set = $this->columns ??= new ColumnSet;
// The one place every consumer of a column passes through — the render,
// the Livewire host's `findColumn()`, the fill writer — so the table's
// row-level state reaches an editable column exactly once, whichever
// order the two were declared in.
$this->shareInactiveStateWithColumns($set);
return $set;
}
/**
* @param array<int, Filter> $filters
*/
public function filters(array $filters): static
{
$this->filters = $filters;
return $this;
}
/**
* @return array<int, Filter>
*/
public function getFilters(): array
{
// A filter draws its own control, so it has to know whether the panel
// applies on Apply or on every change; the table is what decides. A
// deferred panel's controls write to a draft rather than to the filters
// the query reads, so no other request — a page, a sort, a poll —
// carries a half-made choice into the rows before Apply.
$deferred = $this->hasDeferredFilters();
foreach ($this->filters as $filter) {
$filter->deferred($deferred)
->statePathPrefix($deferred ? 'tableState.filterDraft' : 'tableState.filters');
}
return $this->filters;
}
/**
* Whether any select filter — in the panel or a column header — counts its
* options. Those counts are cached, so a write through this table has
* someone to tell (see WithTable::recordTableWrite()).
*/
public function hasFilterFacets(): bool
{
foreach ($this->filters as $filter) {
if ($filter instanceof SelectFilter && $filter->wantsFacets()) {
return true;
}
}
foreach ($this->getColumns() as $column) {
$filter = $column->getFilter();
if ($filter instanceof SelectFilter && $filter->wantsFacets()) {
return true;
}
}
return false;
}
/**
* How many rows a page holds.
*
* @throws TableConfigurationException on a size that is not a positive whole number
*/
public function perPage(int $perPage): static
{
$this->perPage = $this->normalizePerPageOption($perPage);
return $this;
}
public function getPerPage(): int
{
return $this->perPage ??= $this->normalizePerPageOption(
config('wire-table.defaults.per_page') ?? 10,
);
}
/**
* The page sizes the per-page select offers.
*
* ->perPageOptions([10, 25, 50])
*
* @param array<int, int> $options
*
* @throws TableConfigurationException on a size that is not a positive whole number
*/
public function perPageOptions(array $options): static
{
$this->perPageOptions = array_map($this->normalizePerPageOption(...), array_values($options));
return $this;
}
/**
* Whether the footer offers a page-size `<select>`.
*
* On a grid of columns it is a genuine control: how many rows fit before
* you scroll is the reader's business. On a **list** it is table furniture —
* a reader of an inbox does not think in tens and fifties, and a
* `Show [10] records` dropdown is the last thing that makes a list announce
* itself as a table. Turning it off leaves the paging itself untouched; only
* the control goes.
*/
public function perPageSelector(bool $show = true): static
{
$this->showPerPageSelector = $show;
return $this;
}
public function showsPerPageSelector(): bool
{
return $this->showPerPageSelector;
}
/**
* The page sizes the per-page select offers.
*
* The configured perPage() is always one of them. Without this a table
* declaring perPage(3) against the default [10, 25, 50, 100] renders a
* select whose displayed value (10) contradicts the 3 rows on screen, and
* whose "10" option cannot be chosen because the control already claims to
* be on it.
*
* @return array<int, int>
*/
public function getPerPageOptions(): array
{
$options = $this->perPageOptions ??= array_map(
$this->normalizePerPageOption(...),
array_values((array) config('wire-table.defaults.per_page_options', [10, 25, 50, 100])),
);
$perPage = $this->getPerPage();
if (! in_array($perPage, $options, true)) {
$options[] = $perPage;
}
sort($options);
return $options;
}
/**
* A page size as it is stored: a positive integer.
*
* The value travels to the client and back through a `wire:model` select, a
* query-string parameter and the query cache key, all of which compare it
* strictly as an integer. A numeric string is accepted because a config
* value read from the environment is one; anything else — zero, a negative
* count, a word — is refused rather than handed to the paginator.
*/
protected function normalizePerPageOption(mixed $option): int
{
if (is_string($option) && ctype_digit(trim($option))) {
$option = (int) trim($option);
}
if (! is_int($option) || $option < 1) {
throw TableConfigurationException::invalidPerPageOption($option);
}
return $option;
}
public function searchable(bool $searchable = true): static
{
$this->searchable = $searchable;
return $this;
}
/**
* Configure how the search box interprets what is typed into it.
*
* Everything is off by default — the whole term is one substring match —
* so each capability is opted into explicitly:
*
* $table->search(fn (SearchConfig $s) => $s
* ->tokenize() // spaces mean AND: "Ada Lovelace" spans two columns
* ->ranges() // >100, <=20, 10..20, 2026-01-01..2026-03-31
* ->wildcards() // nov* matches novak
* );
*
* @param Closure(SearchConfig): (SearchConfig|null)|SearchConfig $config
*/
public function search(Closure|SearchConfig $config): static
{
if ($config instanceof SearchConfig) {
$this->searchConfig = $config;
return $this;
}
$resolved = SearchConfig::make();
$this->searchConfig = $config($resolved) ?? $resolved;
return $this;
}
/**
* How this table's search term is parsed (literal when never configured).
*/
public function getSearchConfig(): SearchConfig
{
return $this->searchConfig ??= SearchConfig::make();
}
/**
* Persist search, sort, per-page, and filter state in the URL query string.
*
* Pass a string to prefix every parameter name — required when multiple
* tables with query-string persistence render on the same page:
*
* $table->queryString(); // ?search=…&sort=…&filter_status=…
* $table->queryString('orders_'); // ?orders_search=…&orders_sort=…
*
* The current page is already tracked by Livewire's WithPagination.
*/
public function queryString(bool|string $enabled = true): static
{
if (is_string($enabled)) {
$this->queryString = true;
$this->queryStringPrefix = $enabled;
} else {
$this->queryString = $enabled;
}
return $this;
}
public function hasQueryString(): bool
{
return $this->queryString;
}
public function getQueryStringPrefix(): string
{
return $this->queryStringPrefix;
}
public function sortable(bool $sortable = true): static
{
$this->sortable = $sortable;
return $this;
}
public function paginated(bool $paginated = true): static
{
$this->paginated = $paginated;
return $this;
}
public function isPaginated(): bool
{
return $this->paginated;
}
public function selectable(bool $selectable = true): static
{
$this->selectable = $selectable;
// The default record-action trigger is selection-aware, so the resolver's
// memo must be dropped when selection changes.
$this->recordActionResolver = null;
return $this;
}
public function isSelectable(): bool
{
return $this->selectable || ! empty($this->bulkActions);
}
/**
* Pin the bulk bar to the bottom edge of the screen below `sm`, where the
* thumb is, instead of leaving it above the rows.
*
* Off by default: a fixed bar floats over the records the user is still
* ticking — with several actions it wraps and covers a third of a phone
* screen — and it lands on whatever the application keeps at that edge
* itself, a bottom navigation bar most of all. Above the rows it scrolls
* away with the table instead, which is the trade this switch reverses.
*
* An application with its own bottom bar lifts the pinned bar above it
* with `--wire-table-bulk-bar-offset` (any CSS length, `0px` by default).
*/
public function bulkBarAtBottomOnMobile(?bool $atBottom = true): static
{
$this->bulkBarAtBottomOnMobile = $atBottom;
return $this;
}
/** Whether the bulk bar is pinned to the bottom edge below `sm`. */
public function hasBulkBarAtBottomOnMobile(): bool
{
return $this->bulkBarAtBottomOnMobile
?? (bool) config('wire-table.defaults.bulk_bar_at_bottom_on_mobile', false);
}
/**
* The selection checkbox's tick, resolved once per request.
*
* Record-invariant chrome, so it goes through the canonical IconManager (which
* memoises the rendered SVG by name+size+class) rather than being inlined — the
* row echoes a string instead of re-entering the icon layer per row. Shared by
* the row's cell, the header's select-all and the stacked card view's, so all
* three marks stay one glyph.
*
* `table:checkbox-check`, not Heroicons' solid `check`: the latter is a filled
* silhouette spanning its whole 20x20 viewBox, which inside a 16 px bordered box
* came out as a ~1.2 px hairline pressed into the corners — unreadable next to
* the native checkboxes the rest of the stack draws. See
* `resources/icons/table.php`.
*
* No size class. `absolute inset-0` with no width or height stretches the SVG to
* the button's CONTENT box, which is 14 px inside a 16 px border-box button — so
* the mark centres itself exactly. The old `h-4 w-4` fought that: an explicit
* 16 px box anchored at the padding edge sat a pixel down and right of centre and
* overhung the border.
*/
public function getSelectionCheckIcon(): string
{
return app(IconManager::class)->render('table:checkbox-check', '', 'absolute inset-0 text-white');
}
/**
* The selection checkbox's partial-selection bar, resolved once per request.
*
* The tick's sibling, and resolved the same way for the same reasons. It exists
* so the two states of one control are drawn at one weight: a 2 px tick beside
* Heroicons' 1.2 px `minus` looked like two different checkboxes depending on
* how much of the page was selected. Only the two select-all boxes draw it — a
* row is selected or it is not — so unlike the tick it never reaches the row
* loop.
*/
public function getSelectionIndeterminateIcon(): string
{
return app(IconManager::class)->render('table:checkbox-indeterminate', '', 'absolute inset-0 text-white');
}
/**
* The selection cell, rendered once and spliced per row.
*
* `tables.partials.selection-cell` is still the one source of this markup and
* still the vendor:publish override point; what changed is that it is rendered
* ONCE per table instead of once per row, with a {@see Skeleton} slot standing in
* for the record key. The row loop then fills the key — the same "static once,
* dynamic per row" move the `<tr>` and the `<td>` chrome already make.
*
* The inert copy is the second and last shape: an inactive row whose state
* withholds the checkbox ({@see Support\InactiveRow::selectable()}) splices
* that one instead. Still O(shapes), not O(rows).
*
* Worth it because this cell was the most expensive thing left in the row:
* measured at 2 251 B and 10 whitespace text nodes per row, more than the entire
* rest of a three-column row. Memoised per table instance, which is also per
* render — Livewire rebuilds the Table on every request, so nothing goes stale.
*/
public function getSelectionCellSkeleton(bool $inert = false): Skeleton
{
return $this->selectionCellSkeletons[$inert ? 'inert' : 'live'] ??= Skeleton::compile(
view('wire-table::tables.partials.selection-cell', [
'cellPadding' => $this->getCellPadding(),
'usesRangeSelection' => $this->usesRangeSelection(),
'checkIcon' => $this->getSelectionCheckIcon(),
'inert' => $inert,
'keyJs' => Skeleton::slot('keyJs'),
'key' => Skeleton::slot('key'),
])->render(),
'keyJs',
'key',
);
}
/**
* The link a record url puts around a cell, compiled once and spliced per
* record.
*
* Both surfaces that offer the affordance fill this one skeleton — every
* non-editable cell of a row ({@see Support\RowRenderer}) and the stacked
* card's title ({@see Support\CardRenderer}) — so `tables.partials.record-link`
* is the single source of the markup and the vendor:publish override point for
* both, instead of an `<a>` written twice in PHP.
*
* Memoised per table instance, which is per render: a row wraps one link per
* visible column, so a 10-column page would otherwise pay ten view renders per
* row for markup that never changes.
*
* Two shapes rather than one: whether the link carries `wire:navigate` is
* structure, not a value, and a slot substitutes values. In practice a table
* uses one of them — every record url points into the same application.
*/
public function getRecordLinkSkeleton(bool $navigate = false): Skeleton
{
return $this->recordLinkSkeletons[(int) $navigate] ??= Skeleton::compile(
view('wire-table::tables.partials.record-link', [
'url' => Skeleton::slot('url'),
'navigate' => $navigate,
'content' => Skeleton::slot('content'),
])->render(),
'url',
'content',
);
}
/**
* Enable model policy auto-resolution.
*
* When enabled, create/update/delete/view permissions are resolved
* automatically from the model's Laravel Policy.
*/
public function authorize(bool $usePolicy = true): static
{
$this->usePolicy = $usePolicy;
return $this;
}
public function usesPolicy(): bool
{
return $this->usePolicy;
}
/**
* Override create authorization (hides "New" button when denied).
*/
public function authorizeCreate(bool|Closure $authorize = true): static
{
$this->authorizeCreate = $authorize;
return $this;
}
/**
* Override update authorization (per-row, hides edit action when denied).
*/
public function authorizeUpdate(bool|Closure $authorize = true): static
{
$this->authorizeUpdate = $authorize;
return $this;
}
/**
* Override delete authorization (per-row, hides delete action when denied).
*/
public function authorizeDelete(bool|Closure $authorize = true): static
{
$this->authorizeDelete = $authorize;
return $this;
}
/**
* Override view authorization (per-row, hides view action when denied).
*/
public function authorizeView(bool|Closure $authorize = true): static
{
$this->authorizeView = $authorize;
return $this;
}
/**
* Check if the current user can create a new record.
*/
public function canCreate(): bool
{
if ($this->authorizeCreate !== null) {
return $this->authorizeCreate instanceof Closure
? (bool) ($this->authorizeCreate)()
: $this->authorizeCreate;
}
if ($this->usePolicy && $this->model) {
return Gate::allows('create', $this->model);
}
return true;
}
/**
* Check if the current user can update the given record.
*/
public function canUpdate(EloquentModel $record): bool
{
if ($this->authorizeUpdate !== null) {
return $this->authorizeUpdate instanceof Closure
? (bool) ($this->authorizeUpdate)($record)
: $this->authorizeUpdate;
}
if ($this->usePolicy) {
return Gate::allows('update', $record);
}
return true;
}
/**
* Check if the current user can delete the given record.
*/
public function canDelete(EloquentModel $record): bool
{
if ($this->authorizeDelete !== null) {
return $this->authorizeDelete instanceof Closure
? (bool) ($this->authorizeDelete)($record)
: $this->authorizeDelete;
}
if ($this->usePolicy) {
return Gate::allows('delete', $record);
}
return true;
}
/**
* Check if the current user can view the given record.
*/
public function canView(EloquentModel $record): bool