Skip to content

Commit

Permalink
Allow extra x-data to be passed to alpine forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed May 23, 2024
1 parent ff8bbc9 commit cb85c85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Forms/JsDrivers/AbstractJsDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

use Statamic\Forms\Form;
use Statamic\Support\Str;
use Statamic\Tags\Parameters;

abstract class AbstractJsDriver implements JsDriver
{
protected $form;
protected $options;
protected $params;

/**
* Instantiate JS driver.
*
* @param array $options
*/
public function __construct(Form $form, $options = [])
public function __construct(Form $form, $options = [], ?Parameters $params)
{
$this->form = $form;
$this->options = $options;
$this->params = $params;

if (method_exists($this, 'parseOptions')) {
$this->parseOptions($options);
Expand Down
8 changes: 7 additions & 1 deletion src/Forms/JsDrivers/Alpine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ protected function parseOptions($options)
*/
public function addToFormAttributes()
{
$extraData = $this->params->pull('x-data', []);

if (is_string($extraData)) {
$extraData = json_decode($extraData);
}

return [
'x-data' => $this->renderAlpineXData($this->getInitialFormData(), $this->scope),
'x-data' => $this->renderAlpineXData(collect($this->getInitialFormData())->merge($extraData)->all(), $this->scope),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ protected function parseJsParamDriverAndOptions($value, $form)
throw new \Exception("Cannot find JS driver class for [{$handle}]!");
}

$instance = new $class($form, $options);
$instance = new $class($form, $options, $this->params);

if (! $instance instanceof JsDriver) {
throw new \Exception("JS driver must implement [Statamic\Forms\JsDrivers\JsDriver] interface!");
Expand Down
23 changes: 23 additions & 0 deletions tests/Tags/Form/FormCreateAlpineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,29 @@ public function it_dynamically_renders_field_with_fallback_to_default_partial_x_
$this->assertFieldRendersHtml('<input type="text" name="custom" value="" x-model="my_form.custom">', $config, [], ['js' => 'alpine:my_form']);
}

/** @test */
public function it_merges_any_x_data_passed_to_the_tag()
{
$output = $this->tag('{{ form:contact js="alpine:my_form" \x-data=\'{"extra":"yes"}\' }}{{ /form:contact }}');

$expectedXData = $this->jsonEncode([
'my_form' => [
'name' => null,
'email' => null,
'message' => null,
'fav_animals' => [],
'fav_colour' => null,
'fav_subject' => null,
'winnie' => null,
'extra' => 'yes',
],
]);

$expected = '<form method="POST" action="http://localhost/!/forms/contact" x-data="'.$expectedXData.'">';

$this->assertStringContainsString($expected, $output);
}

private function jsonEncode($data)
{
return Statamic::modify($data)->toJson()->entities();
Expand Down

0 comments on commit cb85c85

Please sign in to comment.