Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Jul 13, 2024
2 parents 9204bd2 + 5829183 commit fb63a32
Show file tree
Hide file tree
Showing 35 changed files with 1,110 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Code Quality

on:
- push
- pull_request
- workflow_dispatch

jobs:
php-cs-fixer:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Testing

on:
- push
- pull_request
- workflow_dispatch

jobs:
phpunit:
Expand Down
8 changes: 6 additions & 2 deletions assets/controllers/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ export default class extends Controller {
#updateBatchActionBar() {
const uniqueSelectedCount = this.#getUniqueSelectedCount();

this.selectedCounterTarget.innerHTML = uniqueSelectedCount;
this.batchActionBarTarget.hidden = uniqueSelectedCount === 0;
if (this.hasSelectedCounterTarget) {
this.selectedCounterTarget.innerHTML = uniqueSelectedCount;
}
if (this.hasBatchActionBarTarget) {
this.batchActionBarTarget.hidden = uniqueSelectedCount === 0;
}
}

#updateIndeterminateStates() {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.4",
"dg/bypass-finals": "dev-master",
"openspout/openspout": "^4.23"
"openspout/openspout": "^4.23",
"symfony/http-foundation": "^6.0|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 5 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export default defineConfig({
externalLinkIcon: true,
outline: 'deep',

search: {
provider: 'local',
},

// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Home', link: '/' },
{ text: 'Documentation', link: '/docs/introduction', activeMatch: '/docs/' },
{ text: 'Reference', link: '/reference/types/data-table', activeMatch: '/reference/' },
],
Expand Down Expand Up @@ -91,6 +94,7 @@ export default defineConfig({
{ text: 'DateTime', link: '/reference/types/column/date-time' },
{ text: 'DatePeriod', link: '/reference/types/column/date-period' },
{ text: 'Collection', link: '/reference/types/column/collection' },
{ text: 'Enum', link: '/reference/types/column/enum' },
{ text: 'Template', link: '/reference/types/column/template' },
{ text: 'Actions', link: '/reference/types/column/actions' },
{ text: 'Checkbox', link: '/reference/types/column/checkbox' },
Expand Down
41 changes: 31 additions & 10 deletions docs/src/docs/features/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,33 @@ class ProductController extends AbstractController
}
```

Last but not least, you can also overwrite the themes of the data table inside a template:
## Applying themes in Twig

Similar to forms, you can set the data table themes directly in the Twig template, by using the `data_table_theme` tag:

```twig
<div class="card">
{{ data_table(products, { themes: ['@KreyuDataTable/themes/bootstrap_5.html.twig'] }) }}
</div>
{% data_table_theme products 'themes/data_table.html.twig' %}
{{ data_table(products) }}
```

If you wish to use multiple themes, pass an array using the `with` keyword:

```twig
{% data_table_theme products with [
'themes/data_table.html.twig',
'@KreyuDataTable/themes/bootstrap_5.html.twig',
] %}
{{ data_table(products) }}
```

If you wish to disable currently configured themes for the data table and **only** use given ones, add the `only` keyword after the list of data table themes:

```twig
{% data_table_theme products with ['themes/data_table.html.twig'] only %}
{{ data_table(products) }}
```

## Customizing existing theme
Expand All @@ -103,7 +124,7 @@ you can provide your own theme with only a fraction of Twig blocks,
using the built-in themes as a fallback, for example:

```twig
{# templates/data_table/theme.html.twig #}
{# themes/data_table.html.twig #}
{% block column_boolean_value %}
{# ... #}
{% endblock %}
Expand All @@ -114,7 +135,7 @@ using the built-in themes as a fallback, for example:
kreyu_data_table:
defaults:
themes:
- 'templates/data_table/theme.html.twig',
- 'themes/data_table.html.twig',
- '@KreyuDataTable/themes/bootstrap_5.html.twig'
```
Expand All @@ -123,7 +144,7 @@ use Symfony\Config\KreyuDataTableConfig;

return static function (KreyuDataTableConfig $config) {
$config->defaults()->themes([
'templates/data_table/theme.html.twig',
'themes/data_table.html.twig',
'@KreyuDataTable/themes/bootstrap_5.html.twig',
]);
};
Expand All @@ -139,7 +160,7 @@ class ProductDataTableType extends AbstractDataTableType
{
$resolver->setDefaults([
'themes' => [
'templates/data_table/theme.html.twig',
'themes/data_table.html.twig',
'@KreyuDataTable/themes/bootstrap_5.html.twig',
],
]);
Expand All @@ -163,7 +184,7 @@ class ProductController extends AbstractController
query: $query,
options: [
'themes' => [
'templates/data_table/theme.html.twig',
'themes/data_table.html.twig',
'@KreyuDataTable/themes/bootstrap_5.html.twig',
],
],
Expand All @@ -176,7 +197,7 @@ class ProductController extends AbstractController
<div class="card">
{{ data_table(products, {
themes: [
'templates/data_table/theme.html.twig',
'themes/data_table.html.twig',
'@KreyuDataTable/themes/bootstrap_5.html.twig',
]
}) }}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This bundle can be installed at any moment during a project’s lifecycle.
Use [Composer](https://getcomposer.org/) to install the bundle:

```shell
composer require kreyu/data-table-bundle 0.17.*
composer require kreyu/data-table-bundle 0.19.*
```

::: danger This bundle is not production ready!
Expand Down
38 changes: 24 additions & 14 deletions docs/src/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ In most cases, the data tables are created in the controller, using the `createD

```php
use App\Repository\ProductRepository;
use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait; // [!code highlight]
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait; // [!code ++]

class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait; // [!code ++]
use DataTableFactoryAwareTrait; // [!code highlight]

public function index(ProductRepository $productRepository)
{
$query = $productRepository->createQueryBuilder('product'); // [!code ++]
$queryBuilder = $productRepository->createQueryBuilder('product');

$dataTable = $this->createDataTable(ProductDataTableType::class, $query); // [!code ++]
$dataTable = $this->createDataTable(ProductDataTableType::class, $queryBuilder);
}
}
```
Expand All @@ -64,56 +65,65 @@ In order to be able to paginate, sort, filter, personalize or export the data ta

```php
use App\Repository\ProductRepository;
use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;

class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;

public function index(Request $request, ProductRepository $productRepository)
public function index(ProductRepository $productRepository) // [!code --]
public function index(Request $request, ProductRepository $productRepository) // [!code ++]
{
$query = $productRepository->createQueryBuilder('product');
$queryBuilder = $productRepository->createQueryBuilder('product');

$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable = $this->createDataTable(ProductDataTableType::class, $queryBuilder);
$dataTable->handleRequest($request); // [!code ++]
}
}
```

This method calls the [request handler](./features/extensibility.md#request-handlers) to handle all the hard work.

## Rendering the data tables

In order to render the data table, create the data table view and pass it to the template:

```php
use App\Repository\ProductRepository;
use App\DataTable\Type\ProductDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;

class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;

public function index(Request $request, ProductRepository $productRepository)
{
$query = $productRepository->createQueryBuilder('product');
$queryBuilder = $productRepository->createQueryBuilder('product');

$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable = $this->createDataTable(ProductDataTableType::class, $queryBuilder);
$dataTable->handleRequest($request);

return $this->render('product/index.html.twig', [
return $this->render('product/index.html.twig', [ // [!code ++]
'products' => $dataTable->createView(), // [!code ++]
]);
]); // [!code ++]
}
}
```

:::tip Don't forget to call the `createView()` method!
This creates a `DataTableView` object, used to render the data table.
:::

Now, in the template, render the data table using the `data_table` function:

```twig
{# product/index.html.twig #}
{# templates/product/index.html.twig #}
<div class="card">
{{ data_table(products) }}
Expand Down
21 changes: 21 additions & 0 deletions docs/src/reference/types/column/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup>
import ColumnTypeOptions from "./options/column.md";
</script>

# EnumColumnType

The [`EnumColumnType`](https://github.com/Kreyu/data-table-bundle/blob/main/src/Column/Type/EnumColumnType.php) represents a column with [PHP enumeration](https://www.php.net/manual/language.enumerations.php) as value.

## Overridden options

### `formatter`

- **type**: `null` or `callable`
- **default**: callable that translates the enum if possible

Formats the enum value. If Symfony Translator component is available, and the enum implements [`TranslatableInterface`](https://github.com/symfony/translation-contracts/blob/main/TranslatableInterface.php),
the enum will be translated. Otherwise, the enum name will be displayed.

## Inherited options

<ColumnTypeOptions excludedOptions="['formatter']"/>
8 changes: 8 additions & 0 deletions docs/src/reference/types/column/options/column.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const props = defineProps({
property_path: 'null',
},
},
excludedOptions: {
type: Array,
default: ['test'],
},
})
</script>

Expand Down Expand Up @@ -100,6 +104,8 @@ Allows you to add a custom block prefix and override the block name used to rend
Useful for example if you have multiple instances of the same column type, and you need to personalize
the rendering of some of them, without the need to create a new column type.

<span v-if="!excludedOptions.includes('formatter')">

### `formatter`

- **type**: `null` or `callable`
Expand All @@ -115,6 +121,8 @@ $builder
;
```

</span>

### `export`

- **type**: `bool` or `array`
Expand Down
4 changes: 2 additions & 2 deletions src/Action/Type/LinkActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver
->setDefaults([
'href' => '#',
'target' => '_self',
'target' => null,
])
->setAllowedTypes('href', ['string', 'callable'])
->setAllowedTypes('target', ['string', 'callable'])
->setAllowedTypes('target', ['null', 'string', 'callable'])
;
}
}
32 changes: 32 additions & 0 deletions src/Column/Type/EnumColumnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Column\Type;

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class EnumColumnType extends AbstractColumnType
{
public function __construct(
private ?TranslatorInterface $translator,
) {
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('formatter', $this->format(...));
}

protected function format(\UnitEnum $enum): string
{
return $enum instanceof TranslatableInterface ? $enum->trans($this->translator) : $enum->name;
}

public function getParent(): ?string
{
return TextColumnType::class;
}
}
4 changes: 2 additions & 2 deletions src/Column/Type/LinkColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver
->setDefaults([
'href' => '#',
'target' => '_self',
'target' => null,
])
->setAllowedTypes('href', ['string', 'callable'])
->setAllowedTypes('target', ['string', 'callable'])
->setAllowedTypes('target', ['null', 'string', 'callable'])
;
}

Expand Down
Loading

0 comments on commit fb63a32

Please sign in to comment.