codicastudio/conditional

一个随机的 Codica Studio 包。

1.0.0 2020-09-25 01:53 UTC

This package is auto-updated.

Last update: 2024-09-25 13:28:26 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 方法评估为真,整个操作将被认为是真的。如果您想执行 AND 操作,请在链中追加 ->useAndOperator()

当前支持的运算符

  • AND
  • OR
  • NOT
  • XOR
  • 和括号

当前支持的比较器

示例

  • 如果用户已选择文件,则显示字段
[
    Image::make('Image'),
    ConditionalContainer::make([ Text::make('caption')->rules('required') ])
                        ->if('image truthy true'),
]
  • 如果选择的形态关系类型为 Image 或 Video,则显示额外字段
[
    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();
                        })
]
  • 如果 Reason 字段为空,则显示内联 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)。请参阅许可证文件以获取更多信息。