src/Table/Column/Tooltip.php
<?php
declare(strict_types=1);
namespace Atk4\Ui\Table\Column;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Ui\Exception;
use Atk4\Ui\Table;
/**
* Column to add a little icon to show on hover a text
* text is taken by the Row Model in $tooltipField.
*
* $crud->addDecorator('paid_date', new Table\Column\Tooltip('note'));
* $crud->addDecorator('paid_date', new Table\Column\Tooltip('note', 'error red'));
*/
class Tooltip extends Table\Column
{
/** @var string */
public $icon;
public string $tooltipField;
#[\Override]
protected function init(): void
{
parent::init();
if (!$this->icon) {
$this->icon = 'info circle';
}
}
#[\Override]
public function getDataCellHtml(?Field $field = null, array $attr = []): string
{
if ($field === null) {
throw new Exception('Tooltip can be used only with model field');
}
$attr = $this->mergeTagAttributes(
$this->getTagAttributes('body'),
$attr,
['class' => ['{$_' . $field->shortName . '_tooltip}']],
);
$attr['class'] = implode(' ', $attr['class']);
return $this->getApp()->getTag('td', $attr, [
' {$' . $field->shortName . '}',
['span', [
'class' => 'ui icon link {$_' . $field->shortName . '_data_visible_class}',
'data-tooltip' => '{$_' . $field->shortName . '_data_tooltip}',
], [
['i', ['class' => 'ui icon {$_' . $field->shortName . '_icon}']],
]],
]);
}
#[\Override]
public function getHtmlTags(Model $row, ?Field $field): array
{
// @TODO remove popup tooltip when null
$tooltip = $row->get($this->tooltipField);
if ($tooltip === null || $tooltip === '') {
return [
'_' . $field->shortName . '_data_visible_class' => 'transition hidden',
'_' . $field->shortName . '_data_tooltip' => '',
'_' . $field->shortName . '_icon' => '',
];
}
return [
'_' . $field->shortName . '_data_visible_class' => '',
'_' . $field->shortName . '_data_tooltip' => $tooltip,
'_' . $field->shortName . '_icon' => $this->icon,
];
}
}