Skip to content

Commit

Permalink
Fix loadUrl type hint in RegisterClientScript::class.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 22, 2023
1 parent 9d9e7f0 commit 5823b30
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/RegisterClientScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ trait RegisterClientScript
* @phpstan-var array<array-key, mixed>
*/
public array $clientOptions = [];
public string $loadUrl = '';
public array $loadUrl = [];

Check failure on line 18 in src/RegisterClientScript.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1-ubuntu-latest

Property Yii2\Extensions\Selectize\Selectize::$loadUrl type has no value type specified in iterable type array.
public string $queryParam = 'query';

public function registerClientScript(): void
{
$id = $this->options['id'];

if ($this->loadUrl !== '') {
if ($this->loadUrl !== []) {
$url = Url::to($this->loadUrl);
$this->clientOptions['load'] = new JsExpression(
"function (query, callback) { if (!query.length) return callback(); $.getJSON('$url', { {$this->queryParam}: query }, function (data) { callback(data); }).fail(function () { callback(); }); }"
$loadFunction = <<<JS
function (query, callback) {
if (!query.length) return callback();
$.getJSON('$url', { {$this->queryParam}: query }, function (data) {
callback(data);
}).fail(function () {
callback();
});
}
JS
);
}

Expand All @@ -34,6 +43,10 @@ public function registerClientScript(): void

SelectizeAsset::register($view);

$view->registerJs("jQuery('#$id').selectize($options);");
$view->registerJs(
<<<JS
jQuery('#$id').selectize($options);
JS
);
}
}
23 changes: 18 additions & 5 deletions tests/SelectizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testLoadUrl(): void
$selectize = Selectize::widget(
[
'attribute' => 'tags',
'loadUrl' => '/tags',
'loadUrl' => ['/tags'],
'model' => new SelectizeModel(),
],
);
Expand All @@ -158,8 +158,14 @@ public function testLoadUrl(): void

$this->assertStringContainsString(
<<<JS
<script>jQuery(function ($) {
jQuery('#selectizemodel-tags').selectize({"load":function (query, callback) { if (!query.length) return callback(); $.getJSON('/tags', { query: query }, function (data) { callback(data); }).fail(function () { callback(); }); }});
jQuery('#selectizemodel-tags').selectize({"load":function (query, callback) {
if (!query.length) return callback();
$.getJSON('/index.php?r=tags', { query: query }, function (data) {
callback(data);
}).fail(function () {
callback();
});
}});
});</script>
JS,
$render,
Expand All @@ -171,7 +177,7 @@ public function testLoadUrlWithTypeTextInput(): void
$selectize = Selectize::widget(
[
'attribute' => 'tags',
'loadUrl' => '/tags',
'loadUrl' => ['/tags'],
'model' => new SelectizeModel(),
'type' => Selectize::TYPE_TEXT,
],
Expand All @@ -187,7 +193,14 @@ public function testLoadUrlWithTypeTextInput(): void
$this->assertStringContainsString(
<<<JS
<script>jQuery(function ($) {
jQuery('#selectizemodel-tags').selectize({"load":function (query, callback) { if (!query.length) return callback(); $.getJSON('/tags', { query: query }, function (data) { callback(data); }).fail(function () { callback(); }); }});
jQuery('#selectizemodel-tags').selectize({"load":function (query, callback) {
if (!query.length) return callback();
$.getJSON('/index.php?r=tags', { query: query }, function (data) {
callback(data);
}).fail(function () {
callback();
});
}});
});</script>
JS,
$render,
Expand Down

0 comments on commit 5823b30

Please sign in to comment.