|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rawilk\FormComponents\Components\Choice; |
| 6 | + |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Rawilk\FormComponents\Components\BladeComponent; |
| 9 | +use Rawilk\FormComponents\Concerns\HandlesValidationErrors; |
| 10 | + |
| 11 | +class SwitchToggle extends BladeComponent |
| 12 | +{ |
| 13 | + use HandlesValidationErrors; |
| 14 | + |
| 15 | + protected static array $assets = ['alpine']; |
| 16 | + |
| 17 | + private string $labelId; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + public null|string $name = null, |
| 21 | + public null|string $id = null, |
| 22 | + public mixed $value = false, |
| 23 | + public mixed $onValue = true, |
| 24 | + public mixed $offValue = false, |
| 25 | + public null|string $containerClass = null, |
| 26 | + public bool $short = false, |
| 27 | + public null|string $label = null, |
| 28 | + public string $labelPosition = 'right', |
| 29 | + public null|string $offIcon = null, // doesn't apply to short mode |
| 30 | + public null|string $onIcon = null, // doesn't apply to short mode |
| 31 | + public null|string $buttonLabel = 'form-components::messages.switch_button_label', |
| 32 | + public null|string $size = null, |
| 33 | + public bool $disabled = false, |
| 34 | + ) { |
| 35 | + $this->id = $this->id ?? $this->name; |
| 36 | + $this->labelId = $this->id ?? Str::random(8); |
| 37 | + } |
| 38 | + |
| 39 | + public function labelId(): string |
| 40 | + { |
| 41 | + return "{$this->labelId}-label"; |
| 42 | + } |
| 43 | + |
| 44 | + public function buttonClass(): string |
| 45 | + { |
| 46 | + return collect([ |
| 47 | + 'switch-toggle', |
| 48 | + $this->short ? 'switch-toggle-short' : 'switch-toggle-simple', |
| 49 | + $this->size ? "switch-toggle--{$this->size}" : null, |
| 50 | + $this->disabled ? 'disabled' : null, |
| 51 | + ])->filter()->implode(' '); |
| 52 | + } |
| 53 | + |
| 54 | + public function getContainerClass(): string |
| 55 | + { |
| 56 | + return collect([ |
| 57 | + 'flex', |
| 58 | + 'items-center', |
| 59 | + $this->labelPosition === 'left' ? 'justify-between' : null, |
| 60 | + $this->containerClass, |
| 61 | + ])->filter()->implode(' '); |
| 62 | + } |
| 63 | +} |
0 commit comments