Fire an action after updating Select?

I was wondering if it is possible to run an action when updating the value of a Select, but with a custom form. Like I can do with a Table Action. I want to change e.g. the status of a record, but also set some other fields and allow the user to input a comment.
6 Replies
Dennis Koch
Dennis Koch2y ago
->reactive()->afterStateUpdated(fn () => ...)
Daniel Plomp
Daniel PlompOP2y ago
But can I do something like this:
Tables\Actions\Action::make('change_status')
->icon('heroicon-o-lightning-bolt')
->action(function ($record, $data) {
$status = $data['status'];
$record->status = $status;
$record->save();
})
->modalHeading(__('strings.strings.status_change'))
->label(__('strings.strings.status_change'))
->form([
StatusSelectHelper::makeSelect('status', __('strings.fields.status'))->required(),
]),
Tables\Actions\Action::make('change_status')
->icon('heroicon-o-lightning-bolt')
->action(function ($record, $data) {
$status = $data['status'];
$record->status = $status;
$record->save();
})
->modalHeading(__('strings.strings.status_change'))
->label(__('strings.strings.status_change'))
->form([
StatusSelectHelper::makeSelect('status', __('strings.fields.status'))->required(),
]),
Dennis Koch
Dennis Koch2y ago
What's the issue with that one? You want it to update immediately when you change the Select
Daniel Plomp
Daniel PlompOP2y ago
Yes, from the Form, not the Table.
Dennis Koch
Dennis Koch2y ago
I guess something similar like this could work. ->extraInputAttributes(['wire:change' => 'submit')
Daniel Plomp
Daniel PlompOP2y ago
Someone was using this approach, but that adds a button which results in an extra action for the user:
Forms\Components\Select::make('booker_id')
->reactive()
->searchable()
->relationship('booker', 'name')
->getSearchResultsUsing(fn (string $search, callable $get) => Booker::where('client_id', '=', $get('client_id'))->where('name', 'like', "%$search%")->pluck('name', 'id')->toArray())
->tap(function (Select $component) {
$action = Forms\Components\Actions\Action::make('view_email')
->icon('heroicon-o-search')
->action(fn() => '')
->form(function (callable $get) {
return [
TextInput::make('email')
->disabled()
->default(Booker::find($get('booker_id'))->pluck('email'))
];
});
$component->registerActions(['view_email' => $action]);
$component->suffixAction($action);
}),
Forms\Components\Select::make('booker_id')
->reactive()
->searchable()
->relationship('booker', 'name')
->getSearchResultsUsing(fn (string $search, callable $get) => Booker::where('client_id', '=', $get('client_id'))->where('name', 'like', "%$search%")->pluck('name', 'id')->toArray())
->tap(function (Select $component) {
$action = Forms\Components\Actions\Action::make('view_email')
->icon('heroicon-o-search')
->action(fn() => '')
->form(function (callable $get) {
return [
TextInput::make('email')
->disabled()
->default(Booker::find($get('booker_id'))->pluck('email'))
];
});
$component->registerActions(['view_email' => $action]);
$component->suffixAction($action);
}),
Want results from more Discord servers?
Add your server