leuverink/livewire-property-groups
最新稳定版本:2.0.0
Composer 安装命令:
composer require leuverink/livewire-property-groups
包简介
Livewire Attribute for grouping properties
README 文档
README
This package simplifies property management, validation, and manipulation in Livewire components by allowing you to organize related properties into named groups.
Installation
composer require leuverink/livewire-property-groups
Basic Usage
use Leuverink\PropertyAttribute\Group; use Leuverink\PropertyAttribute\WithGroups; class Form extends Component { use WithGroups; #[Group('a')] public $foo = 1; #[Group('a')] public $bar = 2; #[Group('b')] public $baz = 3; public function submit() { $this->group('a')->validate(); //... } }
Accessing Group Properties
Use the WithGroups trait within your Component or Form object to get access to the group method.
// Get all properties in a group $this->group('a'); // ['foo' => 1, 'bar' => 2] // Get property names $this->group('a')->keys(); // ['foo', 'bar'] // Get property values $this->group('a')->values(); // [1, 2] // Iterate over properties $this->group('a')->each(fn($value, $name) => /* */); // Get all grouped properties, excluding non grouped $this->group(); // Access a group as an array or an object $this->group('a')['foo']; $this->group('a')->foo;
Proxying Livewire Methods
// Reset properties to initial state $this->group('a')->reset(); // Return all properties and reset to initial state $this->group('a')->pull(); // Validate all properties in a group $this->group('a')->validate(); // Works inside a form object $this->userForm->group('a')->validate();
Working with Multiple Groups
// Retrieve properties from multiple groups $this->group(['a', 'b']); // Validate multiple groups $this->group(['a', 'b'])->validate();
Debugging
// dump group properties $this->group('a')->dump(); // dd group properties $this->group('a')->dd(); // dump is chainable $validated = $this->group('a') ->dump() ->validate();
Volt
In class-based components, property groups work like any other Livewire component. When using Volt's functional API you may use property groups like demonstrated below.
use Leuverink\PropertyAttribute\Group; use Leuverink\PropertyAttribute\WithGroups; use function Livewire\Volt\{action, state, uses}; uses([WithGroups::class]); state([ 'foo' => 1, ])->attribute(Group::class, 'a'); state([ 'bar' => 2, 'baz' => 'Lorem', ])->attribute(Group::class, 'b'); $action = action(function() { $groupA = $this->group('a') ->validate() ->values(); $groupB = $this->group('b') ->validate() ->values(); // ... });
Conflicting group method signature
I realize that group is a very generic method name that you might well use inside your own components.
You may change the method signature by providing an alias.
use WithGroups { group as fooBar; }
Development
composer lint # run all linters composer fix # run all fixers composer analyze # run static analysis composer baseline # generate static analysis baseline
License
This package is open-source software licensed under the MIT license.
统计信息
- 总下载量: 367
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-29