diff --git a/resources/js/treeMapChart.js b/resources/js/treeMapChart.js index 3255ebe..739f1a8 100644 --- a/resources/js/treeMapChart.js +++ b/resources/js/treeMapChart.js @@ -54,7 +54,7 @@ const treeMapChart = () => { const block = data[series[seriesIndex].name][dataPointIndex] - component.emit(onBlockClickEventName, block) + component.call('onBlockClick', block) }, } }, diff --git a/src/Charts/LivewireTreeMapChart.php b/src/Charts/LivewireTreeMapChart.php index afeb368..5958217 100644 --- a/src/Charts/LivewireTreeMapChart.php +++ b/src/Charts/LivewireTreeMapChart.php @@ -18,6 +18,17 @@ class LivewireTreeMapChart extends Component $this->treeMapChartModel = $treeMapChartModel->toArray(); } + public function onBlockClick($block) + { + $onBlockClickEventName = data_get($this->treeMapChartModel, 'onBlockClickEventName'); + + if ($onBlockClickEventName === null) { + return; + } + + $this->dispatch($onBlockClickEventName, $block); + } + public function render() { return view('livewire-charts::livewire-tree-map-chart'); diff --git a/tests/LivewireTreeMapChartTest.php b/tests/LivewireTreeMapChartTest.php index b20aeaa..9f7d7c1 100755 --- a/tests/LivewireTreeMapChartTest.php +++ b/tests/LivewireTreeMapChartTest.php @@ -22,4 +22,23 @@ class LivewireTreeMapChartTest extends TestCase //Assert $this->assertNotNull($component); } + + /** @test */ + public function should_emit_event_if_present() + { + //Arrange + $component = $this->buildComponent(); + + $treeMapChartModel = $component->treeMapChartModel; + + data_set($treeMapChartModel, 'onBlockClickEventName', 'custom-event'); + + $component->set('treeMapChartModel', $treeMapChartModel); + + //Act + $component->runAction('onBlockClick', []); + + //Assert + $component->assertDispatched('custom-event'); + } }