Skip to content

Commit

Permalink
Merge pull request #1425 from moonshine-software/sticky-columns-part-2
Browse files Browse the repository at this point in the history
Sticky columns part 2
  • Loading branch information
lee-to authored Dec 26, 2024
2 parents 7dccfa6 + 6252c5f commit 374ef0d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/Contracts/src/UI/FieldContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function columnSelection(bool $active = true): static;

public function isColumnSelection(): bool;

public function sticky(): static;

public function isStickyColumn(): bool;

public function nullable(Closure|bool|null $condition = null): static;

public function isNullable(): bool;
Expand Down
6 changes: 6 additions & 0 deletions src/Contracts/src/UI/TableBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public function sticky(): static;

public function isSticky(): bool;

public function stickyButtons(): static;

public function isStickyButtons(): bool;

public function calculateStickyClass(): string;

public function columnSelection(): static;

public function isColumnSelection(): bool;
Expand Down
5 changes: 4 additions & 1 deletion src/Laravel/src/Pages/Crud/IndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ protected function getItemsComponent(iterable $items, Fields $fields): Component
->when($this->getResource()->isStickyTable(), function (TableBuilderContract $table): void {
$table->sticky();
})
->when($this->getResource()->isStickyButtons(), function (TableBuilderContract $table): void {
$table->stickyButtons();
})
->when($this->getResource()->isLazy(), function (TableBuilderContract $table): void {
$table->lazy()->whenAsync(fn (TableBuilderContract $t): TableBuilder => $t->items($this->getResource()->getItems()));
$table->lazy()->whenAsync(fn (TableBuilderContract $t): TableBuilderContract => $t->items($this->getResource()->getItems()));
})
->when($this->getResource()->isColumnSelection(), function (TableBuilderContract $table): void {
$table->columnSelection();
Expand Down
7 changes: 7 additions & 0 deletions src/Laravel/src/Resources/CrudResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ abstract class CrudResource extends Resource implements CrudResourceContract

protected bool $columnSelection = false;

protected bool $stickyButtons = false;

protected ?string $casterKeyName = null;

protected bool $isRecentlyCreated = false;
Expand Down Expand Up @@ -242,6 +244,11 @@ public function isColumnSelection(): bool
return $this->columnSelection;
}

public function isStickyButtons(): bool
{
return $this->stickyButtons;
}

public function isSubmitShowWhen(): bool
{
return $this->submitShowWhen;
Expand Down
9 changes: 7 additions & 2 deletions src/UI/src/Collections/TableCells.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ public function pushFields(FieldsContract $fields, ?Closure $builder = null, int
{
$initialBuilder = $builder;

foreach ($fields as $field) {
foreach ($fields as $index => $field) {
$attributes = $field->getWrapperAttributes()->jsonSerialize();

$builder = $attributes !== [] ? static fn (TableCellContract $td): TableCellContract => $td->customAttributes(
$field->getWrapperAttributes()->jsonSerialize()
) : $initialBuilder;

$stickyClass = $index > ($fields->count() / 2) ? 'sticky-col--right' : 'sticky-col--left';

$this->pushCell(
(string) $field,
$startIndex,
$builder,
['data-column-selection' => $field->getIdentity()]
[
'data-column-selection' => $field->getIdentity(),
'class' => $field->isStickyColumn() ? $stickyClass : '',
]
);

$builder = null;
Expand Down
12 changes: 9 additions & 3 deletions src/UI/src/Components/Table/TableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ private function resolveRows(): TableRowsContract
ActionGroup::make($buttons->toArray()),
])->justifyAlign('end'),
index: $fields->count() + ($hasBulk ? 1 : 0),
builder: $tdAttributes,
builder: fn (TableCellContract $td): TableCellContract => $tdAttributes(
$td->customAttributes(['class' => $this->calculateStickyClass(afterCenter: true)])
),
);

$rows->pushRow(
Expand Down Expand Up @@ -444,7 +446,8 @@ private function resolveHeadRow(): TableRowContract
);

if (! $this->isVertical()) {
foreach ($this->getPreparedFields()->onlyVisible() as $field) {
$fields = $this->getPreparedFields()->onlyVisible();
foreach ($fields as $index => $field) {
$thContent = $field->isSortable() && ! $this->isPreview()
?
(string) Link::make(
Expand All @@ -467,6 +470,7 @@ private function resolveHeadRow(): TableRowContract
$cells->push(
TableTh::make($thContent, $index)
->customAttributes(['data-column-selection' => $field->getIdentity()])
->customAttributes(['class' => $field->isStickyColumn() ? $this->calculateStickyClass(afterCenter: $index > ($fields->count() / 2)) : ''])
->customAttributes($tdAttributes($index)),
);

Expand All @@ -475,7 +479,9 @@ private function resolveHeadRow(): TableRowContract

$cells->pushWhen(
$this->hasButtons(),
static fn (): TableTh => TableTh::make('', $index)->customAttributes($tdAttributes($index)),
fn (): TableTh => TableTh::make('', $index)
->customAttributes($tdAttributes($index))
->customAttributes(['class' => $this->isStickyButtons() ? $this->calculateStickyClass(afterCenter: true) : '']),
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/UI/src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ abstract class Field extends FormElement implements FieldContract

protected bool $columnSelection = true;

protected bool $stickyColumn = false;

protected bool $nullable = false;

protected bool $isBeforeLabel = false;
Expand Down Expand Up @@ -124,6 +126,18 @@ public function isColumnSelection(): bool
return $this->columnSelection;
}

public function sticky(): static
{
$this->stickyColumn = true;

return $this;
}

public function isStickyColumn(): bool
{
return $this->stickyColumn;
}

public function nullable(Closure|bool|null $condition = null): static
{
$this->nullable = value($condition, $this) ?? true;
Expand Down
21 changes: 21 additions & 0 deletions src/UI/src/Traits/Table/TableStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ trait TableStates

protected bool $isSticky = false;

protected bool $isStickyButtons = false;

protected bool $isLazy = false;

protected bool $isColumnSelection = false;
Expand Down Expand Up @@ -216,6 +218,23 @@ public function isSticky(): bool
return $this->isSticky;
}

public function stickyButtons(): static
{
$this->isStickyButtons = true;

return $this;
}

public function isStickyButtons(): bool
{
return $this->isStickyButtons;
}

public function calculateStickyClass(bool $afterCenter = false): string
{
return $afterCenter ? 'sticky-col--right' : 'sticky-col--left';
}

public function lazy(): static
{
$this->isLazy = true;
Expand Down Expand Up @@ -282,6 +301,7 @@ public function removeAfterClone(): static
* reorderable: bool,
* simple: bool,
* sticky: bool,
* stickyButtons: bool,
* searchable: bool,
* searchValue: string,
* columnSelection: bool,
Expand All @@ -297,6 +317,7 @@ public function statesToArray(): array
'reorderable' => $this->isReorderable(),
'simple' => $this->isSimple(),
'sticky' => $this->isSticky(),
'stickyButtons' => $this->isStickyButtons(),
'lazy' => $this->isLazy(),
'columnSelection' => $this->isColumnSelection(),
'searchable' => $this->isSearchable(),
Expand Down

0 comments on commit 374ef0d

Please sign in to comment.