Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support actions on Input #1235

Open
james-w opened this issue Feb 6, 2024 · 5 comments
Open

Support actions on Input #1235

james-w opened this issue Feb 6, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@james-w
Copy link

james-w commented Feb 6, 2024

Summary

I'd like to be able to trigger focus changes to an input element. One way to do this is with a small action to call .focus() on the element, but arbitrary actions aren't exposed by the component.

Basic example

function focus(el) {
  el.focus();
}

<Input use:focus />

Motivation

When dynamically adding an input to the page I want to be able to focus it to allow the user to start typing.

autofocus is supported, but this doesn't always work depending on focus state of the page.

Alternatively, exposing a ref to the html element would allow the function to be called in a different way.

Thanks for your work on the library, it's been great to use so far!

@hardyjosh
Copy link

@james-w afaik you can't use the use directive with components, only HTML elements. So it would have to be something like

function focus(el) {
  el.focus();
}

<Input action={focus} />

@TopherTimeMachine
Copy link

@james-w afaik you can't use the use directive with components, only HTML elements. So it would have to be something like

function focus(el) {
  el.focus();
}

<Input action={focus} />

This doesn't seem to work for me. I'm trying to do a focus on an input when the modal opens.

What am I doing wrong here?

  <script>
  function focus(el) {
    console.log('focus1',el);  // not working!
    el.focus();
  }

<Section>
  <Modal bind:open={isModalOpen} title={editTag ? 'Edit '+tagName : 'Add '+tagName}>
      <Input bind:this={inputElement} bind:value={newTag} placeholder={tagName} action={focus}/>
      <Button on:click={saveTag}  class="mt-4">{editTag ? 'Save' : 'Add'}</Button>
      <Button on:click={deleteTag}  class="mt-4">Delete</Button>
  </Modal>
</Section>

@hardyjosh
Copy link

@TopherTimeMachine this isn't implemented, I was just discussing a possible approach because use wouldn't work without modifying the preprocessor somehow.

@TopherTimeMachine
Copy link

@TopherTimeMachine this isn't implemented, I was just discussing a possible approach because use wouldn't work without modifying the preprocessor somehow.

Is there currently any workarounds for making this work? Or what's the best way to get access to the html element?

@hardyjosh
Copy link

Is there currently any workarounds for making this work? Or what's the best way to get access to the html element?

No workaround I'm aware of other than making your own component, which is what we did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants