Skip to content

Commit

Permalink
dynamic field reactivity macro, Jetstream belongsToMany team checklis…
Browse files Browse the repository at this point in the history
…t, PrettyPrint view field.
  • Loading branch information
tanthammar committed Dec 22, 2023
1 parent 0abf0e4 commit 2d03718
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
9 changes: 9 additions & 0 deletions resources/views/components/pretty-print.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
>
<div class="border dark:border-gray-600 rounded bg-white dark:bg-gray-800 p-6 w-full">
<pre class="w-full overflow-x-auto">{!! print_r(data_get($this, $getStatePath()), true) !!}</pre>
{{-- <pre><code>{!! json_encode(data_get($this, $getStatePath()), JSON_PRETTY_PRINT) !!}</code>}</pre> --}}
</div>
</x-dynamic-component>
2 changes: 2 additions & 0 deletions src/FilamentExtrasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()))]));

Expand Down
44 changes: 36 additions & 8 deletions src/Forms/JetstreamAuthorSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]));
}
}
21 changes: 21 additions & 0 deletions src/Forms/PrettyPrintView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace TantHammar\FilamentExtras\Forms;

use Filament\Forms\Components\Field;

/**
* Consider using JSONEditor field instead.<br>
* <br>
* OBSERVE that this field does not update component state.<br>
* <br>
* Example usage: <br>
* PrettyPrint::make('account')<br>
* ->ignored()<br>
* ->hiddenOn('create')<br>
* ->columnSpan('full'),<br>
*/
class PrettyPrintView extends Field
{
protected string $view = 'filament-extras::components.pretty-print';
}
7 changes: 5 additions & 2 deletions src/Forms/TeamBelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>
* whereas user only can select between OWNED teams
*/
Expand All @@ -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)
Expand Down

0 comments on commit 2d03718

Please sign in to comment.