From bcba48e49f0dd4e91ff33179d5c77f09cef1933d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 14 Feb 2024 13:09:50 +0100 Subject: [PATCH] fix dd --- docs/form-control.md | 5 +---- src/Form.php | 2 +- src/Form/Control/Dropdown.php | 29 ++++++++++++++++++----------- src/Form/Control/Lookup.php | 28 ++++++++++++++++------------ src/Grid.php | 2 +- src/JsCallback.php | 2 +- src/View.php | 2 +- 7 files changed, 39 insertions(+), 31 deletions(-) diff --git a/docs/form-control.md b/docs/form-control.md index e06e853f70..789dc5f051 100644 --- a/docs/form-control.md +++ b/docs/form-control.md @@ -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') . ')', ]; } @@ -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', ]; @@ -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'), @@ -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 diff --git a/src/Form.php b/src/Form.php index 6673b38653..3911370f6b 100644 --- a/src/Form.php +++ b/src/Form.php @@ -105,7 +105,7 @@ class Form extends View public $controlDisplaySelector = '.field'; /** @var array Use this apiConfig variable to pass API settings to Fomantic-UI in .api(). */ - public $apiConfig = []; + public array $apiConfig = []; /** @var array Use this formConfig variable to pass settings to Fomantic-UI in .from(). */ public $formConfig = []; diff --git a/src/Form/Control/Dropdown.php b/src/Form/Control/Dropdown.php index 7949d28fd3..78efa0209c 100644 --- a/src/Form/Control/Dropdown.php +++ b/src/Form/Control/Dropdown.php @@ -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; @@ -26,7 +27,7 @@ class Dropdown extends Input * 'file' => ['File', 'icon' => 'file'], * ]. * - * @var array + * @var array */ public array $values; @@ -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 @@ -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); + $this->_tItem->set('value', (string) $res['value']); + } + $this->_tItem->set('title', $res['title']); $this->_tItem->del('Icon'); diff --git a/src/Form/Control/Lookup.php b/src/Form/Control/Lookup.php index 82c7bd13dc..fca2ccce85 100644 --- a/src/Form/Control/Lookup.php +++ b/src/Form/Control/Lookup.php @@ -27,7 +27,7 @@ class Lookup extends Input public string $inputType = 'hidden'; - /** @var array Declare this property so Lookup is consistent as decorator to replace Form\Control\Dropdown. */ + /** @var array 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. */ @@ -85,7 +85,7 @@ class Lookup extends Input * * @var array */ - public $apiConfig = ['cache' => false]; + public array $apiConfig = ['cache' => false]; /** * Fomantic-UI dropdown module settings. @@ -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. @@ -180,7 +180,7 @@ public function outputApiResponse(): void * * @param int|bool $limit * - * @return array + * @return array */ public function getData($limit = true): array { @@ -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); @@ -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), + ]; } /** diff --git a/src/Grid.php b/src/Grid.php index 520fd7f637..5139bcce64 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -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); } diff --git a/src/JsCallback.php b/src/JsCallback.php index 186f8248d8..4efdcc7083 100644 --- a/src/JsCallback.php +++ b/src/JsCallback.php @@ -18,7 +18,7 @@ class JsCallback extends Callback public $confirm; /** @var array 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; diff --git a/src/View.php b/src/View.php index 8c3ccf089a..22bb389973 100644 --- a/src/View.php +++ b/src/View.php @@ -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); }