Skip to content

Commit

Permalink
fix dd
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 14, 2024
1 parent d95dd2a commit bcba48e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 31 deletions.
5 changes: 1 addition & 4 deletions docs/form-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ This function is called with each model record and needs to return an array:
```
$dropdown->renderRowFunction = function (Model $record) {
return [
'value' => $record->idField,
'title' => $record->getTitle() . ' (' . $record->get('subtitle') . ')',
];
}
Expand All @@ -390,7 +389,6 @@ You can also use this function to add an Icon to a record:
```
$dropdown->renderRowFunction = function (Model $record) {
return [
'value' => $record->idField,
'title' => $record->getTitle() . ' (' . $record->get('subtitle') . ')',
'icon' => $record->get('value') > 100 ? 'money' : 'coins',
];
Expand Down Expand Up @@ -426,7 +424,6 @@ With the according renderRowFunction:
```
function (Model $record) {
return [
'value' => $record->getId(),
'title' => $record->getTitle,
'icon' => $record->value > 100 ? 'money' : 'coins',
'someOtherField' => $record->get('SomeOtherField'),
Expand All @@ -435,7 +432,7 @@ function (Model $record) {
}
```

Of course, the tags `value`, `title`, `icon`, `someOtherField` and `someOtherField2` need to be set in my_dropdown.html.
Of course, the tags `title`, `icon`, `someOtherField` and `someOtherField2` need to be set in my_dropdown.html.

### Usage with $values property

Expand Down
2 changes: 1 addition & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Form extends View
public $controlDisplaySelector = '.field';

/** @var array<string, mixed> Use this apiConfig variable to pass API settings to Fomantic-UI in .api(). */
public $apiConfig = [];
public array $apiConfig = [];

/** @var array<string, mixed> Use this formConfig variable to pass settings to Fomantic-UI in .from(). */
public $formConfig = [];
Expand Down
29 changes: 18 additions & 11 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atk4\Ui\Form\Control;

use Atk4\Data\Model;
use Atk4\Ui\HtmlTemplate;
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsExpression;
Expand All @@ -26,7 +27,7 @@ class Dropdown extends Input
* 'file' => ['File', 'icon' => 'file'],
* ].
*
* @var array<int|string, mixed>
* @var array<array-key, mixed>
*/
public array $values;

Expand Down Expand Up @@ -87,15 +88,15 @@ class Dropdown extends Input
* ];
* }
*
* @var \Closure(mixed, int|string|null): array{value: mixed, title: mixed, icon?: mixed}|null
* @var \Closure(Model): array{title: mixed, icon?: mixed}|\Closure(mixed, array-key): array{value: mixed, title: mixed, icon?: mixed}
*/
public $renderRowFunction;
public ?\Closure $renderRowFunction = null;

/** @var HtmlTemplate Subtemplate for a single dropdown item. */
protected $_tItem;
/** Subtemplate for a single dropdown item. */
protected HtmlTemplate $_tItem;

/** @var HtmlTemplate Subtemplate for an icon for a single dropdown item. */
protected $_tIcon;
/** Subtemplate for an icon for a single dropdown item. */
protected HtmlTemplate $_tIcon;

#[\Override]
protected function init(): void
Expand Down Expand Up @@ -270,13 +271,19 @@ protected function _renderItemsForValues(): void
* Used when a custom callback is defined for row rendering. Sets
* values to row template and appends it to main template.
*
* @param mixed $row
* @param int|string $key
* @param mixed $row
* @param array-key $key
*/
protected function _addCallBackRow($row, $key = null): void
{
$res = ($this->renderRowFunction)($row, $key);
$this->_tItem->set('value', (string) $res['value']);
if ($this->model !== null) {
$res = ($this->renderRowFunction)($row);
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastSaveField($this->model->getField($this->model->idField), $row->getId()));
} else {
$res = ($this->renderRowFunction)($row, $key);

Check failure on line 283 in src/Form/Control/Dropdown.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Callable (Closure(Atk4\Data\Model): array{title: mixed, icon?: mixed})|(Closure(mixed, int|string): array{value: mixed, title: mixed, icon?: mixed}) invoked with 2 parameters, 1 required.

Check failure on line 283 in src/Form/Control/Dropdown.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Callable (Closure(Atk4\Data\Model): array{title: mixed, icon?: mixed})|(Closure(mixed, int|string): array{value: mixed, title: mixed, icon?: mixed}) invoked with 2 parameters, 1 required.
$this->_tItem->set('value', (string) $res['value']);

Check failure on line 284 in src/Form/Control/Dropdown.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Offset 'value' does not exist on array{title: mixed, icon?: mixed}.

Check failure on line 284 in src/Form/Control/Dropdown.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Offset 'value' does not exist on array{title: mixed, icon?: mixed}.
}

