Skip to content

Commit a944be3

Browse files
authored
Initialize Lookup selected values in JS constructor (#2268)
1 parent 6cce1b3 commit a944be3

17 files changed

+432
-142
lines changed

demos/_unit-test/dropdown-html.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Atk4\Ui\Demos;
6+
7+
use Atk4\Data\Model;
8+
use Atk4\Data\Persistence;
9+
use Atk4\Ui\App;
10+
use Atk4\Ui\Form;
11+
use Atk4\Ui\Message;
12+
13+
/** @var App $app */
14+
require_once __DIR__ . '/../init-app.php';
15+
16+
$makeTestStringFx = static fn ($v) => $v . ' <b>"\' &lt;';
17+
$htmlValues = [
18+
$makeTestStringFx('d') => $makeTestStringFx('dTitle'),
19+
$makeTestStringFx('u') => $makeTestStringFx('uTitle'),
20+
];
21+
22+
$form = Form::addTo($app);
23+
24+
$form->addControl('dropdown_single', [
25+
Form\Control\Dropdown::class,
26+
'caption' => 'Dropdown single',
27+
'values' => $htmlValues,
28+
]);
29+
30+
$form->addControl('dropdown_single2', [
31+
Form\Control\Dropdown::class,
32+
'caption' => 'Dropdown single allow addition',
33+
'values' => $htmlValues,
34+
'dropdownOptions' => ['allowAdditions' => true],
35+
]);
36+
37+
$form->addControl('dropdown_multi', [
38+
Form\Control\Dropdown::class,
39+
'caption' => 'Dropdown multiple',
40+
'multiple' => true,
41+
'values' => $htmlValues,
42+
]);
43+
44+
$form->addControl('dropdown_multi2', [
45+
Form\Control\Dropdown::class,
46+
'caption' => 'Dropdown multiple allow addition',
47+
'multiple' => true,
48+
'values' => $htmlValues,
49+
'dropdownOptions' => ['allowAdditions' => true],
50+
]);
51+
52+
$lookupModel = new Model();
53+
$lookupModel->addField('id', ['type' => 'string']);
54+
$lookupModel->addField('name', ['type' => 'string']);
55+
$lookupModel->setPersistence(new Persistence\Array_(array_combine(
56+
array_keys($htmlValues),
57+
array_map(static fn ($v) => ['name' => $v], $htmlValues)
58+
)));
59+
60+
/* $form->addControl('lookup_single', [
61+
Form\Control\Lookup::class,
62+
'caption' => 'Lookup single',
63+
'model' => $lookupModel,
64+
]);
65+
66+
$form->addControl('lookup_single2', [
67+
Form\Control\Lookup::class,
68+
'caption' => 'Lookup single allow addition',
69+
'model' => $lookupModel,
70+
'settings' => ['allowAdditions' => true],
71+
]);
72+
73+
$form->addControl('lookup_multi', [
74+
Form\Control\Lookup::class,
75+
'caption' => 'Lookup multiple',
76+
'multiple' => true,
77+
'model' => $lookupModel,
78+
]);
79+
80+
$form->addControl('lookup_multi2', [
81+
Form\Control\Lookup::class,
82+
'caption' => 'Lookup multiple allow addition',
83+
'multiple' => true,
84+
'model' => $lookupModel,
85+
'settings' => ['allowAdditions' => true],
86+
]); */
87+
88+
foreach (array_keys($form->entity->getFields()) as $k) {
89+
$form->entity->set($k, $makeTestStringFx('d'));
90+
}
91+
92+
$initData = $form->entity->get();
93+
94+
$form->onSubmit(static function (Form $form) use ($app, $initData, $makeTestStringFx) {
95+
$message = $app->encodeJson($form->entity->get());
96+
97+
// TODO remove once https://github.com/fomantic/Fomantic-UI/pull/3205 is merged
98+
foreach ($form->entity->get() as $k => $v) {
99+
$form->entity->set($k, str_replace('&quot;', '"', $v));
100+
}
101+
102+
$view = new Message('Values:');
103+
$view->setApp($form->getApp());
104+
$view->invokeInit();
105+
$view->text->addParagraph($message);
106+
$view->text->addParagraph('match init: ' . ($form->entity->get() === $initData));
107+
$view->text->addParagraph('match u add: ' . ($form->entity->get() === array_map(static fn ($k) => (str_contains($k, 'multi') ? $initData[$k] . ',' : '') . $makeTestStringFx('u'), array_combine(array_keys($initData), array_keys($initData)))));
108+
$view->text->addParagraph('match empty: ' . ($form->entity->get() === array_map(static fn () => '', $initData)));
109+
$view->text->addParagraph('match u only: ' . ($form->entity->get() === array_map(static fn () => $makeTestStringFx('u'), $initData)));
110+
111+
return $view;
112+
});

0 commit comments

Comments
 (0)