Refactored how event was triggered

This commit is contained in:
asantibanez
2023-07-25 17:07:51 -05:00
parent 1f26a9ed39
commit 46d82cf6bc
3 changed files with 31 additions and 1 deletions

View File

@@ -54,7 +54,7 @@ const treeMapChart = () => {
const block = data[series[seriesIndex].name][dataPointIndex]
component.emit(onBlockClickEventName, block)
component.call('onBlockClick', block)
},
}
},

View File

@@ -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');

View File

@@ -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');
}
}