F
Filamentβ€’6mo ago
ericmp

Autofocus only when on desktop & action is create

i have this field:
Forms\Components\TextInput::make('name')
->autofocus()
Forms\Components\TextInput::make('name')
->autofocus()
currently, is auto-focused, but i want only it to be auto-focused when the user is on a desktop device and the current action is the create action
10 Replies
ericmp
ericmpβ€’6mo ago
at the moment i just found out this: https://filamentphp.com/docs/3.x/forms/advanced#injecting-the-current-form-operation but idk how to use it
Prymiee
Prymieeβ€’6mo ago
Not sure about how to achive the desktop determination (maybe you could check via the user-agent or defer to a package like: https://github.com/phattarachai/laravel-mobile-detect) As for implementation regarding the "create" action (https://filamentphp.com/docs/3.x/forms/advanced#injecting-the-current-form-operation):
TextInput::make()->autofocus(function (string $operation) {
return $operation === 'create';
});
TextInput::make()->autofocus(function (string $operation) {
return $operation === 'create';
});
Prymiee
Prymieeβ€’6mo ago
You may have to use the mount method, inject the Illuminate\Http\Request and set the variable on the component itself with an attribute like Locked (https://livewire.laravel.com/docs/locked) Edit, I'm dumb and should be able to inject within the autofocus closure.
ericmp
ericmpβ€’6mo ago
but the filament resource has mount method? class ClientResource extends Resource all calls seem static cool, the operation thing πŸ‘€
ericmp
ericmpβ€’6mo ago
just tried this, doesnt dd
No description
ericmp
ericmpβ€’6mo ago
also yeah, i know this package, similar to the one u shared - https://github.com/jenssegers/agent but i mean, id like to do it via screen width so i dont need packages, but idk if this can be done πŸ€” argh, also tried: ->visibleFrom('md') i got: Method Filament\Forms\Components\TextInput::visibleFrom does not exist. i keep pushing haha
Prymiee
Prymieeβ€’6mo ago
it would be on the Create resource page
ericmp
ericmpβ€’6mo ago
but in the create resource page i dont have the form fields, can i interact with them there?
Prymiee
Prymieeβ€’6mo ago
@ericmp so sorry for the delay and misleading information;
->autofocus(function (Request $request, string $operation) {
return $operation === 'create' && somewayofdeterminingagent$request;
}),
->autofocus(function (Request $request, string $operation) {
return $operation === 'create' && somewayofdeterminingagent$request;
}),
ericmp
ericmpβ€’6mo ago
no worries, oh hmmm so u can pass in the request too, interesting. im going to work around this. thanks πŸ™Œ
Want results from more Discord servers?
Add your server
More Posts