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

Fix UploadImage::clearThumbnail default image reset #2027

Merged
merged 23 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/form-control/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// $control->set('a_generated_token');

$img->onDelete(function (string $fileId) use ($img) {
$img->clearThumbnail('./images/default.png');
$img->clearThumbnail();
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

return new JsToast([
'title' => 'Delete successfully',
Expand Down
2 changes: 1 addition & 1 deletion docs/fileupload.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ UploadImage form control inherits all of the Upload properties plus these ones:

The thumbnail view associated with the form control.

.. php:attr:: thumnailRegion
.. php:attr:: thumbnailRegion

The region in input template where to add the thumbnail view, default to AfterAfterInput region.

Expand Down
12 changes: 12 additions & 0 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,18 @@ public function textInContainerShouldMatchRegex(string $selector, string $regex)
}
}

/**
* @Then Element :arg1 attribute :arg2 should contain text :arg3
*/
public function elementAttributeShouldContainText(string $selector, string $attribute, string $text): void
{
$element = $this->findElement(null, $selector);
$attr = $element->getAttribute($attribute);
if (!str_contains($attr, $text)) {
throw new \Exception('Element " . $selector . " attribute "' . $attribute . '" does not contain "' . $text . '"');
}
}

// }}}

/**
Expand Down
32 changes: 16 additions & 16 deletions src/Form/Control/UploadImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class UploadImage extends Upload
*
* @var string
*/
public $thumnailRegion = 'AfterAfterInput';
public $thumbnailRegion = 'AfterAfterInput';

/** @var string The default thumbnail source. */
/** @var string|null The default thumbnail source. */
public $defaultSrc;

protected function init(): void
Expand All @@ -30,24 +30,27 @@ protected function init(): void
$this->accept = ['.jpg', '.jpeg', '.png'];
}

if (!$this->thumbnail) {
$this->add($this->getThumbnail(), $this->thumbnailRegion);
}

public function getThumbnail(): View
{
if ($this->thumbnail === null) {
$this->thumbnail = (new View(['element' => 'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))
->setAttr(['width' => 36, 'height' => 36]);
}

if ($this->defaultSrc) {
$this->thumbnail->setAttr(['src' => $this->defaultSrc]);
if ($this->defaultSrc) {
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
$this->thumbnail->setAttr(['src' => $this->defaultSrc]);
}
}

$this->add($this->thumbnail, $this->thumnailRegion);
return $this->thumbnail;
}

/**
* Set the thumbnail img src value.
*
* @param string $src
*/
public function setThumbnailSrc($src): void
public function setThumbnailSrc(string $src): void
{
$this->thumbnail->setAttr(['src' => $src]);
$action = $this->thumbnail->js();
Expand All @@ -57,15 +60,12 @@ public function setThumbnailSrc($src): void

/**
* Clear the thumbnail src.
* You can also supply a default thumbnail src.
*
* @param string $defaultThumbnail
*/
public function clearThumbnail($defaultThumbnail = null): void
public function clearThumbnail(): void
{
$action = $this->thumbnail->js();
if ($defaultThumbnail !== null) {
$action->attr('src', $defaultThumbnail);
if ($this->defaultSrc !== null) {
$action->attr('src', $this->defaultSrc);
} else {
$action->removeAttr('src');
}
Expand Down
10 changes: 10 additions & 0 deletions tests-behat/upload.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ Feature: Upload

When I select file input "file" with "Žlutý kůň" as "$kůň"
Then Toast display should contain text "(name: $kůň, md5: b047fb155be776f5bbae061c7b08cdf0)"

When I click using selector "xpath(//div.action[.//input[@name='file']]//div.button)"
Then Toast display should contain text "has been removed"

When I select file input "img" with "Foo" as "bar.png"
Then Toast display should contain text "is uploaded"

When I click using selector "xpath(//div.action[.//input[@name='img']]//div.button)"
Then Toast display should contain text "has been removed"
Then Element "xpath(//div.action[.//div//input[@name='img']]//img)" attribute "src" should contain text "default.png"