diff --git a/resources/views/components/pretty-print.blade.php b/resources/views/components/pretty-print.blade.php new file mode 100644 index 0000000..a3918d9 --- /dev/null +++ b/resources/views/components/pretty-print.blade.php @@ -0,0 +1,9 @@ + +
+
{!! print_r(data_get($this, $getStatePath()), true)  !!}
+ {{--
{!! json_encode(data_get($this, $getStatePath()), JSON_PRETTY_PRINT)   !!}}
--}} +
+
diff --git a/src/FilamentExtrasServiceProvider.php b/src/FilamentExtrasServiceProvider.php index 91bf7f8..c46120a 100644 --- a/src/FilamentExtrasServiceProvider.php +++ b/src/FilamentExtrasServiceProvider.php @@ -107,6 +107,8 @@ protected function registerMacros(): void Components\Field::macro('ruleEach', fn (array | string $rules, bool | Closure $condition = true): static => $this->nestedRecursiveRules($rules, $condition)); + Components\Field::macro('isReactive', fn (bool $live): static => $live ? $this->live() : $this); + Components\TagsInput::macro('ruleEachInOptions', fn (): static => $this->nestedRecursiveRules(['bail', fn ($component): \Illuminate\Validation\Rules\In => Rule::in(array_keys($component->getOptions()))])); Components\CheckboxList::macro('ruleEachInOptions', fn (): static => $this->nestedRecursiveRules(['bail', fn ($component): \Illuminate\Validation\Rules\In => Rule::in(array_keys($component->getOptions()))])); diff --git a/src/Forms/JetstreamAuthorSection.php b/src/Forms/JetstreamAuthorSection.php index 75345c4..4d4dbba 100644 --- a/src/Forms/JetstreamAuthorSection.php +++ b/src/Forms/JetstreamAuthorSection.php @@ -4,22 +4,50 @@ use App\Models\User; use Filament\Forms\Components\Section; +use Filament\Forms\Components\Select; +use Closure; class JetstreamAuthorSection { - public static function make(): Section + public static function make(bool $teamLive = false): Section { return Section::make(__('fields.authors')) ->schema([ - Author::make() - ->lazy() - ->onUpdated(fn ($set, $state) => $set('team', [ - $state ? User::find($state)?->current_team_id : null, - ])) - ->required(), - TeamBelongsTo::make(), + + self::author('team'), + + TeamBelongsTo::make()->isReactive($teamLive), + ])->columns(2) ->collapsible() + ->collapsed() ->visible(user()?->isSupport()); } + + public static function manyTeams(bool $teamLive = false, ?Closure $onUpdatedTeams = null): Section + { + return Section::make(__('fields.authors')) + ->schema([ + + self::author('teams') + ->disabled(! user()?->isSupport()), + + TeamBelongsToMany::make() + ->isReactive($teamLive) + ->onUpdated($onUpdated), + ]) + ->columns(2) + ->collapsible() + ->collapsed(); + } + + protected static function author(string $updatesField): Select + { + return Author::make() + ->lazy() + ->required() + ->onUpdated(fn ($set, $state) => $set($field, [ + $state ? User::find($state)?->current_team_id : null, + ])); + } } diff --git a/src/Forms/PrettyPrintView.php b/src/Forms/PrettyPrintView.php new file mode 100644 index 0000000..84b5433 --- /dev/null +++ b/src/Forms/PrettyPrintView.php @@ -0,0 +1,21 @@ + + *
+ * OBSERVE that this field does not update component state.
+ *
+ * Example usage:
+ * PrettyPrint::make('account')
+ * ->ignored()
+ * ->hiddenOn('create')
+ * ->columnSpan('full'),
+ */ +class PrettyPrintView extends Field +{ + protected string $view = 'filament-extras::components.pretty-print'; +} diff --git a/src/Forms/TeamBelongsToMany.php b/src/Forms/TeamBelongsToMany.php index ec82830..90d0df9 100644 --- a/src/Forms/TeamBelongsToMany.php +++ b/src/Forms/TeamBelongsToMany.php @@ -5,10 +5,13 @@ use App\Models\User; use Filament\Forms\Components\CheckboxList; use Filament\Forms\Components\Select; +use Closure; class TeamBelongsToMany { /** + * Visible to support and user who owns the current team. + * * Support can search among ALL teams a user belongs to
* whereas user only can select between OWNED teams */ @@ -26,8 +29,8 @@ public static function make(): CheckboxList ->relationship('teams', 'name') ->options( fn ($get) => user()?->isSupport() - ? User::find($get('user_id'))?->allTeams()->pluck('name', 'id') ?? collect() - : user()?->ownedTeams()->pluck('name', 'id') ?? collect() + ? User::find($get('user_id'))?->allTeams()->pluck('name', 'id') ?? collect() //support can see all user related teams + : user()?->ownedTeams()->pluck('name', 'id') ?? collect() //current team owner can only see other owned teams ) ->bulkToggleable() ->columns(2)