Skip to content

Commit

Permalink
unify dropdown with lookup and fix /w left/right action
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 5, 2023
1 parent 84233ba commit 5e302f1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
24 changes: 17 additions & 7 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Atk4\Ui\Form\Control;

use Atk4\Ui\HtmlTemplate;
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsExpressionable;
use Atk4\Ui\Js\JsFunction;

class Dropdown extends Input
{
public $ui = 'dropdown fluid search selection';
public $defaultTemplate = 'form/control/dropdown.html';

public string $inputType = 'hidden';
Expand Down Expand Up @@ -165,12 +165,23 @@ public function setDropdownOptions($options): void
$this->dropdownOptions = array_merge($this->dropdownOptions, $options);
}

/**
* @param bool|string $when
* @param JsExpressionable $action
*
* @return Jquery
*/
protected function jsDropdown($when = false, $action = null): JsExpressionable
{
return $this->js($when, $action, 'div.ui.dropdown:has(> #' . $this->name . '_input)');
}

/**
* Render JS for dropdown.
*/
protected function jsRenderDropdown(): JsExpressionable
{
return $this->js(true)->dropdown($this->dropdownOptions);
return $this->jsDropdown(true)->dropdown($this->dropdownOptions);
}

/**
Expand Down Expand Up @@ -210,22 +221,21 @@ protected function htmlRenderValue(): void
protected function renderView(): void
{
if ($this->multiple) {
$this->addClass('multiple');
$this->template->dangerouslySetHtml('multipleClass', 'multiple');
}

if ($this->readOnly || $this->disabled) {
$this->setDropdownOption('allowTab', false);
$this->removeClass('search');
if ($this->multiple) {
$this->js(true)->find('a i.delete.icon')->attr('class', 'disabled');
$this->jsDropdown(true)->find('a i.delete.icon')->attr('class', 'disabled');
}
}

if ($this->disabled) {
$this->addClass('disabled');
$this->template->set('disabledClass', ' disabled');
$this->template->dangerouslySetHtml('disabled', 'disabled="disabled"');
} elseif ($this->readOnly) {
$this->addClass('read-only');
$this->template->set('disabledClass', ' read-only');
$this->template->dangerouslySetHtml('disabled', 'readonly="readonly"');

$this->setDropdownOption('allowTab', false);
Expand Down
17 changes: 14 additions & 3 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsExpressionable;
use Atk4\Ui\Js\JsFunction;
use Atk4\Ui\Js\JsModal;
use Atk4\Ui\Js\JsToast;
Expand Down Expand Up @@ -130,7 +131,6 @@ protected function init(): void
parent::init();

$this->template->set([
'inputId' => $this->name . '-ac',
'placeholder' => $this->placeholder,
]);

Expand All @@ -142,6 +142,17 @@ protected function init(): void
});
}

/**
* @param bool|string $when
* @param JsExpressionable $action
*
* @return Jquery
*/
protected function jsDropdown($when = false, $action = null): JsExpressionable
{
return $this->js($when, $action, 'div.ui.dropdown:has(> #' . $this->name . '_input)');
}

/**
* Returns URL which would respond with first 50 matching records.
*/
Expand Down Expand Up @@ -261,7 +272,7 @@ protected function initQuickNewRecord(): void
$res->addStatement((new Jquery())->closest('.atk-modal')->modal('hide'));

$row = $this->renderRow($form->model);
$chain = new Jquery('#' . $this->name . '-ac');
$chain = $this->jsDropdown();
$chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']);
$res->addStatement($chain);

Expand Down Expand Up @@ -371,7 +382,7 @@ protected function renderView(): void
], $this->apiConfig['data'] ?? []);
}

$chain = new Jquery('#' . $this->name . '-ac');
$chain = $this->jsDropdown();

$this->initDropdown($chain);

Expand Down
2 changes: 2 additions & 0 deletions template/form/control/dropdown.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

<div {$attributes}>
{$BeforeInput}{$AfterBeforeInput}
<div class="ui dropdown {$multipleClass} search selection{$disabledClass}" style="width: 100%;">
{$Input}
<i class="dropdown icon"></i>
<div class="default text">{$DefaultText}</div>
Expand All @@ -9,5 +10,6 @@
{Item}<div class="item" data-value="{$value}">{Icon}<i class="{$iconClass}"></i>{/Icon}{$title}</div>
{/Item}
</div>
</div>
{$AfterInput}{$AfterAfterInput}
</div>
2 changes: 2 additions & 0 deletions template/form/control/dropdown.pug
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
| <div {$attributes}>
| {$BeforeInput}{$AfterBeforeInput}
| <div class="ui dropdown {$multipleClass} search selection{$disabledClass}" style="width: 100%;">
| {$Input}
| <i class="dropdown icon"></i>
| <div class="default text">{$DefaultText}</div>
Expand All @@ -8,6 +9,7 @@
| {Item}<div class="item" data-value="{$value}">{Icon}<i class="{$iconClass}"></i>{/Icon}{$title}</div>
| {/Item}
| </div>
| </div>
| {$AfterInput}{$AfterAfterInput}
| </div>
= "\n"
2 changes: 1 addition & 1 deletion template/form/control/lookup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div {$attributes}>
{$BeforeInput}{$AfterBeforeInput}
<div id="{$inputId}" class="ui {$multipleClass} search selection dropdown{$disabledClass}" style="width: 100%;">
<div class="ui dropdown {$multipleClass} search selection{$disabledClass}" style="width: 100%;">
{$Input}
<i class="dropdown icon"></i>
<div class="default text">{$placeholder}</div>
Expand Down
2 changes: 1 addition & 1 deletion template/form/control/lookup.pug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| <div {$attributes}>
| {$BeforeInput}{$AfterBeforeInput}
| <div id="{$inputId}" class="ui {$multipleClass} search selection dropdown{$disabledClass}" style="width: 100%;">
| <div class="ui dropdown {$multipleClass} search selection{$disabledClass}" style="width: 100%;">
| {$Input}
| <i class="dropdown icon"></i>
| <div class="default text">{$placeholder}</div>
Expand Down

0 comments on commit 5e302f1

Please sign in to comment.