mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
refactor again
This commit is contained in:
@@ -74,20 +74,25 @@ trait HasCellState
|
||||
return $this->evaluate($this->defaultState);
|
||||
}
|
||||
|
||||
public function getState(bool $isCached = true): mixed
|
||||
public function getCachedState(): mixed
|
||||
{
|
||||
$record = $this->getRecord();
|
||||
|
||||
if (! $record) {
|
||||
return null;
|
||||
return $this->getState();
|
||||
}
|
||||
|
||||
$recordKey = (string) $record->getKey();
|
||||
|
||||
if ($isCached && array_key_exists($recordKey, $this->cachedState)) {
|
||||
if (array_key_exists($recordKey, $this->cachedState)) {
|
||||
return $this->cachedState[$recordKey];
|
||||
}
|
||||
|
||||
return $this->cachedState[$recordKey] = $this->getState();
|
||||
}
|
||||
|
||||
public function getState(): mixed
|
||||
{
|
||||
$state = ($this->getStateUsing !== null) ?
|
||||
$this->evaluate($this->getStateUsing) :
|
||||
$this->getStateFromRecord();
|
||||
@@ -103,11 +108,7 @@ trait HasCellState
|
||||
$state = $this->getDefaultState();
|
||||
}
|
||||
|
||||
if (! $isCached) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
return $this->cachedState[$recordKey] = $state;
|
||||
return $state;
|
||||
}
|
||||
|
||||
public function getStateFromRecord(): mixed
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
@php
|
||||
$isDisabled = $isDisabled();
|
||||
$state = (bool) $getState();
|
||||
$state = (bool) $getCachedState();
|
||||
@endphp
|
||||
|
||||
<div
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
@php
|
||||
$canWrap = $canWrap();
|
||||
|
||||
$arrayState = $getState();
|
||||
$arrayState = $getCachedState();
|
||||
|
||||
if ($arrayState instanceof \Illuminate\Support\Collection) {
|
||||
$arrayState = $arrayState->all();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
@php
|
||||
use Filament\Tables\Columns\IconColumn\IconColumnSize;
|
||||
|
||||
$arrayState = $getState();
|
||||
$arrayState = $getCachedState();
|
||||
|
||||
if ($arrayState instanceof \Illuminate\Support\Collection) {
|
||||
$arrayState = $arrayState->all();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
@php
|
||||
$state = $getState();
|
||||
$state = $getCachedState();
|
||||
|
||||
if ($state instanceof \Illuminate\Support\Collection) {
|
||||
$state = $state->all();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
$canSelectPlaceholder = $canSelectPlaceholder();
|
||||
$isDisabled = $isDisabled();
|
||||
|
||||
$state = $getState();
|
||||
$state = $getCachedState();
|
||||
if ($state instanceof \BackedEnum) {
|
||||
$state = $state->value;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
$alignment = filled($alignment) ? (Alignment::tryFrom($alignment) ?? $alignment) : null;
|
||||
}
|
||||
|
||||
$arrayState = $getState();
|
||||
$arrayState = $getCachedState();
|
||||
|
||||
if ($arrayState instanceof \Illuminate\Support\Collection) {
|
||||
$arrayState = $arrayState->all();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
use Filament\Support\Enums\Alignment;
|
||||
|
||||
$isDisabled = $isDisabled();
|
||||
$state = $getState();
|
||||
$state = $getCachedState();
|
||||
$mask = $getMask();
|
||||
|
||||
$alignment = $getAlignment() ?? Alignment::Start;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
@php
|
||||
$isDisabled = $isDisabled();
|
||||
$state = $getState();
|
||||
$state = $getCachedState();
|
||||
@endphp
|
||||
|
||||
<div
|
||||
|
||||
@@ -80,7 +80,7 @@ class Column extends ViewComponent
|
||||
'livewire' => [$this->getLivewire()],
|
||||
'record' => [$this->getRecord()],
|
||||
'rowLoop' => [$this->getRowLoop()],
|
||||
'state' => [$this->getState()],
|
||||
'state' => [$this->getCachedState()],
|
||||
'table' => [$this->getTable()],
|
||||
default => parent::resolveDefaultClosureDependencyForEvaluationByName($parameterName),
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ trait CanBeCopied
|
||||
return true;
|
||||
}
|
||||
|
||||
$state = $this->getState();
|
||||
$state = $this->getCachedState();
|
||||
|
||||
if (! is_array($state)) {
|
||||
return $this->isCopyable($state);
|
||||
|
||||
@@ -180,7 +180,7 @@ class TestsColumns
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
$state = $column->getState(isCached: false);
|
||||
$state = $column->getState();
|
||||
|
||||
if (is_array($state)) {
|
||||
$state = json_encode($state);
|
||||
@@ -216,7 +216,7 @@ class TestsColumns
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
$state = $column->getState(isCached: false);
|
||||
$state = $column->getState();
|
||||
|
||||
if (is_array($state)) {
|
||||
$state = json_encode($state);
|
||||
@@ -254,7 +254,7 @@ class TestsColumns
|
||||
|
||||
Assert::assertEquals(
|
||||
$value,
|
||||
$column->formatState($column->getState(isCached: false)),
|
||||
$column->formatState($column->getState()),
|
||||
message: "Failed asserting that a table column with name [{$name}] has a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
@@ -280,7 +280,7 @@ class TestsColumns
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertFalse(
|
||||
$column->formatState($column->getState(isCached: false)) == $value,
|
||||
$column->formatState($column->getState()) == $value,
|
||||
message: "Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user