Skip to content

Commit 2dbcbd2

Browse files
author
Maxofil
committed
Initial commit
1 parent 9aac8ac commit 2dbcbd2

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "infinityloop-dev/generic-dropdown",
3+
"description": "Component for Nette framwork which creates generic dropdown selects",
4+
"homepage": "https://www.infinityloop.dev/",
5+
"type": "library",
6+
"license": ["MIT"],
7+
"authors": [
8+
{
9+
"name": "Václav Pelíšek",
10+
"homepage": "https://www.peldax.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.4",
15+
"nette/application": "^3.0",
16+
"nette/forms": "^3.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Infinityloop\\": "src/"
21+
}
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div n:snippet="genericDropDownSnippet">
2+
{form form, class => 'ajax'}
3+
{input genericDropdown}
4+
{/form}
5+
</div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\GenericDropdown;
6+
7+
final class GenericDropdown extends \Nette\Application\UI\Control
8+
{
9+
private string $defaultValue;
10+
private array $dropdownValues = [];
11+
private $callback;
12+
13+
public function render() : void
14+
{
15+
$this->template->setFile(__DIR__ . '/GenericDropdown.latte');
16+
$this->template->render();
17+
}
18+
19+
public function setDropdownValues(array $dropdownValues) : GenericDropdown
20+
{
21+
$this->dropdownValues = $dropdownValues;
22+
23+
return $this;
24+
}
25+
26+
public function setCallback(callable $callback) : GenericDropdown
27+
{
28+
$this->callback = $callback;
29+
30+
return $this;
31+
}
32+
33+
public function setDefaultValue(string $defaultValue) : GenericDropdown
34+
{
35+
$this->defaultValue = $defaultValue;
36+
37+
return $this;
38+
}
39+
40+
public function formSuccess(\Nette\Application\UI\Form $form, $values) : void
41+
{
42+
$newValue = $values['genericDropdown'];
43+
44+
if (\is_callable($this->callback)) {
45+
$success = \call_user_func($this->callback, $newValue) ?? true;
46+
$form->reset();
47+
$form->setDefaults(['genericDropdown' => ($success ? $newValue : $this->defaultValue)], true);
48+
}
49+
50+
$this->redrawControl('genericDropDownSnippet');
51+
}
52+
53+
protected function createComponentForm() : \Nette\Application\UI\Form
54+
{
55+
$form = new \Nette\Application\UI\Form();
56+
$form->addProtection();
57+
58+
$form->addSelect('genericDropdown', null, $this->dropdownValues)
59+
->setDefaultValue($this->defaultValue);
60+
61+
$form->onSuccess[] = [$this, 'formSuccess'];
62+
63+
return $form;
64+
}
65+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\GenericDropdown;
6+
7+
interface IGenericDropdownFactory
8+
{
9+
public function create() : \Infinityloop\GenericDropdown\GenericDropdown;
10+
}

0 commit comments

Comments
 (0)