-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-10990: Introduced ibexa.Table component
#1825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/bundle/Resources/views/themes/admin/components/table.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| {% trans_default_domain 'ibexa_admin_ui' %} | ||
|
|
||
| <div class="ibexa-scrollable-wrapper"> | ||
| <table class="ibexa-table table ibexa-table--last-column-sticky"> | ||
| <thead> | ||
| <tr class="ibexa-table__row ibexa-table__row--header"> | ||
| {% for column in columns %} | ||
| <th class="ibexa-table__header-cell {{ loop.last ? 'ibexa-table__header-cell--no-min-width' }}"> | ||
| <span class="ibexa-table__header-cell-content"> | ||
| {{ column.label|raw }} | ||
| </span> | ||
| </th> | ||
| {% endfor %} | ||
| </tr> | ||
| </thead> | ||
| <tbody class="ibexa-table__body"> | ||
| {% for item in data %} | ||
| <tr class="ibexa-table__row"> | ||
| {% for column in columns %} | ||
| <td class="ibexa-table__cell {{ loop.last ? 'ibexa-table__cell--has-action-btns' }}"> | ||
| {{ this.renderCell(column, item)|raw }} | ||
| </td> | ||
| {% endfor %} | ||
| </tr> | ||
| {% else %} | ||
| <tr class="ibexa-table__row ibexa-table__row--empty"> | ||
| <td class="ibexa-table__cell" colspan="{{ columns|length }}"> | ||
| <div class="ibexa-empty-state"> | ||
| <div class="ibexa-empty-state__image"> | ||
| <img src="{{ asset('/bundles/ibexaadminui/img/ibexa-empty-table.svg') }}" alt=""> | ||
| </div> | ||
| <h2 class="ibexa-empty-state__title">{{ this.emptyStateTitle|trans }}</h2> | ||
| {% if this.emptyStateDescription %} | ||
| <p class="ibexa-empty-state__description">{{ this.emptyStateDescription|trans }}</p> | ||
| {% endif %} | ||
| {% if this.emptyStateExtraActions %} | ||
| <div class="ibexa-empty-state__extra-actions"> | ||
| {{ this.emptyStateExtraActions|raw }} | ||
| </div> | ||
| {% endif %} | ||
| </div> | ||
| </td> | ||
| </tr> | ||
| {% endfor %} | ||
| </tbody> | ||
| </table> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,140 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||||||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||||||
| */ | ||||||||||
| declare(strict_types=1); | ||||||||||
|
|
||||||||||
| namespace Ibexa\Bundle\AdminUi\Templating\Twig\Components; | ||||||||||
|
|
||||||||||
| use Ibexa\Bundle\AdminUi\Templating\Twig\Components\Table\Column; | ||||||||||
| use JMS\TranslationBundle\Model\Message; | ||||||||||
| use JMS\TranslationBundle\Translation\TranslationContainerInterface; | ||||||||||
| use Symfony\Component\Translation\TranslatableMessage; | ||||||||||
| use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||||||||||
| use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||||||||||
|
|
||||||||||
| #[AsTwigComponent( | ||||||||||
| name: 'ibexa.Table', | ||||||||||
| template: '@ibexadesign/components/table.html.twig', | ||||||||||
| )] | ||||||||||
| final class Table implements TranslationContainerInterface | ||||||||||
| { | ||||||||||
| /** | ||||||||||
| * @var iterable<object> | ||||||||||
| */ | ||||||||||
|
Comment on lines
+24
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| public iterable $data = []; | ||||||||||
|
|
||||||||||
| /** @var class-string|null */ | ||||||||||
| private ?string $dataType = null; | ||||||||||
|
|
||||||||||
| public TranslatableMessage $emptyStateTitle; | ||||||||||
|
|
||||||||||
| public ?TranslatableMessage $emptyStateDescription = null; | ||||||||||
|
|
||||||||||
| public ?string $emptyStateExtraActions = null; | ||||||||||
|
|
||||||||||
| /** @var array<string, mixed> */ | ||||||||||
| public array $parameters = []; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @var array<string, Column> | ||||||||||
| */ | ||||||||||
|
Comment on lines
+41
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| private array $columns = []; | ||||||||||
|
|
||||||||||
| public function __construct() | ||||||||||
| { | ||||||||||
| $this->emptyStateTitle = new TranslatableMessage('table.component.default.empty_title', [], 'ibexa_admin_ui'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @param iterable<object> $data | ||||||||||
| */ | ||||||||||
| public function mount(iterable $data = []): void | ||||||||||
| { | ||||||||||
| if ($data !== []) { | ||||||||||
| $this->data = $data; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function getDataType(): ?string | ||||||||||
| { | ||||||||||
| return $this->dataType ??= $this->inferDataType(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return array<string, Column> | ||||||||||
| */ | ||||||||||
| #[ExposeInTemplate('columns')] | ||||||||||
| public function getColumns(): array | ||||||||||
| { | ||||||||||
| uasort($this->columns, static fn (Column $a, Column $b): int => $b->priority <=> $a->priority); | ||||||||||
|
|
||||||||||
| return $this->columns; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @param callable(mixed): string $renderer | ||||||||||
| */ | ||||||||||
| public function addColumn(string $identifier, string $label, callable $renderer, int $priority = 0): self | ||||||||||
| { | ||||||||||
| $this->columns[$identifier] = new Column($identifier, $label, $renderer, $priority); | ||||||||||
|
|
||||||||||
| return $this; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function removeColumn(string $identifier): self | ||||||||||
| { | ||||||||||
| unset($this->columns[$identifier]); | ||||||||||
|
|
||||||||||
| return $this; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return class-string|null | ||||||||||
| */ | ||||||||||
| private function inferDataType(): ?string | ||||||||||
| { | ||||||||||
| $firstItem = null; | ||||||||||
| foreach ($this->data as $item) { | ||||||||||
| $firstItem = $item; | ||||||||||
| break; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (!is_object($firstItem)) { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $candidates = array_merge( | ||||||||||
| [get_class($firstItem)], | ||||||||||
| class_parents($firstItem), | ||||||||||
| class_implements($firstItem) | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| foreach ($this->data as $item) { | ||||||||||
| $candidates = array_filter($candidates, static fn ($candidate): bool => $item instanceof $candidate); | ||||||||||
| if (empty($candidates)) { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** @var class-string|null $inferredType */ | ||||||||||
| $inferredType = reset($candidates); | ||||||||||
|
|
||||||||||
| return $inferredType; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function renderCell(Column $column, mixed $item): string | ||||||||||
| { | ||||||||||
| return (string) ($column->renderer)($item); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public static function getTranslationMessages(): array | ||||||||||
| { | ||||||||||
| return [ | ||||||||||
| Message::create('table.component.default.empty_title', 'ibexa_admin_ui') | ||||||||||
| ->setDesc('Empty table title'), | ||||||||||
| ]; | ||||||||||
| } | ||||||||||
| } | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||
| <?php | ||||||
|
|
||||||
| /** | ||||||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||
| */ | ||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| namespace Ibexa\Bundle\AdminUi\Templating\Twig\Components\Table; | ||||||
|
|
||||||
| final class Column | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| /** | ||||||
| * @param callable(mixed): string $renderer | ||||||
| */ | ||||||
| public function __construct( | ||||||
| public string $identifier, | ||||||
| public string $label, | ||||||
| public $renderer, | ||||||
| public int $priority = 0, | ||||||
| ) { | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be part of Twig components?