Skip to content

Fix lost input focus after remote search #827

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Raidri
Copy link

@Raidri Raidri commented Feb 25, 2020

Description

Fixed the bug #806.
Added to the input element the focus() function. It solves the problem that the focus get lost after a remote search.

Types of changes

  • Chore (tooling change or documentation change)
  • Refactor (non-breaking change which maintains existing functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@mtriff mtriff changed the title fix bug #806 Fix lost input focus after remote search Dec 22, 2021
@Xon
Copy link
Collaborator

Xon commented Aug 22, 2024

I plan to investigate more as it looks like how the input field is being disabled while data is being loaded is just disruptive and probably needs a re-think

@ritwikBiswas007
Copy link

<select id="users"></select>

const select = document.getElementById('users');

const choice = new Choices(select, {
    searchChoices: false,
    searchResultLimit: 15,
    shouldSort: false,
    removeItemButton: false,
});

// Debounce function to limit how often we call API
function debounce(func, delay) {
    let timeout;
    return function (...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => func.apply(this, args), delay);
    };
}

// Debounced search handler
const handleSearch = debounce(async function (event) {
    const searchTerm = event.detail.value;

    if (!searchTerm) return;

    const input = document.querySelector('.choices__input');

    // Show loading text
    choice.clearChoices();
    choice.setChoices([{ value: '', label: 'Loading...', disabled: true }], 'value', 'label', false);

    try {
        const response = await fetch(`https://randomuser.me/api/?results=10`);
        const data = await response.json();

        const newChoices = data.results.map(user => ({
            value: user.login.uuid,
            label: `${user.name.first} ${user.name.last}`,
        }));

        choice.setChoices(newChoices, 'value', 'label', true);

        // Refocus the input
        setTimeout(() => {
            const newInput = document.querySelector('.choices__input');
            if (newInput) {
                newInput.focus();
                // Restore cursor position
                const val = newInput.value;
                newInput.value = '';
                newInput.value = val;
            }
        }, 30);
    } catch (error) {
        console.error('API error:', error);
        choice.setChoices([{ value: '', label: 'Error loading users', disabled: true }], 'value', 'label', false);
    }
}, 300);

// Add listener
select.addEventListener('search', handleSearch);

This code fixed the issue

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

Successfully merging this pull request may close these issues.

3 participants