Make "callback" parameter names more specific

This commit is contained in:
Dan Harrin
2023-07-06 22:05:38 +01:00
parent 2971a12ca9
commit 34af23afaa
16 changed files with 76 additions and 76 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ namespace Livewire\Testing {
public function assertFormExists(string $name = 'form'): static {}
public function assertFormFieldExists(string $fieldName, string | Closure $formName = 'form', ?Closure $callback = null): static {}
public function assertFormFieldExists(string $fieldName, string | Closure $formName = 'form', ?Closure $checkFieldUsing = null): static {}
public function assertFormFieldIsDisabled(string $fieldName, string $formName = 'form'): static {}
+17 -17
View File
@@ -106,10 +106,10 @@ You may employ the `relationship()` method of the `Select` to configure a `Belon
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
```
The `multiple()` method may be used in combination with `relationship()` to use a `BelongsToMany` relationship. Filament will load the options from the relationship, and save them back to the relationship's pivot table when the form is submitted. If a `relationshipName` is not provided, Filament will use the field name as the relationship name:
The `multiple()` method may be used in combination with `relationship()` to use a `BelongsToMany` relationship. Filament will load the options from the relationship, and save them back to the relationship's pivot table when the form is submitted. If a `name` is not provided, Filament will use the field name as the relationship name:
```php
use Filament\Forms\Components\Select;
@@ -127,7 +127,7 @@ By default, if the select is also searchable, Filament will return search result
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable(['name', 'email'])
```
@@ -139,7 +139,7 @@ If you'd like to populate the searchable options from the database when the page
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->preload()
```
@@ -154,9 +154,9 @@ use Illuminate\Database\Eloquent\Builder;
Select::make('author_id')
->relationship(
relationshipName: 'author',
name: 'author',
titleAttribute: 'name',
modifyOptionsQueryUsing: fn (Builder $query) => $query->withTrashed(),
modifyQueryUsing: fn (Builder $query) => $query->withTrashed(),
)
```
@@ -172,7 +172,7 @@ $table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'full_name')
->relationship(name: 'author', titleAttribute: 'full_name')
```
Alternatively, you can use the `getOptionLabelFromRecordUsing()` method to transform an option's Eloquent model into a label:
@@ -184,8 +184,8 @@ use Illuminate\Database\Eloquent\Model;
Select::make('author_id')
->relationship(
relationshipName: 'author',
modifyOptionsQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
name: 'author',
modifyQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
->searchable(['first_name', 'last_name'])
@@ -200,7 +200,7 @@ use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Model;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
@@ -225,7 +225,7 @@ use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Model;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->editOptionAction([
Forms\Components\TextInput::make('name')
->required(),
@@ -401,7 +401,7 @@ When you're using a searchable select or multi-select, you may want to display a
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->loadingMessage('Loading authors...')
```
@@ -414,7 +414,7 @@ When you're using a searchable select or multi-select, you may want to display a
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->noSearchResultsMessage('No authors found.')
```
@@ -427,7 +427,7 @@ When you're using a searchable select or multi-select, you may want to display a
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable(['name', 'email'])
->searchPrompt('Search authors by their name or email address')
```
@@ -440,7 +440,7 @@ When you're using a searchable select or multi-select, you may want to display a
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->searchingMessage('Searching authors...')
```
@@ -453,7 +453,7 @@ By default, Filament will wait 1000 milliseconds (1 second) before searching for
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->searchDebounce(500)
```
@@ -468,7 +468,7 @@ You can limit the number of options that are displayed in a searchable select or
use Filament\Forms\Components\Select;
Select::make('author_id')
->relationship(relationshipName: 'author', titleAttribute: 'name')
->relationship(name: 'author', titleAttribute: 'name')
->searchable()
->optionsLimit(20)
```
@@ -127,7 +127,7 @@ use Illuminate\Database\Eloquent\Builder;
CheckboxList::make('technologies')
->relationship(
titleAttribute: 'name',
modifyOptionsQueryUsing: fn (Builder $query) => $query->withTrashed(),
modifyQueryUsing: fn (Builder $query) => $query->withTrashed(),
)
```
@@ -155,7 +155,7 @@ use Illuminate\Database\Eloquent\Model;
CheckboxList::make('authors')
->relationship(
modifyOptionsQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
modifyQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
```
+2 -2
View File
@@ -191,7 +191,7 @@ You can further customize the rule by passing a [closure](advanced#closure-custo
use Illuminate\Validation\Rules\Exists;
Field::make('invitation')
->exists(callback: function (Exists $rule) {
->exists(modifyRuleUsing: function (Exists $rule) {
return $rule->where('is_active', 1);
})
```
@@ -436,7 +436,7 @@ You can further customize the rule by passing a [closure](advanced#closure-custo
use Illuminate\Validation\Rules\Unique;
Field::make('email')
->unique(callback: function (Unique $rule) {
->unique(modifyRuleUsing: function (Unique $rule) {
return $rule->where('is_active', 1);
})
```
@@ -113,18 +113,18 @@ class CheckboxList extends Field implements Contracts\HasNestedRecursiveValidati
return 'deselectAll';
}
public function relationship(string | Closure | null $relationshipName, string | Closure | null $titleAttribute, ?Closure $modifyOptionsQueryUsing = null): static
public function relationship(string | Closure | null $name, string | Closure | null $titleAttribute, ?Closure $modifyQueryUsing = null): static
{
$this->relationship = $relationshipName ?? $this->getName();
$this->relationship = $name ?? $this->getName();
$this->relationshipTitleAttribute = $titleAttribute;
$this->options(static function (CheckboxList $component) use ($modifyOptionsQueryUsing): array {
$this->options(static function (CheckboxList $component) use ($modifyQueryUsing): array {
$relationship = $component->getRelationship();
$relationshipQuery = $relationship->getRelated()->query();
if ($modifyOptionsQueryUsing) {
$relationshipQuery = $component->evaluate($modifyOptionsQueryUsing, [
if ($modifyQueryUsing) {
$relationshipQuery = $component->evaluate($modifyQueryUsing, [
'query' => $relationshipQuery,
]) ?? $relationshipQuery;
}
@@ -168,16 +168,16 @@ trait CanBeValidated
return $this;
}
public function exists(string | Closure | null $table = null, string | Closure | null $column = null, ?Closure $callback = null): static
public function exists(string | Closure | null $table = null, string | Closure | null $column = null, ?Closure $modifyRuleUsing = null): static
{
$this->rule(static function (Field $component, ?string $model) use ($callback, $column, $table) {
$this->rule(static function (Field $component, ?string $model) use ($column, $modifyRuleUsing, $table) {
$table = $component->evaluate($table) ?? $model;
$column = $component->evaluate($column) ?? $component->getName();
$rule = Rule::exists($table, $column);
if ($callback) {
$rule = $component->evaluate($callback, [
if ($modifyRuleUsing) {
$rule = $component->evaluate($modifyRuleUsing, [
'rule' => $rule,
]) ?? $rule;
}
@@ -501,9 +501,9 @@ trait CanBeValidated
return $this->fieldComparisonRule('same', $statePath, $isStatePathAbsolute);
}
public function unique(string | Closure | null $table = null, string | Closure | null $column = null, Model | Closure $ignorable = null, ?Closure $callback = null, bool $ignoreRecord = false): static
public function unique(string | Closure | null $table = null, string | Closure | null $column = null, Model | Closure $ignorable = null, bool $ignoreRecord = false, ?Closure $modifyRuleUsing = null): static
{
$this->rule(static function (Field $component, ?string $model) use ($callback, $column, $ignorable, $table, $ignoreRecord) {
$this->rule(static function (Field $component, ?string $model) use ($column, $ignorable, $ignoreRecord, $modifyRuleUsing, $table) {
$table = $component->evaluate($table) ?? $model;
$column = $component->evaluate($column) ?? $component->getName();
$ignorable = ($ignoreRecord && ! $ignorable) ?
@@ -519,8 +519,8 @@ trait CanBeValidated
),
);
if ($callback) {
$rule = $component->evaluate($callback, [
if ($modifyRuleUsing) {
$rule = $component->evaluate($modifyRuleUsing, [
'rule' => $rule,
]) ?? $rule;
}
@@ -24,10 +24,10 @@ trait EntanglesStateWithSingularRelationship
protected ?Closure $mutateRelationshipDataBeforeSaveUsing = null;
public function relationship(string $relationshipName, bool | Closure $condition = true): static
public function relationship(string $name, bool | Closure $condition = true): static
{
$this->relationship = $relationshipName;
$this->statePath($relationshipName);
$this->relationship = $name;
$this->statePath($name);
$this->loadStateFromRelationshipsUsing(static function (Component | CanEntangleWithSingularRelationships $component) {
$component->clearCachedExistingRecord();
+14 -14
View File
@@ -621,18 +621,18 @@ class Select extends Field implements Contracts\HasAffixActions, Contracts\HasNe
return $this->evaluate($this->isSearchable) || $this->isMultiple();
}
public function relationship(string | Closure | null $relationshipName, string | Closure | null $titleAttribute, ?Closure $modifyOptionsQueryUsing = null): static
public function relationship(string | Closure | null $name, string | Closure | null $titleAttribute, ?Closure $modifyQueryUsing = null): static
{
$this->relationship = $relationshipName ?? $this->getName();
$this->relationship = $name ?? $this->getName();
$this->relationshipTitleAttribute = $titleAttribute;
$this->getSearchResultsUsing(static function (Select $component, ?string $search) use ($modifyOptionsQueryUsing): array {
$this->getSearchResultsUsing(static function (Select $component, ?string $search) use ($modifyQueryUsing): array {
$relationship = $component->getRelationship();
$relationshipQuery = $relationship->getRelated()->query();
if ($modifyOptionsQueryUsing) {
$relationshipQuery = $component->evaluate($modifyOptionsQueryUsing, [
if ($modifyQueryUsing) {
$relationshipQuery = $component->evaluate($modifyQueryUsing, [
'query' => $relationshipQuery,
]) ?? $relationshipQuery;
}
@@ -683,7 +683,7 @@ class Select extends Field implements Contracts\HasAffixActions, Contracts\HasNe
->toArray();
});
$this->options(static function (Select $component) use ($modifyOptionsQueryUsing): ?array {
$this->options(static function (Select $component) use ($modifyQueryUsing): ?array {
if (($component->isSearchable()) && ! $component->isPreloaded()) {
return null;
}
@@ -692,8 +692,8 @@ class Select extends Field implements Contracts\HasAffixActions, Contracts\HasNe
$relationshipQuery = $relationship->getRelated()->query();
if ($modifyOptionsQueryUsing) {
$relationshipQuery = $component->evaluate($modifyOptionsQueryUsing, [
if ($modifyQueryUsing) {
$relationshipQuery = $component->evaluate($modifyQueryUsing, [
'query' => $relationshipQuery,
]) ?? $relationshipQuery;
}
@@ -783,13 +783,13 @@ class Select extends Field implements Contracts\HasAffixActions, Contracts\HasNe
return $record->getAttributeValue($component->getRelationshipTitleAttribute());
});
$this->getSelectedRecordUsing(static function (Select $component, $state) use ($modifyOptionsQueryUsing): ?Model {
$this->getSelectedRecordUsing(static function (Select $component, $state) use ($modifyQueryUsing): ?Model {
$relationship = $component->getRelationship();
$relationshipQuery = $relationship->getRelated()->query()->where($relationship->getOwnerKeyName(), $state);
if ($modifyOptionsQueryUsing) {
$relationshipQuery = $component->evaluate($modifyOptionsQueryUsing, [
if ($modifyQueryUsing) {
$relationshipQuery = $component->evaluate($modifyQueryUsing, [
'query' => $relationshipQuery,
]) ?? $relationshipQuery;
}
@@ -797,15 +797,15 @@ class Select extends Field implements Contracts\HasAffixActions, Contracts\HasNe
return $relationshipQuery->first();
});
$this->getOptionLabelsUsing(static function (Select $component, array $values) use ($modifyOptionsQueryUsing): array {
$this->getOptionLabelsUsing(static function (Select $component, array $values) use ($modifyQueryUsing): array {
$relationship = $component->getRelationship();
$relatedKeyName = $relationship->getRelatedKeyName();
$relationshipQuery = $relationship->getRelated()->query()
->whereIn($relatedKeyName, $values);
if ($modifyOptionsQueryUsing) {
$relationshipQuery = $component->evaluate($modifyOptionsQueryUsing, [
if ($modifyQueryUsing) {
$relationshipQuery = $component->evaluate($modifyQueryUsing, [
'query' => $relationshipQuery,
]) ?? $relationshipQuery;
}
@@ -33,21 +33,21 @@ trait HasComponents
return $this;
}
public function getComponent(string | Closure $callback, bool $withHidden = false): ?Component
public function getComponent(string | Closure $findComponentUsing, bool $withHidden = false): ?Component
{
if (is_string($callback)) {
$callback = static function (Component $component) use ($callback): bool {
if (is_string($findComponentUsing)) {
$findComponentUsing = static function (Component $component) use ($findComponentUsing): bool {
$key = $component->getKey();
if ($key === null) {
return false;
}
return $key === $callback;
return $key === $findComponentUsing;
};
}
return collect($this->getFlatComponents($withHidden))->first($callback);
return collect($this->getFlatComponents($withHidden))->first($findComponentUsing);
}
/**
+4 -4
View File
@@ -138,9 +138,9 @@ class TestsForms
public function assertFormFieldExists(): Closure
{
return function (string $fieldName, string | Closure $formName = 'form', ?Closure $callback = null): static {
return function (string $fieldName, string | Closure $formName = 'form', ?Closure $checkFieldUsing = null): static {
if ($formName instanceof Closure) {
$callback = $formName;
$checkFieldUsing = $formName;
$formName = 'form';
}
@@ -161,9 +161,9 @@ class TestsForms
"Failed asserting that a field with the name [{$fieldName}] exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
);
if ($callback) {
if ($checkFieldUsing) {
Assert::assertTrue(
$callback($field),
$checkFieldUsing($field),
"Failed asserting that a field with the name [{$fieldName}] and provided configuration exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
);
}
@@ -4,9 +4,9 @@ namespace Filament\Infolists\Components\Concerns;
trait EntanglesStateWithSingularRelationship
{
public function relationship(string $relationshipName): static
public function relationship(string $name): static
{
$this->statePath($relationshipName);
$this->statePath($name);
return $this;
}
+2 -2
View File
@@ -612,10 +612,10 @@ class FilamentManager
}
}
public function registerRenderHook(string $name, Closure $callback): void
public function registerRenderHook(string $name, Closure $hook): void
{
try {
$this->getDefaultPanel()->renderHook($name, $callback);
$this->getDefaultPanel()->renderHook($name, $hook);
} catch (NoDefaultPanelSetException $exception) {
throw new Exception('Please use the `renderHook()` method on the panel configuration to register render hooks. See the documentation - https://filamentphp.com/docs/panels/configuration#render-hooks');
}
@@ -13,9 +13,9 @@ trait HasRenderHooks
*/
protected array $renderHooks = [];
public function renderHook(string $name, Closure $callback): static
public function renderHook(string $name, Closure $hook): static
{
$this->renderHooks[$name][] = $callback;
$this->renderHooks[$name][] = $hook;
return $this;
}
@@ -15,9 +15,9 @@ trait CanValidateInput
/**
* @param array<array-key> $rules
*/
protected function validateInput(Closure $callback, string $field, array $rules, ?Closure $onError = null): string
protected function validateInput(Closure $askUsing, string $field, array $rules, ?Closure $onError = null): string
{
$input = $callback();
$input = $askUsing();
$validator = Validator::make(
[$field => $input],
@@ -31,7 +31,7 @@ trait CanValidateInput
$onError($validator);
}
$input = $this->validateInput($callback, $field, $rules);
$input = $this->validateInput($askUsing, $field, $rules);
}
return $input;
@@ -16,14 +16,14 @@ trait Configurable
*/
protected static array $importantConfigurations = [];
public static function configureUsing(Closure $callback, ?Closure $during = null, bool $isImportant = false): mixed
public static function configureUsing(Closure $modifyUsing, ?Closure $during = null, bool $isImportant = false): mixed
{
if ($isImportant) {
static::$importantConfigurations[static::class] ??= [];
static::$importantConfigurations[static::class][] = $callback;
static::$importantConfigurations[static::class][] = $modifyUsing;
} else {
static::$configurations[static::class] ??= [];
static::$configurations[static::class][] = $callback;
static::$configurations[static::class][] = $modifyUsing;
}
if (! $during) {
@@ -10,11 +10,11 @@ trait HasRelationship
{
protected ?Closure $modifyRelationshipQueryUsing = null;
public function relationship(string $relationshipName, string $titleAttribute = null, Closure $callback = null): static
public function relationship(string $name, string $titleAttribute = null, ?Closure $modifyQueryUsing = null): static
{
$this->attribute("{$relationshipName}.{$titleAttribute}");
$this->attribute("{$name}.{$titleAttribute}");
$this->modifyRelationshipQueryUsing = $callback;
$this->modifyRelationshipQueryUsing = $modifyQueryUsing;
return $this;
}