Skip to content

Commit

Permalink
Merge pull request #5 from adrianthedev/feature/add-custom-avatar-in-…
Browse files Browse the repository at this point in the history
…search

feature: add custom avatar ability in search
  • Loading branch information
chaseconey authored May 10, 2020
2 parents c6706c7 + 0f4c9bb commit f7543a7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 66 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ Set the image with avatar-like style (rounded and fixed height + width).
ExternalImage::make('Image')
->avatar(),
```

### Search Avatar

If you want the external image to act as an [Avatar](https://nova.laravel.com/docs/2.0/resources/fields.html#avatar-field) in the search box use `ExternalImageAsAvatar` instead of `ExternalImage`
68 changes: 2 additions & 66 deletions src/ExternalImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,5 @@

namespace Chaseconey\ExternalImage;

use Laravel\Nova\Fields\Field;

class ExternalImage extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'external-image';

/**
*
* Add prefix to the image string provided
*
* @param string $prefix
* @return ExternalImage
*/
public function prefix(string $prefix)
{
return $this->withMeta(['prefix' => $prefix]);
}

/**
* Set the image with avatar-like style
*
* @return $this
*/
public function avatar()
{
return $this->width(42)->height(42)->radius(99);
}

/**
* Set the width of the image
*
* @param int $width
* @return $this
*/
public function width(int $width)
{
return $this->withMeta(['width' => $width]);
}

/**
* Set the height of the image
*
* @param int $height
* @return $this
*/
public function height(int $height)
{
return $this->withMeta(['height' => $height]);
}

/**
* Set the border-radius of the image
*
* @param int $radius
* @return $this
*/
public function radius(int $radius)
{
return $this->withMeta(['borderRadius' => $radius]);
}
}
class ExternalImage extends ExternalImageBase
{}
24 changes: 24 additions & 0 deletions src/ExternalImageAsAvatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Chaseconey\ExternalImage;

use Laravel\Nova\Contracts\Cover;

class ExternalImageAsAvatar extends ExternalImageBase implements Cover
{
/**
* Resolve the thumbnail URL for the field.
*
* @return string|null
*/
public function resolveThumbnailUrl()
{
return $this->value;
}

public function isRounded()
{
return $this->rounded;
}

}
74 changes: 74 additions & 0 deletions src/ExternalImageBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Chaseconey\ExternalImage;

use Laravel\Nova\Fields\Field;

class ExternalImageBase extends Field
{
protected $rounded = false;

/**
* The field's component.
*
* @var string
*/
public $component = 'external-image';

/**
*
* Add prefix to the image string provided
*
* @param string $prefix
* @return ExternalImage
*/
public function prefix(string $prefix)
{
return $this->withMeta(['prefix' => $prefix]);
}

/**
* Set the image with avatar-like style
*
* @return $this
*/
public function avatar()
{
$this->rounded = true;

return $this->width(42)->height(42)->radius(99);
}

/**
* Set the width of the image
*
* @param int $width
* @return $this
*/
public function width(int $width)
{
return $this->withMeta(['width' => $width]);
}

/**
* Set the height of the image
*
* @param int $height
* @return $this
*/
public function height(int $height)
{
return $this->withMeta(['height' => $height]);
}

/**
* Set the border-radius of the image
*
* @param int $radius
* @return $this
*/
public function radius(int $radius)
{
return $this->withMeta(['borderRadius' => $radius]);
}
}

0 comments on commit f7543a7

Please sign in to comment.