From a968b5d76bf49a558acc78e6c2fd3ba4fc91183d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Bajsarowicz?= Date: Sun, 15 Nov 2026 01:29:17 +0100 Subject: [PATCH] fix: Remove a grid filter from the state when its value is cleared --- CHANGELOG.md | 2 + Grid/Action/FilterAction.php | 3 +- Grid/Filter/Filter.php | 2 +- Grid/State.php | 13 +++++ Test/Unit/Grid/Action/FilterActionTest.php | 55 +++++++++++++++++++++ Test/Unit/Grid/Filter/FilterTest.php | 57 ++++++++++++++++++++++ 6 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 Test/Unit/Grid/Action/FilterActionTest.php create mode 100644 Test/Unit/Grid/Filter/FilterTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 53910c0..b70a40d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Remove a grid filter from the state when its value is cleared instead of keeping the previous value ## [0.8.3] - 25 October 2026 ### Fixed diff --git a/Grid/Action/FilterAction.php b/Grid/Action/FilterAction.php index 63d7448..6d7560b 100644 --- a/Grid/Action/FilterAction.php +++ b/Grid/Action/FilterAction.php @@ -33,11 +33,12 @@ public function execute(GridRepository $gridRepository, array $value): void } $gridFilter = $gridFilters[$filterName]; + $state = $this->stateManager->get($gridRepository->getNamespace()); if ($gridFilter->isEmpty($filterValue)) { + $state->removeFilter($filterName); continue; } - $state = $this->stateManager->get($gridRepository->getNamespace()); $state->setFilter( $filterName, $filterValue, diff --git a/Grid/Filter/Filter.php b/Grid/Filter/Filter.php index 709fa38..d194f65 100644 --- a/Grid/Filter/Filter.php +++ b/Grid/Filter/Filter.php @@ -104,6 +104,6 @@ public function isEmpty(mixed $value): bool return true; } - return $value === null; + return $value === null || $value === '' || $value === []; } } diff --git a/Grid/State.php b/Grid/State.php index 9aa91d1..4948ff9 100644 --- a/Grid/State.php +++ b/Grid/State.php @@ -199,6 +199,19 @@ public function setFilter(string $name, mixed $value, ?string $conditionType = ' $this->saveBookmarkData(['filters' => ['applied' => [$name => $value]]]); } + public function removeFilter(string $name): void + { + $filters = $this->getFilters(); + if (!isset($filters[$name])) { + return; + } + + unset($filters[$name]); + + $this->setFilters($filters); + $this->saveBookmarkData(['filters' => ['applied' => [$name => null]]]); + } + /** * @param mixed[] $filters * diff --git a/Test/Unit/Grid/Action/FilterActionTest.php b/Test/Unit/Grid/Action/FilterActionTest.php new file mode 100644 index 0000000..ac2a6b7 --- /dev/null +++ b/Test/Unit/Grid/Action/FilterActionTest.php @@ -0,0 +1,55 @@ +createMock(State::class); + $state->expects($this->once())->method('removeFilter')->with('status'); + $state->expects($this->never())->method('setFilter'); + + $this->execute($state, ['status' => '']); + } + + public function testFilledFilterIsStored(): void + { + $state = $this->createMock(State::class); + $state->expects($this->never())->method('removeFilter'); + $state->expects($this->once())->method('setFilter')->with('status', '0', 'eq'); + + $this->execute($state, ['status' => '0']); + } + + private function execute(State $state, array $filters): void + { + $filter = $this->createMock(Filter::class); + $filter->method('isEmpty')->willReturnCallback(fn(mixed $value): bool => $value === ''); + $filter->method('getConditionType')->willReturn('eq'); + + $viewModel = $this->createMock(GridViewModel::class); + $viewModel->method('getGridFilters')->willReturn(['status' => $filter]); + + $component = $this->createMock(Component::class); + $component->method('getViewModel')->willReturn($viewModel); + + $repository = $this->createMock(GridRepository::class); + $repository->method('getComponent')->willReturn($component); + $repository->method('getNamespace')->willReturn('ns'); + + $stateManager = $this->createMock(StateManager::class); + $stateManager->method('get')->willReturn($state); + + (new FilterAction($stateManager))->execute($repository, ['filters' => $filters]); + } +} diff --git a/Test/Unit/Grid/Filter/FilterTest.php b/Test/Unit/Grid/Filter/FilterTest.php new file mode 100644 index 0000000..1de54c6 --- /dev/null +++ b/Test/Unit/Grid/Filter/FilterTest.php @@ -0,0 +1,57 @@ +assertTrue($this->getFilter($conditionType)->isEmpty($value)); + } + + public static function emptyValues(): array + { + return [ + 'null' => ['eq', null], + 'empty string' => ['eq', ''], + 'empty array' => ['in', []], + 'empty from_to' => ['from_to', ['from' => '', 'to' => '']], + ]; + } + + #[DataProvider('nonEmptyValues')] + public function testIsEmptyForRealValues(string $conditionType, mixed $value): void + { + $this->assertFalse($this->getFilter($conditionType)->isEmpty($value)); + } + + public static function nonEmptyValues(): array + { + return [ + 'string zero' => ['eq', '0'], + 'int zero' => ['eq', 0], + 'text' => ['like', 'foo'], + 'selection' => ['in', ['1', '2']], + 'selection with zero' => ['in', ['0']], + 'from only' => ['from_to', ['from' => '5', 'to' => '']], + ]; + } + + private function getFilter(string $conditionType): Filter + { + return new Filter( + $this->createMock(LayoutInterface::class), + $this->createMock(FieldFactory::class), + 'Label', + 'code', + $conditionType, + ); + } +}