Last active
December 1, 2020 13:08
-
-
Save Log1x/83513ac4ee10012e391be10688bd9823 to your computer and use it in GitHub Desktop.
Rating component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x-rating /> | |
<x-rating rating="3.4" size="20" type="thumbsUp" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<fa-rating | |
:glyph="{{ $type }}" | |
:item-size="{{ $size }}" | |
active-color="currentColor" | |
:increment="0.1" | |
:rating="{{ $rating }}" | |
v-model="rating"> | |
</fa-rating> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\View\Components; | |
use Roots\Acorn\View\Component; | |
class Rating extends Component | |
{ | |
/** | |
* The rating value. | |
* | |
* @return int | |
*/ | |
public $rating; | |
/** | |
* The rating type. | |
* | |
* @return string | |
*/ | |
public $type; | |
/** | |
* The rating size. | |
* | |
* @return int | |
*/ | |
public $size; | |
/** | |
* Create the component instance. | |
* | |
* @param string $type | |
* @param int $size | |
* @param int $rating | |
* @return void | |
*/ | |
public function __construct($type = 'star', $size = 16, $rating = null) | |
{ | |
$this->type = $type; | |
$this->size = $size; | |
$this->rating = $rating; | |
if (empty($this->$rating)) { | |
$this->rating = get_field('rating') ?? get_sub_field('rating') ?? 1; | |
} | |
} | |
/** | |
* The formatted rating. | |
* | |
* @return string | |
*/ | |
public function rating() | |
{ | |
return $this->format($this->rating); | |
} | |
/** | |
* Format a number to the first decimal. | |
* | |
* @param int $x | |
* @param int $y | |
* @return string | |
*/ | |
protected function format($x, $y = 1) | |
{ | |
if (! is_numeric($x)) { | |
$x = 1; | |
} | |
return number_format( | |
min(max($x, 1), 5), | |
$y | |
); | |
} | |
/** | |
* Get the view / contents that represent the component. | |
* | |
* @return \Illuminate\View\View|string | |
*/ | |
public function render() | |
{ | |
return $this->view('components.rating'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment