From 19493d45a498d91b1ca2d72fa9d3a3efd42fbaf6 Mon Sep 17 00:00:00 2001 From: Cristhofer Montalvo Date: Sun, 26 Jun 2022 16:28:00 -0500 Subject: [PATCH] add component column and test --- src/Views/Columns/ComponentColumn.php | 60 +++++++++++++++++++ .../ComponentColumnConfiguration.php | 24 ++++++++ .../Traits/Helpers/ComponentColumnHelpers.php | 39 ++++++++++++ tests/Views/ComponentColumnTest.php | 36 +++++++++++ .../ComponentColumnConfigurationTest.php | 38 ++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 src/Views/Columns/ComponentColumn.php create mode 100644 src/Views/Traits/Configuration/ComponentColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/ComponentColumnHelpers.php create mode 100644 tests/Views/ComponentColumnTest.php create mode 100644 tests/Views/Traits/Configuration/ComponentColumnConfigurationTest.php diff --git a/src/Views/Columns/ComponentColumn.php b/src/Views/Columns/ComponentColumn.php new file mode 100644 index 000000000..762e68616 --- /dev/null +++ b/src/Views/Columns/ComponentColumn.php @@ -0,0 +1,60 @@ +isLabel()) { + throw new DataTableConfigurationException('You can not use a label column with a component column'); + } + + if (false === $this->hasComponentView()) { + throw new DataTableConfigurationException('You must specify a component view for a component column'); + } + + $attributes = []; + $value = $this->getValue($row); + $slotContent = $value; + + if ($this->hasAttributesCallback()) { + $attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this); + + if (!is_array($attributes)) { + throw new DataTableConfigurationException('The return type of callback must be an array'); + } + } + if ($this->hasSlotCallback()) { + $slotContent = call_user_func($this->getSlotCallback(), $value, $row, $this); + if (is_string($slotContent)) { + $slotContent = new HtmlString($slotContent); + } + } + + return view($this->getComponentView(), [ + 'attributes' => new ComponentAttributeBag($attributes), + 'slot' => $slotContent, + ]); + } +} diff --git a/src/Views/Traits/Configuration/ComponentColumnConfiguration.php b/src/Views/Traits/Configuration/ComponentColumnConfiguration.php new file mode 100644 index 000000000..c4e8beff2 --- /dev/null +++ b/src/Views/Traits/Configuration/ComponentColumnConfiguration.php @@ -0,0 +1,24 @@ +componentView = 'components.' . $component; + return $this; + } + + public function attributes(callable $callback): self + { + $this->attributesCallback = $callback; + return $this; + } + + public function slot(callable $callback): self + { + $this->slotCallback = $callback; + return $this; + } +} diff --git a/src/Views/Traits/Helpers/ComponentColumnHelpers.php b/src/Views/Traits/Helpers/ComponentColumnHelpers.php new file mode 100644 index 000000000..7db29ae87 --- /dev/null +++ b/src/Views/Traits/Helpers/ComponentColumnHelpers.php @@ -0,0 +1,39 @@ +attributesCallback; + } + + public function hasAttributesCallback(): bool + { + return $this->attributesCallback !== null; + } + + public function getSlotCallback() + { + return $this->slotCallback; + } + + public function hasSlotCallback(): bool + { + return $this->slotCallback !== null; + } + + /** + * Get the value of componentView + */ + public function getComponentView() + { + return $this->componentView; + } + + public function hasComponentView(): bool + { + return isset($this->componentView); + } +} diff --git a/tests/Views/ComponentColumnTest.php b/tests/Views/ComponentColumnTest.php new file mode 100644 index 000000000..86276f063 --- /dev/null +++ b/tests/Views/ComponentColumnTest.php @@ -0,0 +1,36 @@ +expectException(DataTableConfigurationException::class); + ComponentColumn::make('Name') + ->component('alert') + ->attributes(fn () => 'string')->getContents(Pet::find(1)); + } + + /** @test */ + public function component_column_component_has_to_be_an_string() + { + $column = ComponentColumn::make('Name') + ->component('alert'); + $this->assertEquals('components.alert', $column->getComponentView()); + } + + /** @test */ + public function component_column_component_view_has_to_be_set() + { + $this->expectException(DataTableConfigurationException::class); + ComponentColumn::make('Name') + ->getContents(Pet::find(1)); + } +} diff --git a/tests/Views/Traits/Configuration/ComponentColumnConfigurationTest.php b/tests/Views/Traits/Configuration/ComponentColumnConfigurationTest.php new file mode 100644 index 000000000..ce64f8a1d --- /dev/null +++ b/tests/Views/Traits/Configuration/ComponentColumnConfigurationTest.php @@ -0,0 +1,38 @@ +assertFalse($column->hasSlotCallback()); + + $column->slot(fn ($value) => $value); + + $this->assertTrue($column->hasSlotCallback()); + + $this->assertTrue($column->getSlotCallback() instanceof Closure); + } + + /** @test */ + public function component_column_can_set_attributes_callback(): void + { + $column = ComponentColumn::make('Name'); + + $this->assertFalse($column->hasAttributesCallback()); + + $column->attributes(fn ($value) => $value); + + $this->assertTrue($column->hasAttributesCallback()); + + $this->assertTrue($column->getAttributesCallback() instanceof Closure); + } +}