diff --git a/CHANGELOG.md b/CHANGELOG.md index f96ef86..19967f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `nova-enum-field` will be documented in this file. +## Unreleased +- Added ability to specify default value for `EnumFilter` and `EnumBooleanFilter` + ## 2.3.0 - 2021-01-25 - Drop support for Laravel `7.x` diff --git a/README.md b/README.md index ae11f63..e6a1c77 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ class Example extends Resource // Or with optional filter name: (new EnumFilter('user_type', UserType::class)) ->name('Type of user'), + + // With optional default value: + (new EnumFilter('user_type', UserType::class)) + ->default(UserType::Administrator), ]; } } @@ -140,9 +144,16 @@ class Example extends Resource new EnumBooleanFilter('user_permissions', UserPermissions::class), - // Or with optional filter name: + // With optional filter name: (new EnumBooleanFilter('user_type', UserType::class)) ->name('Type of user'), + + // With optional default values: + (new EnumBooleanFilter('user_type', UserType::class)) + ->default([ + UserType::Administrator, + UserType::Moderator, + ]), // When filtering a FlaggedEnum, it will default to filtering // by ANY flags, however you may wish to filter by ALL flags: diff --git a/src/EnumBooleanFilter.php b/src/EnumBooleanFilter.php index 1d7f800..84bb702 100644 --- a/src/EnumBooleanFilter.php +++ b/src/EnumBooleanFilter.php @@ -16,6 +16,8 @@ class EnumBooleanFilter extends BooleanFilter protected $class; + protected $default; + protected $flagged; protected $scope = 'any'; @@ -90,4 +92,24 @@ public function options(Request $request) return array_flip($this->class::asSelectArray()); } + + public function default() + { + if (isset(func_get_args()[0])) { + $this->default = collect(is_array(func_get_args()[0]) ? func_get_args()[0] : [func_get_args()[0]]) + ->map(function ($value, $key) { + return is_subclass_of($value, \BenSampo\Enum\Enum::class) ? $value->value : $value; + })->all(); + + return $this; + } + + if (is_null($this->default)) { + return parent::default(); + } + + return collect($this->default)->mapWithKeys(function ($option) { + return [$option => true]; + })->all() + parent::default(); + } } diff --git a/src/EnumFilter.php b/src/EnumFilter.php index d363104..ba97eec 100644 --- a/src/EnumFilter.php +++ b/src/EnumFilter.php @@ -15,6 +15,8 @@ class EnumFilter extends Filter protected $class; + protected $default; + protected $flagged; public function __construct($column, $class) @@ -49,4 +51,19 @@ public function options(Request $request) { return array_flip($this->class::asSelectArray()); } + + public function default() + { + if (isset(func_get_args()[0])) { + $this->default = is_subclass_of(func_get_args()[0], \BenSampo\Enum\Enum::class) ? func_get_args()[0]->value : func_get_args()[0]; + + return $this; + } + + if (is_null($this->default)) { + return parent::default(); + } + + return $this->default; + } } diff --git a/tests/Filters/BooleanFilterTest.php b/tests/Filters/BooleanFilterTest.php index 3bcc989..21f8b6e 100644 --- a/tests/Filters/BooleanFilterTest.php +++ b/tests/Filters/BooleanFilterTest.php @@ -39,4 +39,45 @@ public function it_can_have_a_different_name() $this->assertEquals('Different name', $this->filter->name()); } + + /** @test */ + public function it_accepts_optional_default_values() + { + $this->filter->default(IntegerEnum::Moderator); + + $this->assertEquals([ + IntegerEnum::Administrator => false, + IntegerEnum::Moderator => true, + IntegerEnum::Subscriber => false, + ], $this->filter->jsonSerialize()['currentValue']); + + $this->filter->default(IntegerEnum::Administrator()); + + $this->assertEquals([ + IntegerEnum::Administrator => true, + IntegerEnum::Moderator => false, + IntegerEnum::Subscriber => false, + ], $this->filter->jsonSerialize()['currentValue']); + + $this->filter->default([ + IntegerEnum::Subscriber, + IntegerEnum::Moderator(), + ]); + + $this->assertEquals([ + IntegerEnum::Administrator => false, + IntegerEnum::Moderator => true, + IntegerEnum::Subscriber => true, + ], $this->filter->jsonSerialize()['currentValue']); + } + + /** @test */ + public function it_has_no_default_value_by_default() + { + $this->assertEquals([ + IntegerEnum::Administrator => false, + IntegerEnum::Moderator => false, + IntegerEnum::Subscriber => false, + ], $this->filter->jsonSerialize()['currentValue']); + } } diff --git a/tests/Filters/FilterTest.php b/tests/Filters/FilterTest.php index 1d16ad9..a8b7662 100644 --- a/tests/Filters/FilterTest.php +++ b/tests/Filters/FilterTest.php @@ -39,4 +39,22 @@ public function it_can_have_a_different_name() $this->assertEquals('Different name', $this->filter->name()); } + + /** @test */ + public function it_accepts_an_optional_default_value() + { + $this->filter->default(IntegerEnum::Moderator); + + $this->assertEquals(IntegerEnum::Moderator, $this->filter->jsonSerialize()['currentValue']); + + $this->filter->default(IntegerEnum::Subscriber()); + + $this->assertEquals(IntegerEnum::Subscriber, $this->filter->jsonSerialize()['currentValue']); + } + + /** @test */ + public function it_has_no_default_value_by_default() + { + $this->assertEquals('', $this->filter->jsonSerialize()['currentValue']); + } }