Skip to content

Commit

Permalink
reimplement Text content using HtmlValue
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Nov 27, 2024
1 parent ac3de5c commit 50a4b2e
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,47 @@

namespace Atk4\Ui;

use Atk4\Ui\HtmlTemplate\Value as HtmlValue;

/**
* Simple text block view.
*/
class Text extends View
{
public $defaultTemplate;

public $content = '';
/** @var list<HtmlValue> */
public $content = [];

Check failure on line 17 in src/Text.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

PHPDoc type list<Atk4\Ui\HtmlTemplate\Value> of property Atk4\Ui\Text::$content is not the same as PHPDoc type string|null of overridden property Atk4\Ui\View::$content.

Check failure on line 17 in src/Text.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

PHPDoc type list<Atk4\Ui\HtmlTemplate\Value> of property Atk4\Ui\Text::$content is not the same as PHPDoc type string|null of overridden property Atk4\Ui\View::$content.

public function __construct($label = [])
{
$defaults = is_array($label) ? $label : [$label];

if (array_key_exists(0, $defaults)) {
$defaults[0] = (new HtmlValue())->set($defaults[0]);
}

parent::__construct($defaults);
}

#[\Override]
public function renderToHtml(): string
{
return $this->content;
return $this->getHtml();
}

#[\Override]
public function getHtml(): string
{
return $this->content;
return implode('', array_map(static fn ($v) => $v->getHtml(), $this->content));
}

#[\Override]
public function set($text)
{
return parent::set($this->getApp()->encodeHtml($text));
$this->content = [(new HtmlValue())->set($text)];

return $this;
}

/**
Expand All @@ -40,7 +56,9 @@ public function set($text)
*/
public function addParagraph($text)
{
$this->content .= $this->getApp()->getTag('p', [], $text);
$this->content[] = (new HtmlValue())->dangerouslySetHtml('<p>');
$this->content[] = (new HtmlValue())->set($text);
$this->content[] = (new HtmlValue())->dangerouslySetHtml('</p>');

return $this;
}
Expand All @@ -52,7 +70,7 @@ public function addParagraph($text)
*/
public function dangerouslyAddHtml(string $html)
{
$this->content .= $html;
$this->content[] = (new HtmlValue())->dangerouslySetHtml($html);

return $this;
}
Expand Down

0 comments on commit 50a4b2e

Please sign in to comment.