|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rawilk\FormComponents\Components\Files; |
| 6 | + |
| 7 | +use Rawilk\FormComponents\Components\BladeComponent; |
| 8 | +use Rawilk\FormComponents\Concerns\AcceptsFiles; |
| 9 | +use Rawilk\FormComponents\Concerns\HandlesValidationErrors; |
| 10 | + |
| 11 | +class FilePond extends BladeComponent |
| 12 | +{ |
| 13 | + use HandlesValidationErrors; |
| 14 | + use AcceptsFiles; |
| 15 | + |
| 16 | + protected static array $assets = ['alpine', 'filepond']; |
| 17 | + |
| 18 | + public bool $multiple; |
| 19 | + public bool $allowDrop; |
| 20 | + public bool $disabled; |
| 21 | + public array $options; |
| 22 | + |
| 23 | + /** @var string|null */ |
| 24 | + public $name; |
| 25 | + |
| 26 | + /** @var int|null */ |
| 27 | + public $maxFiles; |
| 28 | + |
| 29 | + /** @var string|null */ |
| 30 | + public $description; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + bool $multiple = false, |
| 34 | + bool $allowDrop = true, |
| 35 | + string $name = null, |
| 36 | + array $options = [], |
| 37 | + bool $disabled = false, |
| 38 | + int $maxFiles = null, |
| 39 | + string $type = null, |
| 40 | + string $description = null |
| 41 | + ) { |
| 42 | + $this->multiple = $multiple; |
| 43 | + $this->allowDrop = $allowDrop; |
| 44 | + $this->name = $name; |
| 45 | + $this->disabled = $disabled; |
| 46 | + $this->maxFiles = $maxFiles; |
| 47 | + $this->type = $type; |
| 48 | + $this->options = $options; |
| 49 | + $this->description = $description; |
| 50 | + } |
| 51 | + |
| 52 | + public function options(): array |
| 53 | + { |
| 54 | + $label = array_filter([ |
| 55 | + '<span class="filepond--label-action">Upload a file</span> or drag and drop', |
| 56 | + $this->description, |
| 57 | + ]); |
| 58 | + |
| 59 | + if (isset($label[1])) { |
| 60 | + $label[1] = '<span class="fc-filepond--sub-desc">' . $label[1] . '</span>'; |
| 61 | + } |
| 62 | + |
| 63 | + $defaultOptions = [ |
| 64 | + 'allowMultiple' => $this->multiple, |
| 65 | + 'allowDrop' => $this->allowDrop, |
| 66 | + 'disabled' => $this->disabled, |
| 67 | + ] + array_filter([ |
| 68 | + 'maxFiles' => $this->multiple && $this->maxFiles ? $this->maxFiles : null, |
| 69 | + 'name' => $this->name, |
| 70 | + 'labelIdle' => '<span class="fc-filepond--desc">' . implode('<br>', $label) . '</span>', |
| 71 | + ]); |
| 72 | + |
| 73 | + return array_merge($defaultOptions, $this->options); |
| 74 | + } |
| 75 | + |
| 76 | + public function jsonOptions(): string |
| 77 | + { |
| 78 | + if (empty($this->options())) { |
| 79 | + return ''; |
| 80 | + } |
| 81 | + |
| 82 | + return '...' . json_encode((object) $this->options()) . ','; |
| 83 | + } |
| 84 | +} |
0 commit comments