Skip to content

Commit

Permalink
Add ability to pass any value into s-method
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Aug 8, 2024
1 parent e69df67 commit a476a2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Added the component configurations to a `Sprig` variable in the browser console when `devMode` is enabled.
- Added the ability to pass any value into `s-method`, including the `put`, `patch` and `delete` verbs.
- Added the `sprig.registerJs(js)` function.

### Changed
Expand Down
12 changes: 5 additions & 7 deletions src/services/ComponentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,10 @@ private function parseAttributes(array &$attributes): void
*/
private function parseSprigAttribute(array &$attributes): void
{
$verb = 'get';
$params = [];
$method = strtolower($this->getSprigAttributeValue($attributes, 'method', 'get'));

$method = $this->getSprigAttributeValue($attributes, 'method');
if (strtolower($method) == 'post') {
$verb = 'post';
if ($method === 'post') {
$this->mergeJsonAttributes($attributes, 'headers', [
Request::CSRF_HEADER => Craft::$app->getRequest()->getCsrfToken(),
]);
Expand All @@ -444,7 +442,7 @@ private function parseSprigAttribute(array &$attributes): void
]);
}

$attributes[self::HTMX_PREFIX . $verb] = $this->getSprigActionUrl($params);
$attributes[self::HTMX_PREFIX . $method] = $this->getSprigActionUrl($params);
}

/**
Expand Down Expand Up @@ -566,7 +564,7 @@ private function getSprigAttributeName(string $key): string
/**
* Returns a Sprig attribute value if it exists.
*/
private function getSprigAttributeValue(array $attributes, string $name): string
private function getSprigAttributeValue(array $attributes, string $name, string $default = ''): string
{
foreach (self::SPRIG_PREFIXES as $prefix) {
if (!empty($attributes[$prefix . $name])) {
Expand All @@ -578,7 +576,7 @@ private function getSprigAttributeValue(array $attributes, string $name): string
}
}

return '';
return $default;
}

/**
Expand Down

0 comments on commit a476a2b

Please sign in to comment.