kayacekovic / conditional-container
提供了一种简单的方式来在您的 Nova 资源中条件性地显示和隐藏字段。
dev-development
2022-11-14 15:23 UTC
Requires
- php: >=7.1.0
- laravel/nova: ^3.12.0
This package is auto-updated.
Last update: 2024-09-27 19:44:05 UTC
README
提供了一种简单的方式来在您的 Nova 资源中条件性地显示和隐藏字段。
安装
您可以通过 composer 安装此包
composer require digital-creative/conditional-container
用法
基本演示展示了该字段的强大功能
use DigitalCreative\ConditionalContainer\ConditionalContainer; use DigitalCreative\ConditionalContainer\HasConditionalContainer; class ExampleNovaResource extends Resource { use HasConditionalContainer; // Important!! public function fields(Request $request) { return [ Select::make('Option', 'option') ->options([ 1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3', ]), Text::make('Content', 'content')->rules('required'), /** * Only show field Text::make('Field A') if the value of option is equals 1 */ ConditionalContainer::make([ Text::make('Field A') ]) ->if('option = 1'), /** * Equivalent to: if($option === 2 && $content === 'hello world') */ ConditionalContainer::make([ Text::make('Field B') ]) ->if('option = 2 AND content = "hello world"'), /** * Equivalent to: if(($option !== 2 && $content > 10) || $option === 3) */ ConditionalContainer::make([ Text::make('Field C')->rules('required') ]) ->if('(option != 2 AND content > 10) OR option = 3'), /** * Example with Validation and nested ConditionalContainer! * Equivalent to: if($option === 3 || $content === 'demo') */ ConditionalContainer::make([ Text::make('Field D')->rules('required') // Yeah! validation works flawlessly!! ConditionalContainer::make([ Text::make('Field E') ]) ->if('field_d = Nice!') ]) ->if('option = 3 OR content = demo') ]; } }
->if()
方法接受一个格式如下的单个表达式参数
(attribute COMPARATOR value) OPERATOR ...so on
您可以通过将条件包裹在括号中来构建任何复杂的逻辑运算
ConditionalContainer::make(...)->if('first_name = John'); ConditionalContainer::make(...)->if('(first_name = John AND last_name = Doe) OR (first_name = foo AND NOT last_name = bar)'); ConditionalContainer::make(...)->if('first_name = John AND last_name = Doe');
您可以将多个->if()
链接起来,以按关注点分组表达式,例如
ConditionalContainer::make(...) //->useAndOperator() ->if('age > 18 AND gender = male') ->if('A contains "some word"') ->if('B contains "another word"');
默认情况下,对每个->if()
应用的运算将是OR
,因此如果任何if方法评估为true,整个操作将被视为truthy。如果您想执行AND
操作,请将->useAndOperator()
附加到链中
当前支持的运算符
- AND
- OR
- NOT
- XOR
- 以及括号
当前支持的比较器
示例
- 仅在用户选择了文件时显示字段
[ Image::make('Image'), ConditionalContainer::make([ Text::make('caption')->rules('required') ]) ->if('image truthy true'), ]
- 仅在所选的形态关系为图像或视频类型时显示额外字段
[ MorphTo::make('Resource Type', 'fileable')->types([ App\Nova\Image::class, App\Nova\Video::class, App\Nova\File::class, ]), ConditionalContainer::make([ Image::make('thumbnail')->rules('required') ]) ->if(function () { return 'fileable = ' . App\Nova\Image::uriKey(); }) ->if(function () { return 'fileable = ' . App\Nova\Video::uriKey(); }) ]
- 仅在
原因
字段为空时显示内联HTML,否则显示额外字段。
[ Trix::make('Reason'), ConditionalContainer::make([ Text::make('Extra Information')->rules('required') ]) ->if('reason truthy true'), ConditionalContainer::make([ Heading::make('<p class="text-danger">Please write a good reason...</p>')->asHtml() ]) ->if('reason truthy false'), ]
许可协议
MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。