digital-creative / 条件容器
提供了一种简单的方法,在您的 Nova 资源中条件性地显示和隐藏字段。
1.4.3
2022-05-07 14:01 UTC
Requires
- php: >=7.1.0
- laravel/nova: ^3.12.0
This package is auto-updated.
Last update: 2024-09-07 18:42:04 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,整个操作将被视为真值,如果您想执行 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)。请参阅许可证文件获取更多信息。