Skip to content
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

Add badge option to column types #187

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/src/reference/types/column/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ The [`EnumColumnType`](https://github.com/Kreyu/data-table-bundle/blob/main/src/
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.

### `badge`

- **type**: `bool`, `string`, or `callable`
- **default**: `false`

Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.

Example usage:

```php
$builder
->addColumn('status', EnumColumnType::class, [
'badge' => 'primary',
])
;
```

## Inherited options

<ColumnTypeOptions excludedOptions="['formatter']"/>
<ColumnTypeOptions excludedOptions="['formatter']"/>
17 changes: 17 additions & 0 deletions docs/src/reference/types/column/money.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ For more details, see:
- [Intl number formatter documentation](https://www.php.net/manual/en/class.numberformatter.php)
- [Twig `format_currency` filter documentation](https://twig.symfony.com/doc/2.x/filters/format_currency.html)

### `badge`

- **type**: `bool`, `string`, or `callable`
- **default**: `false`

Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.

Example usage:

```php
$builder
->addColumn('price', MoneyColumnType::class, [
'badge' => 'primary',
])
;
```

## Inherited options

<ColumnTypeOptions/>
9 changes: 9 additions & 0 deletions src/Column/Type/BooleanColumnType.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
$view->vars = array_replace($view->vars, [
'label_true' => $options['label_true'],
'label_false' => $options['label_false'],
'badge' => $options['badge'],
]);

if ($options['badge']) {
$badgeClass = is_callable($options['badge']) ? $options['badge']($view->data) : $options['badge'];
$view->vars['attr']['class'] = trim(($view->vars['attr']['class'] ?? '') . ' badge ' . $badgeClass);
}
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -26,11 +32,14 @@ public function configureOptions(OptionsResolver $resolver): void
'label_true' => 'Yes',
'label_false' => 'No',
'value_translation_domain' => 'KreyuDataTable',
'badge' => false,
])
->setAllowedTypes('label_true', ['string', TranslatableInterface::class])
->setAllowedTypes('label_false', ['string', TranslatableInterface::class])
->setAllowedTypes('badge', ['bool', 'string', 'callable'])
->setInfo('label_true', 'Label displayed when the value equals true.')
->setInfo('label_false', 'Label displayed when the value equals false.')
->setInfo('badge', 'Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.')
;
}
}
9 changes: 9 additions & 0 deletions src/Column/Type/DateTimeColumnType.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
$view->vars = array_replace($view->vars, [
'format' => $options['format'],
'timezone' => $options['timezone'],
'badge' => $options['badge'],
]);

if ($options['badge']) {
$badgeClass = is_callable($options['badge']) ? $options['badge']($view->data) : $options['badge'];
$view->vars['attr']['class'] = trim(($view->vars['attr']['class'] ?? '') . ' badge ' . $badgeClass);
}
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -25,6 +31,7 @@ public function configureOptions(OptionsResolver $resolver): void
->setDefaults([
'format' => 'd.m.Y H:i:s',
'timezone' => null,
'badge' => false,
])
->setNormalizer('export', function (Options $options, $value) {
if (true === $value) {
Expand All @@ -47,8 +54,10 @@ public function configureOptions(OptionsResolver $resolver): void
})
->setAllowedTypes('format', ['string'])
->setAllowedTypes('timezone', ['null', 'string'])
->setAllowedTypes('badge', ['bool', 'string', 'callable'])
->setInfo('format', 'A date time string format, supported by the PHP date() function.')
->setInfo('timezone', 'A timezone used to render the date time as string.')
->setInfo('badge', 'Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.')
;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Column/Type/LinkColumnType.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
$view->vars = array_replace($view->vars, [
'href' => $href,
'target' => $target,
'badge' => $options['badge'],
]);

if ($options['badge']) {
$badgeClass = is_callable($options['badge']) ? $options['badge']($view->data) : $options['badge'];
$view->vars['attr']['class'] = trim(($view->vars['attr']['class'] ?? '') . ' badge ' . $badgeClass);
}
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -32,9 +38,12 @@ public function configureOptions(OptionsResolver $resolver): void
->setDefaults([
'href' => '#',
'target' => null,
'badge' => false,
])
->setAllowedTypes('href', ['string', 'callable'])
->setAllowedTypes('target', ['null', 'string', 'callable'])
->setAllowedTypes('badge', ['bool', 'string', 'callable'])
->setInfo('badge', 'Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.')
;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Column/Type/NumberColumnType.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
$view->vars = array_merge($view->vars, [
'use_intl_formatter' => $options['use_intl_formatter'],
'intl_formatter_options' => $options['intl_formatter_options'],
'badge' => $options['badge'],
]);

if ($options['badge']) {
$badgeClass = is_callable($options['badge']) ? $options['badge']($view->data) : $options['badge'];
$view->vars['attr']['class'] = trim(($view->vars['attr']['class'] ?? '') . ' badge ' . $badgeClass);
}
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -34,8 +40,11 @@ public function configureOptions(OptionsResolver $resolver): void
->setAllowedTypes('style', 'string')
;
},
'badge' => false,
])
->setAllowedTypes('use_intl_formatter', 'bool')
->setAllowedTypes('badge', ['bool', 'string', 'callable'])
->setInfo('badge', 'Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.')
;
}

Expand Down
27 changes: 26 additions & 1 deletion src/Column/Type/TextColumnType.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,32 @@

namespace Kreyu\Bundle\DataTableBundle\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class TextColumnType extends AbstractColumnType
{
// ...
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
{
$view->vars = array_replace($view->vars, [
'badge' => $options['badge'],
]);

if ($options['badge']) {
$badgeClass = is_callable($options['badge']) ? $options['badge']($view->data) : $options['badge'];
$view->vars['attr']['class'] = trim(($view->vars['attr']['class'] ?? '') . ' badge ' . $badgeClass);
}
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'badge' => false,
])
->setAllowedTypes('badge', ['bool', 'string', 'callable'])
->setInfo('badge', 'Defines whether the value should be rendered as a badge. Can be a boolean, string, or callable.')
;
}
}
Loading