diff --git a/resources/lang/en/tabs.php b/resources/lang/en/tabs.php deleted file mode 100644 index 288f57b82e..0000000000 --- a/resources/lang/en/tabs.php +++ /dev/null @@ -1,7 +0,0 @@ - 'Errors found. Please check all form tabs for errors and try again.', - -]; diff --git a/resources/views/components/button.blade.php b/resources/views/components/button.blade.php index 6fa263ac97..a40f37b6f6 100644 --- a/resources/views/components/button.blade.php +++ b/resources/views/components/button.blade.php @@ -1,5 +1,5 @@ @props([ -'type' => 'submit', + 'type' => 'submit', ]) diff --git a/resources/views/components/tabs.blade.php b/resources/views/components/tabs.blade.php index 2b1cb7ac8b..85d6775c6e 100644 --- a/resources/views/components/tabs.blade.php +++ b/resources/views/components/tabs.blade.php @@ -1,16 +1,16 @@ @props([ + 'defaultTabId', 'label', - 'tab', ])
-
- {{ $tablist }} +
+ {{ $tabList }}
{{ $slot }} diff --git a/resources/views/fields/avatar.blade.php b/resources/views/fields/avatar.blade.php index 58a4186c1a..3276c2e1b3 100644 --- a/resources/views/fields/avatar.blade.php +++ b/resources/views/fields/avatar.blade.php @@ -42,7 +42,7 @@ {{ $field->nameAttribute }}="{{ $field->name }}" class="sr-only" id="{{ $field->id }}" - {{ Filament\format_attributes($field->attributes) }} + {{ Filament\format_attributes($field->extraAttributes) }} />
diff --git a/resources/views/fields/checkbox.blade.php b/resources/views/fields/checkbox.blade.php index db73dcf522..275fe7d15d 100644 --- a/resources/views/fields/checkbox.blade.php +++ b/resources/views/fields/checkbox.blade.php @@ -1,7 +1,7 @@
diff --git a/resources/views/fields/file.blade.php b/resources/views/fields/file.blade.php index 4f3a85f6e8..65b37fdf56 100644 --- a/resources/views/fields/file.blade.php +++ b/resources/views/fields/file.blade.php @@ -13,7 +13,7 @@ errorKey" :for="$field->id" - :help="__($field->help)" + :help-message="__($field->helpMessage)" :hint="__($field->hint)" :label="__($field->label)" :required="$field->required" @@ -155,7 +155,7 @@ toolbar="trix-toolbar-{{ $field->id }}" x-ref="trix" class="block w-full rounded shadow-sm border-gray-400 focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50 bg-white border-gray-300 prose max-w-none" - {{ Filament\format_attributes($field->attributes) }} + {{ Filament\format_attributes($field->extraAttributes) }} /> @else
diff --git a/resources/views/fields/select-sortable.blade.php b/resources/views/fields/select-sortable.blade.php index 398221a277..78f5134be5 100644 --- a/resources/views/fields/select-sortable.blade.php +++ b/resources/views/fields/select-sortable.blade.php @@ -5,7 +5,7 @@ diff --git a/resources/views/fields/select.blade.php b/resources/views/fields/select.blade.php index 302dba327f..258f97d35c 100644 --- a/resources/views/fields/select.blade.php +++ b/resources/views/fields/select.blade.php @@ -1,7 +1,7 @@ - + + @foreach (array_keys($field->tabs) as $label) - + {{ __($label) }} @endforeach @foreach($field->tabs as $label => $fields) - - + + @endforeach - -@if (count($field->tabs) > 1 && $errors->any()) - -@endif diff --git a/resources/views/fields/text.blade.php b/resources/views/fields/text.blade.php index 91a2dc81d4..b072cd24e2 100644 --- a/resources/views/fields/text.blade.php +++ b/resources/views/fields/text.blade.php @@ -1,7 +1,7 @@ kebab() + ->replace('-', ' ') + ->title(); + } + + public function reset(...$properties) + { + parent::reset(...$properties); + + $defaults = $this->getPropertyDefaults(); + + if (count($properties) && is_array($properties[0])) $properties = $properties[0]; + + if (empty($properties)) $properties = array_keys($defaults); + + $propertiesToFill = collect($properties) + ->filter(fn ($property) => in_array($property, $defaults)) + ->mapWithKeys(fn ($property) => [$property => $defaults[$property]]) + ->toArray(); + + $this->fill($propertiesToFill); + } + protected function callFormHooks($event) { return $this->getForm()->callHook($this, $event); } - protected function fillWithFormDefaults() - { - $this->fill($this->getPropertyDefaults()); - } - - protected function getPropertyDefaults() - { - return $this->getForm()->getDefaults(); - } - - protected function getFields() - { - $fields = static::$resource ? static::$resource::fields() : []; - - if (method_exists($this, 'fields')) return array_merge($fields, $this->fields()); - - return $fields; - } - public function getForm() { $record = null; @@ -52,16 +78,23 @@ abstract class Action extends Component ); } - public static function getModel() + protected function getFields() { - $resource = static::getResource(); + $fields = static::$resource ? static::$resource::fields() : []; - return $resource::$model; + if (method_exists($this, 'fields')) return array_merge($fields, $this->fields()); + + return $fields; } - public static function getResource() + protected function fillWithFormDefaults() { - return static::$resource; + $this->fill($this->getPropertyDefaults()); + } + + protected function getPropertyDefaults() + { + return $this->getForm()->getDefaults(); } protected function getRules() @@ -77,16 +110,6 @@ abstract class Action extends Component return $rules; } - protected static function getTitle() - { - if (static::$title) return static::$title; - - return (string) Str::of(class_basename(static::class)) - ->kebab() - ->replace('-', ' ') - ->title(); - } - protected function getValidationAttributes() { $attributes = $this->getForm()->getValidationAttributes(); @@ -97,27 +120,4 @@ abstract class Action extends Component return $attributes; } - - public function reset(...$properties) - { - parent::reset(...$properties); - - $defaults = $this->getPropertyDefaults(); - - if (count($properties) && is_array($properties[0])) $properties = $properties[0]; - - if (empty($properties)) $properties = array_keys($defaults); - - $propertiesToFill = collect($properties) - ->filter(fn($property) => in_array($property, $defaults)) - ->mapWithKeys(fn($property) => [$property => $defaults[$property]]) - ->toArray(); - - $this->fill($propertiesToFill); - } - - public static function route($uri, $name) - { - return new ResourceRoute(static::class, $uri, $name); - } } diff --git a/src/Actions/CreateAction.php b/src/Actions/CreateAction.php index 163c9b0796..5aa007bac8 100644 --- a/src/Actions/CreateAction.php +++ b/src/Actions/CreateAction.php @@ -3,7 +3,6 @@ namespace Filament\Actions; use Filament\Action; -use Filament\Traits\WithNotifications; abstract class CreateAction extends Action { diff --git a/src/Actions/EditAction.php b/src/Actions/EditAction.php index aa1f7f6dbd..4cbe171542 100644 --- a/src/Actions/EditAction.php +++ b/src/Actions/EditAction.php @@ -3,8 +3,6 @@ namespace Filament\Actions; use Filament\Action; -use Filament\Traits\WithNotifications; -use Filament\View\Components\Form; abstract class EditAction extends Action { diff --git a/src/Fields/Avatar.php b/src/Fields/Avatar.php index 80bd542a15..3986db55b4 100644 --- a/src/Fields/Avatar.php +++ b/src/Fields/Avatar.php @@ -14,6 +14,11 @@ class Avatar extends InputField public $user; + public static function make($name) + { + return static::make($name)->image(); + } + public function avatar($avatar) { $this->avatar = $avatar; @@ -28,11 +33,6 @@ class Avatar extends InputField return $this; } - public static function make($name) - { - return static::make($name)->image(); - } - public function size($size) { $this->size = $size; diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 5a89b1da6c..a900fb28fd 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -10,25 +10,13 @@ class Field { use Tappable; - public $attributes = []; - - protected $context; - public $fields = []; - public $help; - public $hidden = false; - public $hint; - public $hooks = []; - public $id; - - public $label; - - public $name; + protected $context; protected $pendingExcludedContextModifications = []; @@ -38,18 +26,6 @@ class Field protected $view; - public function __construct($name = null) - { - if ($name !== null) $this->name($name); - } - - public function attributes($attributes) - { - $this->attributes = $attributes; - - return $this; - } - public function context($context) { $this->context = $context; @@ -63,7 +39,7 @@ class Field $this->pendingIncludedContextModifications = []; collect($this->pendingExcludedContextModifications) - ->filter(fn($callbacks, $context) => $context !== $this->context) + ->filter(fn ($callbacks, $context) => $context !== $this->context) ->each(function ($callbacks) { foreach ($callbacks as $callback) { $callback($this); @@ -89,7 +65,7 @@ class Field if (! $callback) { $this->hidden(); - $callback = fn($field) => $field->visible(); + $callback = fn ($field) => $field->visible(); } if (! $this->context) { @@ -111,6 +87,13 @@ class Field return $this; } + public function hidden() + { + $this->hidden = true; + + return $this; + } + public function fields($fields) { $this->fields = $fields; @@ -124,7 +107,7 @@ class Field $defaults = []; - if ($this->name !== null && property_exists($this, 'default')) { + if (property_exists($this, 'name') && property_exists($this, 'default')) { $defaults[$this->name] = $this->default; } @@ -176,7 +159,7 @@ class Field $attributes = []; - if ($this->name !== null && $this->label !== null) { + if (property_exists($this, 'name') && property_exists($this, 'label')) { $label = Str::of($this->label)->lower(); $attributes[$this->name] = $label; @@ -187,62 +170,6 @@ class Field return $attributes; } - public function help($help) - { - $this->help = $help; - - return $this; - } - - public function hidden() - { - $this->hidden = true; - - return $this; - } - - public function hint($hint) - { - $this->hint = $hint; - - return $this; - } - - public function id($id) - { - $this->id = $id; - - return $this; - } - - public function label($label) - { - $this->label = $label; - - return $this; - } - - public function name($name) - { - $this->name = $name; - - $this->id( - (string) Str::of($this->name) - ->replace('.', '-') - ->slug(), - ); - - $this->label( - (string) Str::of($this->name) - ->afterLast('.') - ->kebab() - ->replace(['-', '_'], ' ') - ->ucfirst(), - ); - - return $this; - } - public function only($contexts, $callback = null) { if (! is_array($contexts)) $contexts = [$contexts]; @@ -250,7 +177,7 @@ class Field if (! $callback) { $this->hidden(); - $callback = fn($field) => $field->visible(); + $callback = fn ($field) => $field->visible(); } if (! $this->context) { diff --git a/src/Fields/Fieldset.php b/src/Fields/Fieldset.php index 766977f264..f06c8e84c5 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -2,8 +2,12 @@ namespace Filament\Fields; +use Filament\Traits\FieldConcerns; + class Fieldset extends Field { + use FieldConcerns\CanHaveLabel; + public $columns = 1; public static function make($label = null) diff --git a/src/Fields/InputField.php b/src/Fields/InputField.php index 53d8bc7db7..902ced49df 100644 --- a/src/Fields/InputField.php +++ b/src/Fields/InputField.php @@ -2,53 +2,58 @@ namespace Filament\Fields; +use Filament\Traits\FieldConcerns; use Illuminate\Support\Str; class InputField extends Field { + use FieldConcerns\CanHaveId; + use FieldConcerns\CanHaveLabel; + public $default = ''; public $errorKey; + public $extraAttributes = []; + + public $helpMessage; + + public $hint; + + public $name; + public $nameAttribute = 'wire:model.defer'; public $required = false; public $rules = []; - public function addRules($rules) + public function __construct($name) { - foreach ($rules as $field => $conditionsToAdd) { - if (! is_array($conditionsToAdd)) $conditionsToAdd = explode('|', $conditionsToAdd); - - $this->rules[$field] = collect($this->rules[$field] ?? []) - ->filter(function ($originalCondition) use ($conditionsToAdd) { - if (! is_string($originalCondition)) return true; - - $conditionsToAdd = collect($conditionsToAdd); - - if ($conditionsToAdd->contains($originalCondition)) return false; - - if (! Str::of($originalCondition)->contains(':')) return true; - - $originalConditionType = (string) Str::of($originalCondition)->before(':'); - - return ! $conditionsToAdd->contains(function ($conditionToAdd) use ($originalConditionType) { - return $originalConditionType === (string) Str::of($conditionToAdd)->before(':'); - }); - }) - ->push(...$conditionsToAdd) - ->toArray(); - } - - return $this; + $this->name($name); } - public function default($default) + public function name($name) { - $this->default = $default; + $this->name = $name; - return $this; + $this->errorKey($this->name); + + $this->id( + (string) Str::of($this->name) + ->replace('.', '-') + ->slug(), + ); + + $this->label( + (string) Str::of($this->name) + ->afterLast('.') + ->kebab() + ->replace(['-', '_'], ' ') + ->ucfirst(), + ); + + $this->rules(['nullable']); } public function errorKey($errorKey) @@ -58,17 +63,44 @@ class InputField extends Field return $this; } + public function rules($conditions) + { + $this->rules = [$this->name => $conditions]; + + return $this; + } + public static function make($name) { return new static($name); } - public function name($name) + public function default($default) { - parent::name($name); + $this->default = $default; - $this->errorKey($this->name); - $this->rules(['nullable']); + return $this; + } + + public function extraAttributes($attributes) + { + $this->extraAttributes = $attributes; + + return $this; + } + + public function helpMessage($message) + { + $this->helpMessage = $message; + + return $this; + } + + public function hint($hint) + { + $this->hint = $hint; + + return $this; } public function nameAttribute($nameAttribute) @@ -121,6 +153,34 @@ class InputField extends Field return $this; } + public function addRules($rules) + { + foreach ($rules as $field => $conditionsToAdd) { + if (! is_array($conditionsToAdd)) $conditionsToAdd = explode('|', $conditionsToAdd); + + $this->rules[$field] = collect($this->rules[$field] ?? []) + ->filter(function ($originalCondition) use ($conditionsToAdd) { + if (! is_string($originalCondition)) return true; + + $conditionsToAdd = collect($conditionsToAdd); + + if ($conditionsToAdd->contains($originalCondition)) return false; + + if (! Str::of($originalCondition)->contains(':')) return true; + + $originalConditionType = (string) Str::of($originalCondition)->before(':'); + + return ! $conditionsToAdd->contains(function ($conditionToAdd) use ($originalConditionType) { + return $originalConditionType === (string) Str::of($conditionToAdd)->before(':'); + }); + }) + ->push(...$conditionsToAdd) + ->toArray(); + } + + return $this; + } + public function required() { $this->required = true; @@ -131,13 +191,6 @@ class InputField extends Field return $this; } - public function rules($conditions) - { - $this->rules = [$this->name => $conditions]; - - return $this; - } - public function requiredWith($field) { $this->required = false; diff --git a/src/Fields/RichEditor.php b/src/Fields/RichEditor.php index 74a7c39d14..5a8d60cbf9 100644 --- a/src/Fields/RichEditor.php +++ b/src/Fields/RichEditor.php @@ -34,7 +34,7 @@ class RichEditor extends InputField $this->toolbarButtons = array_merge($this->toolbarButtons, $buttonsToDisable); $this->toolbarButtons = collect($this->toolbarButtons) - ->filter(fn($button) => ! in_array($button, $buttonsToDisable)) + ->filter(fn ($button) => ! in_array($button, $buttonsToDisable)) ->toArray(); return $this; diff --git a/src/Fields/Tab.php b/src/Fields/Tab.php new file mode 100644 index 0000000000..b6d76ad127 --- /dev/null +++ b/src/Fields/Tab.php @@ -0,0 +1,19 @@ +label($label) + ->id((string) Str::of($label)->slug()); + } +} diff --git a/src/Fields/Tabs.php b/src/Fields/Tabs.php index e14f395310..47aef86b92 100644 --- a/src/Fields/Tabs.php +++ b/src/Fields/Tabs.php @@ -2,25 +2,26 @@ namespace Filament\Fields; +use Filament\Traits\FieldConcerns; +use Illuminate\Support\Str; + class Tabs extends Field { - public $label; + use FieldConcerns\CanHaveId; + use FieldConcerns\CanHaveLabel; - public $tabs = []; - - public function __construct($label) + public static function make($label = null) { - $this->label = $label; + $tabs = (new static())->label($label); + + if ($label) $tabs = $tabs->id((string) Str::of($label)->slug()); + + return $tabs; } - public static function make(string $label) + public function tabs($tabs) { - return new static($label); - } - - public function tab(string $label, array $fields) - { - $this->tabs[$label] = $fields; + $this->fields($tabs); return $this; } diff --git a/src/Fields/Text.php b/src/Fields/Text.php index fc430a0344..390a047d1d 100644 --- a/src/Fields/Text.php +++ b/src/Fields/Text.php @@ -25,17 +25,17 @@ class Text extends InputField return $this; } - public function password() - { - $this->type('password'); - - return $this; - } - public function type($type) { $this->type = $type; return $this; } + + public function password() + { + $this->type('password'); + + return $this; + } } diff --git a/src/FilamentManager.php b/src/FilamentManager.php index a804a3719a..092733a8e6 100644 --- a/src/FilamentManager.php +++ b/src/FilamentManager.php @@ -13,34 +13,6 @@ class FilamentManager public $styles; - protected function getAsset($key) - { - $manifest = json_decode(file_get_contents($this->distPath('mix-manifest.json')), true); - - if (! isset($manifest[$key])) { - return; - } - - return $manifest[$key]; - } - - protected function getPublicAsset($key) - { - $manifestFile = public_path('vendor/filament/mix-manifest.json'); - - if (! file_exists($manifestFile)) { - return; - } - - $manifest = json_decode(file_get_contents($manifestFile), true); - - if (! isset($manifest[$key])) { - return; - } - - return $manifest[$key]; - } - public function scripts() { if ($this->scripts) return $this->scripts; @@ -68,6 +40,44 @@ class FilamentManager '); } + protected function getAsset($key) + { + $manifest = json_decode(file_get_contents($this->distPath('mix-manifest.json')), true); + + if (! isset($manifest[$key])) { + return; + } + + return $manifest[$key]; + } + + public function distPath($path = '') + { + return $this->basePath('dist/' . ltrim($path, '/')); + } + + public function basePath($path = '') + { + return __DIR__ . '/../' . ltrim($path, '/'); + } + + protected function getPublicAsset($key) + { + $manifestFile = public_path('vendor/filament/mix-manifest.json'); + + if (! file_exists($manifestFile)) { + return; + } + + $manifest = json_decode(file_get_contents($manifestFile), true); + + if (! isset($manifest[$key])) { + return; + } + + return $manifest[$key]; + } + public function storage() { if ($this->storage) return $this->storage; @@ -108,14 +118,4 @@ class FilamentManager "); } - - public function basePath($path = '') - { - return __DIR__ . '/../' . ltrim($path, '/'); - } - - public function distPath($path = '') - { - return $this->basePath('dist/' . ltrim($path, '/')); - } } diff --git a/src/FilamentServiceProvider.php b/src/FilamentServiceProvider.php index 58ca72482e..fdf1038907 100644 --- a/src/FilamentServiceProvider.php +++ b/src/FilamentServiceProvider.php @@ -4,9 +4,13 @@ namespace Filament; use Filament\Commands\MakeUserCommand; use Filament\Http\Middleware\AuthorizeResourceRoute; +use Filament\Models\FilamentUser; use Filament\Providers\RouteServiceProvider; use Filament\Providers\ServiceProvider; use Filament\Traits\CanRegisterLivewireComponentDirectories; +use Filament\View\Components\Avatar; +use Filament\View\Components\Image; +use Filament\View\Components\Nav; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Blade; @@ -46,7 +50,7 @@ class FilamentServiceProvider extends ServiceProvider $this->app['config']->set('auth.providers.filament_users', [ 'driver' => 'eloquent', - 'model' => \Filament\Models\FilamentUser::class, + 'model' => FilamentUser::class, ]); } @@ -72,9 +76,9 @@ class FilamentServiceProvider extends ServiceProvider protected function bootLoaders() { $this->loadViewComponentsAs('filament', [ - 'avatar' => \Filament\View\Components\Avatar::class, - 'image' => \Filament\View\Components\Image::class, - 'nav' => \Filament\View\Components\Nav::class, + 'avatar' => Avatar::class, + 'image' => Image::class, + 'nav' => Nav::class, ]); $this->loadViewsFrom(__DIR__ . '/../resources/views', 'filament'); diff --git a/src/Http/Livewire/Auth/Login.php b/src/Http/Livewire/Auth/Login.php index a58d0e4118..0f724f07c4 100644 --- a/src/Http/Livewire/Auth/Login.php +++ b/src/Http/Livewire/Auth/Login.php @@ -6,7 +6,6 @@ use DanHarrin\LivewireRateLimiting\WithRateLimiting; use Filament\Action; use Filament\Fields; use Illuminate\Support\Facades\Auth; -use Livewire\Component; class Login extends Action { diff --git a/src/Http/Livewire/Auth/RequestPassword.php b/src/Http/Livewire/Auth/RequestPassword.php index 8005d38b23..8161080396 100644 --- a/src/Http/Livewire/Auth/RequestPassword.php +++ b/src/Http/Livewire/Auth/RequestPassword.php @@ -7,7 +7,6 @@ use Filament\Action; use Filament\Fields; use Filament\Traits\WithNotifications; use Illuminate\Support\Facades\Password; -use Livewire\Component; class RequestPassword extends Action { diff --git a/src/Http/Livewire/Auth/ResetPassword.php b/src/Http/Livewire/Auth/ResetPassword.php index b844c1be8d..d034b04233 100644 --- a/src/Http/Livewire/Auth/ResetPassword.php +++ b/src/Http/Livewire/Auth/ResetPassword.php @@ -8,7 +8,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Password; -use Livewire\Component; class ResetPassword extends Action { diff --git a/src/Http/Livewire/EditAccount.php b/src/Http/Livewire/EditAccount.php index 25cc617c9d..6c3f291eda 100644 --- a/src/Http/Livewire/EditAccount.php +++ b/src/Http/Livewire/EditAccount.php @@ -17,6 +17,11 @@ class EditAccount extends Action public $record; + public function getForm() + { + return new Form($this->fields(), static::class, $this->record); + } + public function fields() { return [ @@ -50,11 +55,6 @@ class EditAccount extends Action ]; } - public function getForm() - { - return new Form($this->fields(), static::class, $this->record); - } - public function mount() { $this->record = Auth::guard('filament')->user(); diff --git a/src/Resource.php b/src/Resource.php index 48430066f0..1d13312a90 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -45,15 +45,6 @@ abstract class Resource ->replace('-', ' '); } - public static function getSlug() - { - if (static::$slug) return static::$slug; - - return (string) Str::of(class_basename(static::$model)) - ->plural() - ->kebab(); - } - public static function route($name = null, $parameters = [], $absolute = true) { if (! $name) $name = static::router()->getIndexRoute()->name; @@ -66,6 +57,15 @@ abstract class Resource return new ResourceRouter(static::class); } + public static function getSlug() + { + if (static::$slug) return static::$slug; + + return (string) Str::of(class_basename(static::$model)) + ->plural() + ->kebab(); + } + public static function routes() { return []; diff --git a/src/ResourceAuthorizationManager.php b/src/ResourceAuthorizationManager.php index f58363819e..dde0d5c55b 100644 --- a/src/ResourceAuthorizationManager.php +++ b/src/ResourceAuthorizationManager.php @@ -43,14 +43,14 @@ class ResourceAuthorizationManager if (! $this->resource::router()->hasRoute($route)) return $this->type === 'deny'; return collect($this->authorizations) - ->contains(function ($authorization) use ($route, $user) { - if (! $user->hasRole($authorization->role)) return false; + ->contains(function ($authorization) use ($route, $user) { + if (! $user->hasRole($authorization->role)) return false; - if (in_array($route, $authorization->onlyRoutes)) return $this->type === 'allow'; + if (in_array($route, $authorization->onlyRoutes)) return $this->type === 'allow'; - if (in_array($route, $authorization->exceptRoutes)) return $this->type === 'deny'; + if (in_array($route, $authorization->exceptRoutes)) return $this->type === 'deny'; - return $this->type === 'deny'; - }) || $this->type === 'deny'; + return $this->type === 'deny'; + }) || $this->type === 'deny'; } } diff --git a/src/ResourceRouter.php b/src/ResourceRouter.php index 4daf2e8518..e74454dc56 100644 --- a/src/ResourceRouter.php +++ b/src/ResourceRouter.php @@ -15,6 +15,17 @@ class ResourceRouter $this->register($resource::routes()); } + public function register($routes) + { + if (! $routes) return; + + if (! is_array($routes)) $routes = [$routes]; + + $this->routes = array_merge($this->routes, $routes); + + return $this; + } + public function getIndexRoute() { return collect($this->routes) @@ -26,15 +37,4 @@ class ResourceRouter return collect($this->routes) ->contains(fn ($route) => $route->name === $name); } - - public function register($routes) - { - if (! $routes) return; - - if (! is_array($routes)) $routes = [$routes]; - - $this->routes = array_merge($this->routes, $routes); - - return $this; - } } diff --git a/src/Traits/FieldConcerns/CanBeAutocompleted.php b/src/Traits/FieldConcerns/CanBeAutocompleted.php index 809080fa09..d2a672dd5d 100644 --- a/src/Traits/FieldConcerns/CanBeAutocompleted.php +++ b/src/Traits/FieldConcerns/CanBeAutocompleted.php @@ -6,17 +6,17 @@ trait CanBeAutocompleted { public $autocomplete; - public function autocomplete($autocomplete = 'on') - { - $this->autocomplete = $autocomplete; - - return $this; - } - public function disableAutocomplete() { $this->autocomplete('off'); return $this; } + + public function autocomplete($autocomplete = 'on') + { + $this->autocomplete = $autocomplete; + + return $this; + } } diff --git a/src/Traits/FieldConcerns/CanHaveId.php b/src/Traits/FieldConcerns/CanHaveId.php new file mode 100644 index 0000000000..b6b794c299 --- /dev/null +++ b/src/Traits/FieldConcerns/CanHaveId.php @@ -0,0 +1,15 @@ +id = $id; + + return $this; + } +} diff --git a/src/Traits/FieldConcerns/CanHaveLabel.php b/src/Traits/FieldConcerns/CanHaveLabel.php new file mode 100644 index 0000000000..a0f2821826 --- /dev/null +++ b/src/Traits/FieldConcerns/CanHaveLabel.php @@ -0,0 +1,15 @@ +label = $label; + + return $this; + } +} diff --git a/src/Traits/Sushi.php b/src/Traits/Sushi.php index 42b590d682..454ffa6897 100644 --- a/src/Traits/Sushi.php +++ b/src/Traits/Sushi.php @@ -2,36 +2,23 @@ namespace Filament\Traits; +use DateTime; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Support\Str; +use ReflectionClass; trait Sushi { protected static $sushiConnection; - public function getRows() - { - return $this->rows; - } - - public function getSchema() - { - return $this->schema ?? []; - } - - public static function resolveConnection($connection = null) - { - return static::$sushiConnection; - } - public static function bootSushi() { $instance = (new static); - $cacheFileName = config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite'; + $cacheFileName = config('sushi.cache-prefix', 'sushi') . '-' . Str::kebab(str_replace('\\', '', static::class)) . '.sqlite'; $cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache'))); - $cachePath = $cacheDirectory.'/'.$cacheFileName; - $modelPath = (new \ReflectionClass(static::class))->getFileName(); + $cachePath = $cacheDirectory . '/' . $cacheFileName; + $modelPath = (new ReflectionClass(static::class))->getFileName(); $states = [ 'cache-file-found-and-up-to-date' => function () use ($cachePath) { @@ -94,6 +81,11 @@ trait Sushi static::insert($rows); } + public function getRows() + { + return $this->rows; + } + public function createTable(string $tableName, $firstRow) { static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) use ($firstRow) { @@ -113,7 +105,7 @@ trait Sushi case is_string($value): $type = 'string'; break; - case is_object($value) && $value instanceof \DateTime: + case is_object($value) && $value instanceof DateTime: $type = 'dateTime'; break; default: @@ -138,6 +130,24 @@ trait Sushi }); } + public static function resolveConnection($connection = null) + { + return static::$sushiConnection; + } + + public function getSchema() + { + return $this->schema ?? []; + } + + public function usesTimestamps() + { + // Override the Laravel default value of $timestamps = true; Unless otherwise set. + return (new ReflectionClass($this))->getProperty('timestamps')->class === static::class + ? parent::usesTimestamps() + : false; + } + public function createTableWithNoData(string $tableName) { static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) { @@ -161,12 +171,4 @@ trait Sushi } }); } - - public function usesTimestamps() - { - // Override the Laravel default value of $timestamps = true; Unless otherwise set. - return (new \ReflectionClass($this))->getProperty('timestamps')->class === static::class - ? parent::usesTimestamps() - : false; - } } diff --git a/src/View/Components/Form.php b/src/View/Components/Form.php index d420f3d726..e63c94c870 100644 --- a/src/View/Components/Form.php +++ b/src/View/Components/Form.php @@ -23,6 +23,32 @@ class Form extends Component if ($record) $this->passRecordToFields($record); } + public function passContextToFields($context = null) + { + if (! $context) return $this->fields; + + return $this->fields = collect($this->fields) + ->map(function ($field) use ($context) { + return $field + ->context($context) + ->fields($field->getForm()->passContextToFields($context)); + }) + ->toArray(); + } + + public function passRecordToFields($record = null) + { + if (! $record) return $this->fields; + + return $this->fields = collect($this->fields) + ->map(function ($field) use ($record) { + return $field + ->record($record) + ->fields($field->getForm()->passRecordToFields($record)); + }) + ->toArray(); + } + public function callHooks($action, $event) { $hooks = $this->getHooks(); @@ -38,6 +64,19 @@ class Form extends Component return $action; } + public function getHooks() + { + $hooks = $this->hooks; + + foreach ($this->fields as $field) { + foreach ($field->getHooks as $event => $callbacks) { + $hooks[$event] = array_merge($hooks[$event] ?? [], $callbacks); + } + } + + return $hooks; + } + public function columns($columns) { $this->columns = $columns; @@ -56,19 +95,6 @@ class Form extends Component return $defaults; } - public function getHooks() - { - $hooks = $this->hooks; - - foreach ($this->fields as $field) { - foreach ($field->getHooks as $event => $callbacks) { - $hooks[$event] = array_merge($hooks[$event] ?? [], $callbacks); - } - } - - return $hooks; - } - public function getRules() { $rules = $this->rules; @@ -98,32 +124,6 @@ class Form extends Component $this->hooks = $hooks; } - public function passContextToFields($context = null) - { - if (! $context) return $this->fields; - - return $this->fields = collect($this->fields) - ->map(function ($field) use ($context) { - return $field - ->context($context) - ->fields($field->getForm()->passContextToFields($context)); - }) - ->toArray(); - } - - public function passRecordToFields($record = null) - { - if (! $record) return $this->fields; - - return $this->fields = collect($this->fields) - ->map(function ($field) use ($record) { - return $field - ->record($record) - ->fields($field->getForm()->passRecordToFields($record)); - }) - ->toArray(); - } - public function rules($rules) { $this->rules = $rules; diff --git a/src/helpers.php b/src/helpers.php index 8bc8ef2631..9f97f71295 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -8,7 +8,7 @@ if (! function_exists('Filament\format_attributes')) { function format_attributes($attributes = []) { return collect($attributes) - ->map(fn($value, $key) => "{$key}={$value}") + ->map(fn ($value, $key) => "{$key}={$value}") ->join(' '); } }