Skip to content

Commit

Permalink
allow state to be passed to attribute builder
Browse files Browse the repository at this point in the history
  • Loading branch information
darrencoutts118 committed Dec 20, 2023
1 parent a81dfaf commit 3c76cdf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/AttributeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class AttributeBuilder

public function __construct(
protected ComponentAttributeBag &$attributeBag,
protected Collection $options,
array $elements = []
) {
// loop through each of the elements that have been specified
Expand Down Expand Up @@ -300,7 +299,7 @@ protected function conditionalPasses(string|callable $condition, bool $negateCon
$conditionResult = !$conditionResult;
}

return $conditionResult;
return !!$conditionResult;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Components/Concerns/HasAttributeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait HasAttributeBuilder
*/
protected AttributeBuilder $attributeBuilder;

protected $attributeBuilderStates = [];

/**
* Add a new attribute builder parser at a given weight
*
Expand Down Expand Up @@ -92,6 +94,14 @@ public static function resetAllCustomizations()
static::resetAllAttributeBuilderParsers();
}

public function exposePropertyAsState($property, $state = null) {
if (!$state) {
$state = $property;
}

$this->attributeBuilderStates[$state] = fn () => $this->{$property};
}

/**
* Register a new element for the attribute builder
*
Expand All @@ -116,7 +126,11 @@ protected function registerAttributeBuilderElement(string $element): ElementAttr
protected function runAttributeBuilder(array $data): array
{
// get the instance of the attribute builder
$this->attributeBuilder = new AttributeBuilder($data['attributes'], collect(), $this->attributeBuilderElements);
$this->attributeBuilder = new AttributeBuilder($data['attributes'], $this->attributeBuilderElements);

foreach ($this->attributeBuilderStates as $state => $closure) {
$this->attributeBuilder->registerConditional($state, $closure);
}

// sort the parsers by their weight
ksort(static::$attributeBuilderParsers);
Expand Down
24 changes: 24 additions & 0 deletions tests/ComponentCustomisationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,27 @@
// check that the attributes are created, and in the correct place
expect($instance->getAttributeBuilder()->getAttributes())->toHaveKey('foo', 'bat');
});

it('can pass properties down to state', function () {
HigherOrderTestComponent::customise(function (AttributeBuilder $attributes) {
$attributes->setAttributeWhenToggle('foo', 'bar');
});

// render a component
$this->blade('<x-test-component />');

// get the instance of the component that was rendered
$instance = HigherOrderTestComponent::lastInstance();

// check that the attributes are created, and in the correct place
expect($instance->getAttributeBuilder()->getAttributes())->not()->toHaveKey('foo', 'bar');

// render another component
$this->blade('<x-test-component toggle="toggle" size="xs" />');

// get the instance of the component that was rendered
$instance = HigherOrderTestComponent::lastInstance();

// check that the attributes are created, and in the correct place
expect($instance->getAttributeBuilder()->getAttributes())->toHaveKey('foo', 'bar');
});
2 changes: 1 addition & 1 deletion tests/Components/HigherOrderTestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HigherOrderTestComponent extends TestComponent
*/
private static $lastInstance = null;

public function __construct()
public function __construct($toggle = false, $size = '')
{
// store the last instance
static::$lastInstance = $this;
Expand Down
17 changes: 14 additions & 3 deletions tests/Components/TestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ class TestComponent extends Component
use HasAttributeBuilder;

/**
* Example property that can be set on the component
* Example boolean property that can be set on the component
* @var bool
*/
public bool $property = true;
public bool $toggle = false;

/**
* Example string property that can be set on the component
* @var string
*/
public string $size = '';

/**
* An example element attribute bag
Expand All @@ -28,9 +34,14 @@ class TestComponent extends Component
*
* @return void
*/
public function __construct()
public function __construct($toggle = false, $size = '')
{
$this->toggle = $toggle;
$this->size = $size;

$this->labelAttributes = $this->registerAttributeBuilderElement('label');

$this->exposePropertyAsState('toggle');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
* @param array $options
* @return AttributeBuilder
*/
function createAttributeBuilder($attributes = [], $options = [], $elements = [])
function createAttributeBuilder($attributes = [], $elements = [])
{
// create an attribute bag that will be passed to the attribute builder
$attributeBag = new ComponentAttributeBag($attributes);

// return the attribute builder
return new AttributeBuilder($attributeBag, collect($options), $elements);
return new AttributeBuilder($attributeBag, $elements);
}

function addDataAttributeHelperToAttributeBuilder()
Expand Down

0 comments on commit 3c76cdf

Please sign in to comment.