Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow radio form control to be unchecked #2063

Merged
merged 13 commits into from
Jun 5, 2023
Prev Previous commit
Next Next commit
unify upload control template
mvorisek committed Jun 5, 2023
commit fbad84000984ee9e3155b64e92bb956e29492d03
4 changes: 2 additions & 2 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
@@ -241,10 +241,10 @@ protected function renderView(): void
}

if ($this->dropIcon) {
$this->template->trySet('DropIcon', $this->dropIcon);
$this->template->set('DropIcon', $this->dropIcon);
}

$this->template->trySet('DefaultText', $this->empty);
$this->template->set('DefaultText', $this->empty);

$this->htmlRenderValue();
$this->jsRenderDropdown();
30 changes: 18 additions & 12 deletions src/Form/Control/Upload.php
Original file line number Diff line number Diff line change
@@ -65,8 +65,11 @@ protected function init(): void

$this->cb = JsCallback::addTo($this);

if (!$this->action) {
$this->action = new Button(['icon' => 'upload', 'class.disabled' => $this->disabled || $this->readOnly]);
if ($this->action === null) {
$this->action = new Button([
'icon' => 'upload',
'class.disabled' => $this->disabled || $this->readOnly,
]);
}
}

@@ -123,10 +126,8 @@ public function setFileId($id): void

/**
* Add a JS action to be returned to server on callback.
*
* @param JsExpressionable $action
*/
public function addJsAction($action): void
public function addJsAction(JsExpressionable $action): void
{
$this->jsActions[] = $action;
}
@@ -162,7 +163,10 @@ public function onUpload(\Closure $fx): void
$this->setInput($fileId);
}

$this->addJsAction($fx(...$postFiles));
$jsRes = $fx(...$postFiles);
if ($jsRes !== null) { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/9388
$this->addJsAction($jsRes);
}

if (count($postFiles) > 0 && reset($postFiles)['error'] === 0) {
$this->addJsAction(
@@ -186,7 +190,11 @@ public function onDelete(\Closure $fx): void
if (($_POST['fUploadAction'] ?? null) === self::DELETE_ACTION) {
$this->cb->set(function () use ($fx) {
$fileId = $_POST['fUploadId'];
$this->addJsAction($fx($fileId));

$jsRes = $fx($fileId);
if ($jsRes !== null) { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/9388
$this->addJsAction($jsRes);
}

return new JsBlock($this->jsActions);
});
@@ -195,10 +203,6 @@ public function onDelete(\Closure $fx): void

protected function renderView(): void
{
// need before parent rendering.
if ($this->disabled) {
$this->addClass('disabled');
}
parent::renderView();

if ($this->cb->canTerminate()) {
@@ -222,8 +226,10 @@ protected function renderView(): void
$this->template->dangerouslySetHtml('multiple', 'multiple="multiple"');
}

$this->template->set('placeholderReadonly', $this->disabled ? 'disabled="disabled"' : 'readonly="readonly"');

if ($this->placeholder) {
$this->template->trySet('PlaceHolder', $this->placeholder);
$this->template->set('Placeholder', $this->placeholder);
}

$this->js(true)->atkFileUpload([
14 changes: 7 additions & 7 deletions src/Form/Control/UploadImage.php
Original file line number Diff line number Diff line change
@@ -48,22 +48,22 @@ public function getThumbnail(): View
public function setThumbnailSrc(string $src): void
{
$this->thumbnail->setAttr(['src' => $src]);
$action = $this->thumbnail->js();
$action->attr('src', $src);
$this->addJsAction($action);
$js = $this->thumbnail->js();
$js->attr('src', $src);
$this->addJsAction($js);
}

/**
* Clear the thumbnail src.
*/
public function clearThumbnail(): void
{
$action = $this->thumbnail->js();
$js = $this->thumbnail->js();
if ($this->defaultSrc !== null) {
$action->attr('src', $this->defaultSrc);
$js->attr('src', $this->defaultSrc);
} else {
$action->removeAttr('src');
$js->removeAttr('src');
}
$this->addJsAction($action);
$this->addJsAction($js);
}
}
6 changes: 3 additions & 3 deletions template/form/control/upload.html
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@
<div {$attributes}>
{$BeforeInput}{$AfterBeforeInput}
<div class="ui input" style="position: relative;">
<input type="text" placeholder="{$PlaceHolder}" readonly="readonly">
{Input}<input type="{inputType}text{/}">{/}
<input type="file" accept="{$accept}" {$disabled} {$multiple} style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;">
<input type="text" placeholder="{$Placeholder}" {$placeholderReadonly}>
{$Input}
<input type="file" accept="{$accept}" {$disabled} {$multiple} style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;" tabindex="-1">
</div>
{$AfterInput}{$AfterAfterInput}
</div>
6 changes: 3 additions & 3 deletions template/form/control/upload.pug
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
| <div {$attributes}>
| {$BeforeInput}{$AfterBeforeInput}
| <div class="ui input" style="position: relative;">
| <input type="text" placeholder="{$PlaceHolder}" readonly="readonly">
| {Input}<input type="{inputType}text{/}">{/}
| <input type="file" accept="{$accept}" {$disabled} {$multiple} style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;">
| <input type="text" placeholder="{$Placeholder}" {$placeholderReadonly}>
| {$Input}
| <input type="file" accept="{$accept}" {$disabled} {$multiple} style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;" tabindex="-1">
| </div>
| {$AfterInput}{$AfterAfterInput}
| </div>