behe

behe

I have created a list with items inn LiveView which when an item is clicked is rerendered as an input field. Is there a callback or similar that I can hook into to also be able to put text input focus on the selected input field?

Showing Posts 1 to 10

chrismccord

chrismccord

Creator of Phoenix

Add autofocus to the input tag

hurty

hurty

The autofocus attribute doesn’t seem sufficient in practice. The behaviour is not the same across all browsers. Can I use... Support tables for HTML5, CSS3, etc says it doesn’t work with iOS Safari.

I’ve updated the table editor demo with an autofocus attribute that should resemble @behe use case : https://liveview.cleverapps.io/tables

  • Chrome: works for the first input appearance, but not for the other subsequent inputs. The cursor is at the beginning of text.
  • Firefox: doesn’t work at all,
  • MacOS Safari : works for each input, the input is focused and all the text inside is selected,
  • iOS Safari: the input is focused, but the text is not selected and the keyboard isn’t shown.
behe

behe OP

This only seems to works once. The next input will unfortunately not receive focus.

sribe

sribe

Changing an input type, or enabling, or adding it, then giving it focus is just one of those things that GUI libs worked out in the 1980s, but that web browsers still screw up royally. If you were to take the time to try, you’d find that you cannot do this reliably in straight browser-side Javascript. The closest you can come is to set a timer to fire to set focus after you’ve done the other stuff, and even then it’s iffy.

maz

maz

And this is the REAL reason why the Native vs. Web war never ends.

jsteiner

jsteiner

Similar question: I’d like to scroll to a newly added element. Basically Add dynamic inputs_for example · chrismccord/phoenix_live_view_example@1181c79 · GitHub, but the new element may be out of view so I want to move it into the viewport.

Sounds like maybe there are no client side callbacks available? Any plans/thoughts on supporting something like that?

Owens

Owens

I have the same need, but decided to just let the item be a text input instead of click and turn it into a text input. If needed you can style the input so when it’s not focused it doesn’t look like an input, then when you focus it, the style changes and it looks like an input.

behe

behe OP

This can now be solved with a phx-hook: Phoenix.LiveView — Phoenix LiveView v1.2.5

ibarch

ibarch

The following solution works for me.

Create AutoFocus hook in ./assets/js/app.js

let Hooks = {}
Hooks.AutoFocus = {
  mounted() {
    this.el.focus()
  }
}

let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, ...})  

Attach it to the first input field of a form

<.form let={f} for={@changeset} id="my-form"
  <%= label f, :name  %>
  <%= text_input f, :name, phx_hook: "AutoFocus" %>
  ...
</.form>

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews