mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Add modifyTypeSelectUsing and modifyKeySelectUsing methods to MorphToSelect
This commit is contained in:
@@ -31,6 +31,16 @@ class MorphToSelect extends Component
|
||||
*/
|
||||
protected array | Closure $types = [];
|
||||
|
||||
/**
|
||||
* @var ?Closure
|
||||
*/
|
||||
protected ?Closure $modifyTypeSelectUsing = null;
|
||||
|
||||
/**
|
||||
* @var ?Closure
|
||||
*/
|
||||
protected ?Closure $modifyKeySelectUsing = null;
|
||||
|
||||
final public function __construct(string $name)
|
||||
{
|
||||
$this->name($name);
|
||||
@@ -59,48 +69,75 @@ class MorphToSelect extends Component
|
||||
/** @var ?Type $selectedType */
|
||||
$selectedType = $types[$this->evaluate(fn (Get $get): ?string => $get($typeColumn))] ?? null;
|
||||
|
||||
return [
|
||||
Select::make($typeColumn)
|
||||
->label($this->getLabel())
|
||||
->hiddenLabel()
|
||||
->options(array_map(
|
||||
fn (Type $type): string => $type->getLabel(),
|
||||
$types,
|
||||
))
|
||||
->native($this->isNative())
|
||||
->required($isRequired)
|
||||
->live()
|
||||
->afterStateUpdated(function (Set $set) use ($keyColumn) {
|
||||
$set($keyColumn, null);
|
||||
$this->callAfterStateUpdated();
|
||||
}),
|
||||
Select::make($keyColumn)
|
||||
->label($selectedType?->getLabel())
|
||||
->hiddenLabel()
|
||||
->options($selectedType?->getOptionsUsing)
|
||||
->getSearchResultsUsing($selectedType?->getSearchResultsUsing)
|
||||
->getOptionLabelUsing($selectedType?->getOptionLabelUsing)
|
||||
->native($this->isNative())
|
||||
->required(filled($selectedType))
|
||||
->hidden(blank($selectedType))
|
||||
->dehydratedWhenHidden()
|
||||
->searchable($this->isSearchable())
|
||||
->searchDebounce($this->getSearchDebounce())
|
||||
->searchPrompt($this->getSearchPrompt())
|
||||
->searchingMessage($this->getSearchingMessage())
|
||||
->noSearchResultsMessage($this->getNoSearchResultsMessage())
|
||||
->loadingMessage($this->getLoadingMessage())
|
||||
->allowHtml($this->isHtmlAllowed())
|
||||
->optionsLimit($this->getOptionsLimit())
|
||||
->preload($this->isPreloaded())
|
||||
->when(
|
||||
$this->isLive(),
|
||||
fn (Select $component) => $component->live(onBlur: $this->isLiveOnBlur()),
|
||||
)
|
||||
->afterStateUpdated(function () {
|
||||
$this->callAfterStateUpdated();
|
||||
}),
|
||||
];
|
||||
$typeSelect = Select::make($typeColumn)
|
||||
->label($this->getLabel())
|
||||
->hiddenLabel()
|
||||
->options(array_map(
|
||||
fn (Type $type): string => $type->getLabel(),
|
||||
$types,
|
||||
))
|
||||
->native($this->isNative())
|
||||
->required($isRequired)
|
||||
->live()
|
||||
->afterStateUpdated(function (Set $set) use ($keyColumn) {
|
||||
$set($keyColumn, null);
|
||||
$this->callAfterStateUpdated();
|
||||
});
|
||||
|
||||
$keySelect = Select::make($keyColumn)
|
||||
->label($selectedType?->getLabel())
|
||||
->hiddenLabel()
|
||||
->options($selectedType?->getOptionsUsing)
|
||||
->getSearchResultsUsing($selectedType?->getSearchResultsUsing)
|
||||
->getOptionLabelUsing($selectedType?->getOptionLabelUsing)
|
||||
->native($this->isNative())
|
||||
->required(filled($selectedType))
|
||||
->hidden(blank($selectedType))
|
||||
->dehydratedWhenHidden()
|
||||
->searchable($this->isSearchable())
|
||||
->searchDebounce($this->getSearchDebounce())
|
||||
->searchPrompt($this->getSearchPrompt())
|
||||
->searchingMessage($this->getSearchingMessage())
|
||||
->noSearchResultsMessage($this->getNoSearchResultsMessage())
|
||||
->loadingMessage($this->getLoadingMessage())
|
||||
->allowHtml($this->isHtmlAllowed())
|
||||
->optionsLimit($this->getOptionsLimit())
|
||||
->preload($this->isPreloaded())
|
||||
->when(
|
||||
$this->isLive(),
|
||||
fn (Select $component) => $component->live(onBlur: $this->isLiveOnBlur()),
|
||||
)
|
||||
->afterStateUpdated(function () {
|
||||
$this->callAfterStateUpdated();
|
||||
});
|
||||
|
||||
if ($this->modifyTypeSelectUsing) {
|
||||
$typeSelect = $this->evaluate($this->modifyTypeSelectUsing, [
|
||||
'select' => $typeSelect,
|
||||
]) ?? $typeSelect;
|
||||
}
|
||||
|
||||
if ($this->modifyKeySelectUsing) {
|
||||
$keySelect = $this->evaluate($this->modifyKeySelectUsing, [
|
||||
'select' => $keySelect,
|
||||
]) ?? $keySelect;
|
||||
}
|
||||
|
||||
return [$typeSelect, $keySelect];
|
||||
}
|
||||
|
||||
public function modifyTypeSelectUsing(?Closure $callback): static
|
||||
{
|
||||
$this->modifyTypeSelectUsing = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifyKeySelectUsing(?Closure $callback): static
|
||||
{
|
||||
$this->modifyKeySelectUsing = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function optionsLimit(int | Closure $limit): static
|
||||
|
||||
Reference in New Issue
Block a user