$this->_tItem->set('title', $res['title']);

$this->_tItem->del('Icon');
Expand Down
28 changes: 16 additions & 12 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Lookup extends Input

public string $inputType = 'hidden';

/** @var array<int|string, mixed> Declare this property so Lookup is consistent as decorator to replace Form\Control\Dropdown. */
/** @var array<array-key, mixed> Declare this property so Lookup is consistent as decorator to replace Form\Control\Dropdown. */
public array $values;

/** @var CallbackLater Object used to capture requests from the browser. */
Expand Down Expand Up @@ -85,7 +85,7 @@ class Lookup extends Input
*
* @var array<string, mixed>
*/
public $apiConfig = ['cache' => false];
public array $apiConfig = ['cache' => false];

/**
* Fomantic-UI dropdown module settings.
Expand Down Expand Up @@ -113,9 +113,9 @@ class Lookup extends Input
* Define callback for generating the row data
* If left empty default callback Lookup::defaultRenderRow is used.
*
* @var \Closure($this, Model): array{value: mixed, title: mixed}|null
* @var \Closure($this, Model): array{title: mixed}
*/
public $renderRowFunction;
public ?\Closure $renderRowFunction = null;

/**
* Whether or not to accept multiple value.
Expand Down Expand Up @@ -180,7 +180,7 @@ public function outputApiResponse(): void
*
* @param int|bool $limit
*
* @return array<int, array{value: mixed, title: mixed}>
* @return array<int, array{value: string, title: mixed}>
*/
public function getData($limit = true): array
{
Expand All @@ -205,12 +205,15 @@ public function getData($limit = true): array
/**
* Renders the Lookup row depending on properties set.
*
* @return array{value: mixed, title: mixed}
* @return array{value: string, title: mixed}
*/
public function renderRow(Model $row): array
{
if ($this->renderRowFunction !== null) {
return ($this->renderRowFunction)($this, $row);
return array_merge(
['value' => $this->defaultRenderRow($row)['value']],
($this->renderRowFunction)($this, $row)
);
}

return $this->defaultRenderRow($row);
Expand All @@ -219,16 +222,17 @@ public function renderRow(Model $row): array
/**
* Default callback for generating data row.
*
* @param string $key
*
* @return array{value: mixed, title: mixed}
* @return array{value: string, title: mixed}
*/
public function defaultRenderRow(Model $row, $key = null)
protected function defaultRenderRow(Model $row)
{
$idField = $this->idField ?? $row->idField;
$titleField = $this->titleField ?? $row->titleField;

return ['value' => $row->get($idField), 'title' => $row->get($titleField)];
return [
'value' => $this->getApp()->uiPersistence->typecastSaveField($row->getField($idField), $row->get($idField)),
'title' => $row->get($titleField),
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function addQuickSearch($fields = [], $hasAutoQuery = false): void
}

#[\Override]
public function jsReload($args = [], $afterSuccess = null, $apiConfig = []): JsExpressionable
public function jsReload($args = [], $afterSuccess = null, array $apiConfig = []): JsExpressionable
{
return new JsReload($this->container, $args, $afterSuccess, $apiConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JsCallback extends Callback
public $confirm;

/** @var array<string, mixed> Use this apiConfig variable to pass API settings to Fomantic-UI in .api(). */
public $apiConfig = [];
public array $apiConfig = [];

/** @var string|null Include web storage data item (key) value to be included in the request. */
public $storeName;
Expand Down
2 changes: 1 addition & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ public function jsAddStoreData(array $data, bool $useSession = false): JsExpress
*
* @return JsReload
*/
public function jsReload($args = [], $afterSuccess = null, $apiConfig = []): JsExpressionable
public function jsReload($args = [], $afterSuccess = null, array $apiConfig = []): JsExpressionable
{
return new JsReload($this, $args, $afterSuccess, $apiConfig);
}
Expand Down

0 comments on commit bcba48e

Please sign in to comment.