diff --git a/docs/src/reference/types/column/enum.md b/docs/src/reference/types/column/enum.md index bfa98d7c..538cb11d 100644 --- a/docs/src/reference/types/column/enum.md +++ b/docs/src/reference/types/column/enum.md @@ -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 - \ No newline at end of file + diff --git a/docs/src/reference/types/column/money.md b/docs/src/reference/types/column/money.md index 1b93e704..c16cf904 100644 --- a/docs/src/reference/types/column/money.md +++ b/docs/src/reference/types/column/money.md @@ -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 diff --git a/src/Column/Type/BooleanColumnType.php b/src/Column/Type/BooleanColumnType.php old mode 100755 new mode 100644 index 57b53b7a..3631ff7a --- a/src/Column/Type/BooleanColumnType.php +++ b/src/Column/Type/BooleanColumnType.php @@ -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 @@ -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.') ; } } diff --git a/src/Column/Type/DateTimeColumnType.php b/src/Column/Type/DateTimeColumnType.php old mode 100755 new mode 100644 index a08ccb6d..dcfb177e --- a/src/Column/Type/DateTimeColumnType.php +++ b/src/Column/Type/DateTimeColumnType.php @@ -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 @@ -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) { @@ -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.') ; } diff --git a/src/Column/Type/LinkColumnType.php b/src/Column/Type/LinkColumnType.php old mode 100755 new mode 100644 index 2ed60c3b..15ae8d47 --- a/src/Column/Type/LinkColumnType.php +++ b/src/Column/Type/LinkColumnType.php @@ -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 @@ -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.') ; } diff --git a/src/Column/Type/NumberColumnType.php b/src/Column/Type/NumberColumnType.php old mode 100755 new mode 100644 index e1fadc3b..74f027d2 --- a/src/Column/Type/NumberColumnType.php +++ b/src/Column/Type/NumberColumnType.php @@ -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 @@ -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.') ; } diff --git a/src/Column/Type/TextColumnType.php b/src/Column/Type/TextColumnType.php old mode 100755 new mode 100644 index 8704f364..b907ea10 --- a/src/Column/Type/TextColumnType.php +++ b/src/Column/Type/TextColumnType.php @@ -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.') + ; + } }