| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +namespace Rappasoft\LaravelLivewireTables\Views\Traits;  | 
 | 4 | + | 
 | 5 | +use Livewire\Attributes\{Modelable,On,Renderless};  | 
 | 6 | + | 
 | 7 | +trait IsExternalArrayFilter  | 
 | 8 | +{  | 
 | 9 | +    #[Modelable]  | 
 | 10 | +    public $value = [];  | 
 | 11 | + | 
 | 12 | +    public string $filterKey = '';  | 
 | 13 | + | 
 | 14 | +    public string $tableName = '';  | 
 | 15 | + | 
 | 16 | +    public string $tableComponent = '';  | 
 | 17 | + | 
 | 18 | +    protected bool $needsUpdating = false;  | 
 | 19 | + | 
 | 20 | +    protected array $returnValues = [];  | 
 | 21 | + | 
 | 22 | +    public array $selectedItems = [];  | 
 | 23 | + | 
 | 24 | +    public array $selectOptions = [];  | 
 | 25 | + | 
 | 26 | +    #[On('filter-was-set')]  | 
 | 27 | +    #[Renderless]   | 
 | 28 | +    public function setFilterValues($tableName, $filterKey, $value)  | 
 | 29 | +    {  | 
 | 30 | +        if ($tableName == $this->tableName && $filterKey == $this->filterKey)  | 
 | 31 | +        {  | 
 | 32 | +            $this->selectedItems = $value;  | 
 | 33 | +            $this->clearFilter();  | 
 | 34 | +            $this->needsUpdating = false;  | 
 | 35 | + | 
 | 36 | +        }  | 
 | 37 | +    }  | 
 | 38 | + | 
 | 39 | +    protected function clearFilter() {}  | 
 | 40 | + | 
 | 41 | +    #[Renderless]   | 
 | 42 | +    public function updatedSelectedItems($values)  | 
 | 43 | +    {  | 
 | 44 | +        $this->needsUpdating = true;  | 
 | 45 | +    }  | 
 | 46 | + | 
 | 47 | +    protected function enableUpdateDispatch(): void  | 
 | 48 | +    {  | 
 | 49 | +        $this->needsUpdating = true;  | 
 | 50 | +    }  | 
 | 51 | + | 
 | 52 | +    protected function disableUpdateDispatch(): void  | 
 | 53 | +    {  | 
 | 54 | +        $this->needsUpdating = false;  | 
 | 55 | +    }  | 
 | 56 | + | 
 | 57 | +    #[Renderless]   | 
 | 58 | +    protected function sendUpdateDispatch(array $returnValues)  | 
 | 59 | +    {  | 
 | 60 | +        if ($this->needsUpdating)  | 
 | 61 | +        {  | 
 | 62 | +            if (!empty($returnValues))  | 
 | 63 | +            {  | 
 | 64 | +                $this->dispatch('livewireArrayFilterUpdateValues', tableName: $this->tableName, filterKey: $this->filterKey, values: $returnValues)->to($this->tableComponent);   | 
 | 65 | +                $this->value = array_keys($returnValues);  | 
 | 66 | +            }  | 
 | 67 | +            else  | 
 | 68 | +            {  | 
 | 69 | +                $this->value = [];  | 
 | 70 | +            }  | 
 | 71 | +            $this->needsUpdating = false;  | 
 | 72 | + | 
 | 73 | +        }  | 
 | 74 | +    }  | 
 | 75 | + | 
 | 76 | +    public function renderingIsExternalArrayFilter()  | 
 | 77 | +    {  | 
 | 78 | +        $returnValues = [];  | 
 | 79 | + | 
 | 80 | +        if ($this->needsUpdating == true && !empty($this->selectedItems))  | 
 | 81 | +        {  | 
 | 82 | +            foreach ($this->selectedItems as $selectedItem)  | 
 | 83 | +            {  | 
 | 84 | +                $returnValues[$selectedItem] = $this->selectOptions[$selectedItem] ?? 'Unknown';  | 
 | 85 | +            }  | 
 | 86 | +            $this->sendUpdateDispatch($returnValues);  | 
 | 87 | +        }  | 
 | 88 | +    }  | 
 | 89 | + | 
 | 90 | +}  | 
0 commit comments