Skip to content

Commit 6179ca6

Browse files
Rating component (#486)
* Update MaryServiceProvider.php * Create Rating.php * WIP --------- Co-authored-by: Robson Tenório <[email protected]>
1 parent f40d667 commit 6179ca6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/MaryServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Mary\View\Components\ProgressRadial;
5454
use Mary\View\Components\Radio;
5555
use Mary\View\Components\Range;
56+
use Mary\View\Components\Rating;
5657
use Mary\View\Components\Select;
5758
use Mary\View\Components\SelectGroup;
5859
use Mary\View\Components\Signature;
@@ -156,6 +157,7 @@ public function registerComponents()
156157
Blade::component($prefix . 'progress-radial', ProgressRadial::class);
157158
Blade::component($prefix . 'radio', Radio::class);
158159
Blade::component($prefix . 'range', Range::class);
160+
Blade::component($prefix . 'rating', Rating::class);
159161
Blade::component($prefix . 'select', Select::class);
160162
Blade::component($prefix . 'signature', Signature::class);
161163
Blade::component($prefix . 'spotlight', Spotlight::class);

src/View/Components/Rating.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Mary\View\Components;
4+
5+
use Closure;
6+
use Illuminate\Contracts\View\View;
7+
use Illuminate\View\Component;
8+
9+
class Rating extends Component
10+
{
11+
public string $uuid;
12+
13+
public function __construct(
14+
public int $total = 5
15+
) {
16+
$this->uuid = "mary" . md5(serialize($this));
17+
}
18+
19+
public function modelName(): ?string
20+
{
21+
return $this->attributes->whereStartsWith('wire:model')->first();
22+
}
23+
24+
public function size(): ?string
25+
{
26+
return str($this->attributes->get('class'))->match('/(rating-(..))/');
27+
}
28+
29+
public function render(): View|Closure|string
30+
{
31+
return <<<'HTML'
32+
<div class="rating gap-1 {{ $size }}">
33+
@for ($i = 1; $i <= $total; $i++)
34+
<input
35+
type="radio"
36+
name="{{ $modelName() }}"
37+
value="{{ $i }}"
38+
{{ $attributes->whereStartsWith('wire:model') }}
39+
{{ $attributes->class(["mask mask-star-2 bg-yellow-400"]) }}
40+
/>
41+
@endfor
42+
</div>
43+
HTML;
44+
}
45+
}

0 commit comments

Comments
 (0)