Upstream filamentphp/filament state of the touched files before 4650cecc5a
This commit is contained in:
commit
c4540cd302
3 files changed
+2575
No files matched your search
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Filament\Tables\Concerns;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
trait CanSortRecords
|
||||||
|
{
|
||||||
|
public ?string $tableSort = null;
|
||||||
|
|
||||||
|
public function sortTable(?string $column = null, ?string $direction = null): void
|
||||||
|
{
|
||||||
|
if ($column === $this->getTableSortColumn()) {
|
||||||
|
$direction ??= match ($this->getTableSortDirection()) {
|
||||||
|
'asc' => 'desc',
|
||||||
|
'desc' => null,
|
||||||
|
default => 'asc',
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
$direction ??= 'asc';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->tableSort = $direction ? "{$column}:{$direction}" : null;
|
||||||
|
|
||||||
|
$this->updatedTableSort();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTableSortColumn(): ?string
|
||||||
|
{
|
||||||
|
if (blank($this->tableSort)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (string) str($this->tableSort)->before(':');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTableSortDirection(): ?string
|
||||||
|
{
|
||||||
|
if (blank($this->tableSort)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! str($this->tableSort)->contains(':')) {
|
||||||
|
return 'asc';
|
||||||
|
}
|
||||||
|
|
||||||
|
return match ((string) str($this->tableSort)->after(':')) {
|
||||||
|
'asc' => 'asc',
|
||||||
|
'desc' => 'desc',
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatedTableSort(): void
|
||||||
|
{
|
||||||
|
if ($this->getTable()->persistsSortInSession()) {
|
||||||
|
session()->put(
|
||||||
|
$this->getTableSortSessionKey(),
|
||||||
|
$this->tableSort,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatedTableSortDirection(): void
|
||||||
|
{
|
||||||
|
if ($this->getTable()->persistsSortInSession()) {
|
||||||
|
session()->put(
|
||||||
|
$this->getTableSortSessionKey(),
|
||||||
|
$this->tableSort,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function applySortingToTableQuery(Builder $query): Builder
|
||||||
|
{
|
||||||
|
if ($this->getTable()->isGroupsOnly()) {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isTableReordering()) {
|
||||||
|
return $query->orderBy($this->getTable()->getReorderColumn(), $this->getTable()->getReorderDirection());
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableSortColumn = $this->getTableSortColumn();
|
||||||
|
|
||||||
|
if (
|
||||||
|
$tableSortColumn &&
|
||||||
|
$column = $this->getTable()->getSortableVisibleColumn($tableSortColumn)
|
||||||
|
) {
|
||||||
|
$sortDirection = $this->getTableSortDirection() === 'desc' ? 'desc' : 'asc';
|
||||||
|
|
||||||
|
$column->applySort($query, $sortDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortDirection = ($this->getTable()->getDefaultSortDirection() ?? $this->getTableSortDirection()) === 'desc' ? 'desc' : 'asc';
|
||||||
|
$defaultSort = $this->getTable()->getDefaultSort($query, $sortDirection);
|
||||||
|
|
||||||
|
if (
|
||||||
|
is_string($defaultSort) &&
|
||||||
|
($defaultSort !== $tableSortColumn) &&
|
||||||
|
($sortColumn = $this->getTable()->getSortableVisibleColumn($defaultSort))
|
||||||
|
) {
|
||||||
|
$sortColumn->applySort($query, $sortDirection);
|
||||||
|
} elseif (
|
||||||
|
is_string($defaultSort) &&
|
||||||
|
$defaultSort !== $tableSortColumn
|
||||||
|
) {
|
||||||
|
$query->orderBy($defaultSort, $sortDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($defaultSort instanceof Builder) {
|
||||||
|
$query = $defaultSort;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->getTable()->hasDefaultKeySort()) {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
$qualifiedKeyName = $query->getModel()->getQualifiedKeyName();
|
||||||
|
|
||||||
|
foreach ($query->getQuery()->orders ?? [] as $order) {
|
||||||
|
if (($order['column'] ?? null) === $qualifiedKeyName) {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
is_string($order['column'] ?? null) &&
|
||||||
|
str($order['column'] ?? null)->afterLast('.')->is(
|
||||||
|
str($qualifiedKeyName)->afterLast('.')
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->orderBy($qualifiedKeyName, $sortDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Override the `table()` method to configure the table.
|
||||||
|
*/
|
||||||
|
protected function getDefaultTableSortColumn(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Override the `table()` method to configure the table.
|
||||||
|
*/
|
||||||
|
protected function getDefaultTableSortDirection(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTableSortSessionKey(): string
|
||||||
|
{
|
||||||
|
return $this->getTableSessionKey('sort');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Override the `table()` method to configure the table.
|
||||||
|
*/
|
||||||
|
protected function shouldPersistTableSortInSession(): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Filament\Tests\Fixtures\Livewire;
|
||||||
|
|
||||||
|
use Filament\Actions\Concerns\InteractsWithActions;
|
||||||
|
use Filament\Actions\Contracts\HasActions;
|
||||||
|
use Filament\Schemas\Concerns\InteractsWithSchemas;
|
||||||
|
use Filament\Schemas\Contracts\HasSchemas;
|
||||||
|
use Filament\Tables;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Filament\Tests\Fixtures\Models\User;
|
||||||
|
use Illuminate\Contracts\View\View;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
class UsersTable extends Component implements HasActions, HasSchemas, Tables\Contracts\HasTable
|
||||||
|
{
|
||||||
|
use InteractsWithActions;
|
||||||
|
use InteractsWithSchemas;
|
||||||
|
use Tables\Concerns\InteractsWithTable;
|
||||||
|
|
||||||
|
public function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->query(User::query())
|
||||||
|
->groups(fn () => [
|
||||||
|
Tables\Grouping\Group::make('name'),
|
||||||
|
Tables\Grouping\Group::make('profile.bio'),
|
||||||
|
Tables\Grouping\Group::make('profile.company.name'),
|
||||||
|
Tables\Grouping\Group::make('profile.setting.theme'),
|
||||||
|
Tables\Grouping\Group::make('image.url'),
|
||||||
|
Tables\Grouping\Group::make('setting.theme'),
|
||||||
|
Tables\Grouping\Group::make('setting.language.name'),
|
||||||
|
Tables\Grouping\Group::make('setting.languageWithTrashed.name'),
|
||||||
|
Tables\Grouping\Group::make('company.name'),
|
||||||
|
Tables\Grouping\Group::make('publishedPost.title'),
|
||||||
|
])
|
||||||
|
->columns([
|
||||||
|
Tables\Columns\TextColumn::make('name')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('email')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('profile.bio')
|
||||||
|
->label('Profile Bio')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('profile.company.name')
|
||||||
|
->label('Company')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('profile.setting.theme')
|
||||||
|
->label('Theme')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('profile.setting.language')
|
||||||
|
->label('Language')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('image.url')
|
||||||
|
->label('Image URL')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('setting.theme')
|
||||||
|
->label('Setting Theme (HasOneThrough)')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('setting.language')
|
||||||
|
->label('Setting Language (HasOneThrough)')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('setting.language.name')
|
||||||
|
->label('Setting Language Name (HasOneThrough -> BelongsTo)')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('setting.languageWithTrashed.name')
|
||||||
|
->label('Setting Language With Trashed (HasOneThrough -> BelongsTo with withTrashed)')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('company.name')
|
||||||
|
->label('Company (BelongsToThrough)')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('publishedPost.title')
|
||||||
|
->label('Published Post Title (HasOne with where())')
|
||||||
|
->sortable()
|
||||||
|
->searchable(),
|
||||||
|
Tables\Columns\TextColumn::make('posts_count')
|
||||||
|
->counts('posts'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): View
|
||||||
|
{
|
||||||
|
return view('livewire.table');
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user