Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Allow extra x-data to be passed to alpine forms #10174

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = null)
{
$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
44 changes: 44 additions & 0 deletions tests/Tags/Form/FormCreateAlpineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,50 @@ 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);

$params = ['xdata' => ['extra' => 'no']];

$output = $this->tag('{{ form:contact js="alpine:my_form" :x-data="xdata" }}{{ /form:contact }}', $params);

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

$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
4 changes: 2 additions & 2 deletions tests/Tags/Form/FormTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function post($uri, array $data = [], array $headers = [])
], $headers));
}

protected function tag($tag)
protected function tag($tag, $params = [])
{
return Parse::template($tag, []);
return Parse::template($tag, $params);
}

protected function createForm($blueprintContents = null, $handle = null)
Expand Down
Loading