Skip to content

Commit

Permalink
Feat: support default sorting on panel
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Jan 2, 2024
1 parent acb42a6 commit 8ea8b30
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ CuratorPicker::make(string $fieldName)
->lazyLoad(bool | Closure $condition) // defaults to true
->listDisplay(bool | Closure $condition) // defaults to true
->tenantAware(bool | Closure $condition) // defaults to true
->defaultPanelSort(string | Closure $direction) // defaults to 'desc'
// see https://filamentphp.com/docs/2.x/forms/fields#file-upload for more information about the following methods
->preserveFilenames()
->maxWidth()
Expand Down
15 changes: 15 additions & 0 deletions src/Components/Forms/CuratorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class CuratorPicker extends Field

protected bool|Closure|null $shouldDisplayAsList = null;

protected string|Closure|null $defaultPanelSort = null;

/**
* @throws Exception
*/
Expand Down Expand Up @@ -152,6 +154,18 @@ public function constrained(bool|Closure|null $condition = true): static
return $this;
}

public function defaultPanelSort(string|Closure|null $direction = 'desc'): static
{
$this->defaultPanelSort = $direction;

return $this;
}

public function getDefaultPanelSort(): string
{
return $this->evaluate($this->defaultPanelSort) ?? 'desc';
}

public function getButtonLabel(): string
{
return $this->evaluate($this->buttonLabel);
Expand Down Expand Up @@ -255,6 +269,7 @@ public function getPickerAction(): Action
->action(function (CuratorPicker $component, \Livewire\Component $livewire) {
$livewire->dispatch('open-modal', id: 'curator-panel', settings: [
'acceptedFileTypes' => $component->getAcceptedFileTypes(),
'defaultSort' => $component->getDefaultPanelSort(),
'directory' => $component->getDirectory(),
'diskName' => $component->getDiskName(),
'imageCropAspectRatio' => $component->getImageCropAspectRatio(),
Expand Down
6 changes: 5 additions & 1 deletion src/Components/Modals/CuratorPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class CuratorPanel extends Component implements HasForms, HasActions

public int $lastPage = 0;

public string $defaultSort = 'desc';

public function mount(): void
{
$this->form->fill();
Expand All @@ -106,6 +108,7 @@ public function openModal(string $id, array $settings = []): void
{
if ($id === 'curator-panel') {
$this->acceptedFileTypes = $settings['acceptedFileTypes'];
$this->defaultSort = $settings['defaultSort'];
$this->directory = $settings['directory'];
$this->diskName = $settings['diskName'];
$this->imageCropAspectRatio = $settings['imageCropAspectRatio'];
Expand Down Expand Up @@ -194,7 +197,8 @@ public function getFiles(int $page = 0, bool $excludeSelected = false): array
$wildcardTypes?->map(fn($type) => $query->orWhere('type', 'LIKE', str_replace('*', '%', $type)));

return $query;
});
})
->orderBy('created_at', $this->defaultSort);

$paginator = $files->paginate($this->defaultLimit, page: $page);

Expand Down

0 comments on commit 8ea8b30

Please sign in to comment.