Skip to content

Commit

Permalink
collapsable tree, dynamic icons, asset transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 2, 2023
1 parent 7bd523f commit 2a92175
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 84 deletions.
5 changes: 4 additions & 1 deletion src/actions/CompileTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public function handle($force = false)

$slotNames = $component->getAccessed()->map(fn (SlotDefinition $defn) => $defn->getConfig())->toArray();

$exportedPropTypes = collect($exports->exports['propTypes'] ?? [])
->map(fn (FieldDefinition $defn, string $key) => $defn->handle($key));

$propTypes = $props->getAccessed()
->each(fn (FieldDefinition $defn, string $key) => $defn->config = array_merge($defn->config, $exports->exports['propTypes'][$key]->config ?? []))
->merge($exportedPropTypes)
->map(fn (FieldDefinition $defn) => $defn->config)
->toArray();

Expand Down
1 change: 1 addition & 0 deletions src/attributes/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getInputHtml(): string
['label' => 'Inline', 'value' => 'inline'],
['label' => 'Flex', 'value' => 'flex'],
['label' => 'Grid', 'value' => 'grid'],
['label' => 'None', 'value' => 'none'],
],
'value' => $this->value,
]);
Expand Down
7 changes: 2 additions & 5 deletions src/base/ComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\base\FieldInterface;
use craft\helpers\Html;
use Illuminate\Support\Collection;
use Twig\Markup;

Expand Down Expand Up @@ -44,11 +45,7 @@ public function getHandle(): string

public function getIcon(array $attributes = []): Markup|string
{
$attributes = collect($attributes)
->map(fn ($v, $k) => "{$k}=\"{$v}\"")
->join(' ');

return new Markup(str_replace('<svg', '<svg '.$attributes, $this->icon), 'utf-8');
return new Markup(Html::modifyTagAttributes($this->icon, $attributes), 'utf-8');
}

public function render(array $variables = []): string
Expand Down
3 changes: 1 addition & 2 deletions src/base/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class FieldDefinition
{
public function __construct(
public array $config = ['className' => PlainText::class],
) {
}
) { }

public static function for(string $name): self
{
Expand Down
4 changes: 4 additions & 0 deletions src/fields/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st

public function normalizeValue(mixed $value, ElementInterface $element = null): mixed
{
if (empty($value)) {
$value = ['class' => EntryCondition::class];
}

$condition = Craft::$app->getConditions()->createCondition($value);

$condition->modifyQuery($query = new EntryQuery(Entry::class));
Expand Down
14 changes: 9 additions & 5 deletions src/models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use craft\base\ElementInterface;
use craft\base\FieldInterface;
use craft\db\ActiveQuery;
use craft\helpers\Html;
use Illuminate\Support\Collection;
use markhuot\keystone\actions\GetComponentType;
use markhuot\keystone\actions\NormalizeFieldDataForComponent;
Expand Down Expand Up @@ -204,12 +205,13 @@ public function setSlot(?string $slot): void
$this->setAttribute('slot', $slot === '' ? null : $slot);
}

public function render(): string
public function render(array $props=[]): string
{
return $this->getType()->render([
'component' => $this,
'props' => $this->getProps(),
'attributes' => $this->getAttributeBag(),
...$props,
]);
}

Expand Down Expand Up @@ -239,16 +241,18 @@ public function add(mixed $key, mixed $value): void
}
};

$this->getType()->render([
'component' => $this,
'props' => $this->data,
'attributes' => new AttributeBag($this->data->getDataAttributes()),
$this->render([
'exports' => $exports,
]);

return $exports->exports;
}

public function getIcon(?array $attributes)
{
return Html::modifyTagAttributes($this->getExports()['icon'], $attributes) ?? $this->getType()->getIcon($attributes);
}

public function __toString(): string
{
$html = $this->render();
Expand Down
7 changes: 7 additions & 0 deletions src/models/ComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ public function merge(array $new): self

$old = $this->getData();

// Replace out attributes first
$new['_attributes'] = array_replace($old['_attributes'] ?? [], $new['_attributes'] ?? []);
if (empty($new['_attributes'])) {
unset($new['_attributes']);
}

// This used to be array_replace_recursive, which seems nice. Then we could pass
// in a very sparse fieldset and retain existing data and only update what we
// actually want to change. But a good chunk of Craft fields don't work that
Expand All @@ -191,6 +197,7 @@ public function merge(array $new): self
// Because of that we only merge the top level keys, anything deeper must be passed
// in full if you want to retain existing data.
$new = array_replace($old, $new);

$this->setAttribute('data', $new);

return $this;
Expand Down
Loading

0 comments on commit 2a92175

Please sign in to comment.