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

onBlur prop #93

Closed
devmartynov opened this issue Sep 15, 2022 · 3 comments
Closed

onBlur prop #93

devmartynov opened this issue Sep 15, 2022 · 3 comments

Comments

@devmartynov
Copy link

devmartynov commented Sep 15, 2022

it would be great if you add possibility to pass onBlur prop for handling this event.

I made PR

@kraaden
Copy link
Owner

kraaden commented Feb 8, 2023

Not sure if it make sense to include the functionality into the widget. This can easily be achieved by extending autocomplete in the following way:

function extendedAutocomplete(settings) {
	function handler(e) {
		console.log('blur');
	}
	settings.input.addEventListener('blur', handler);
	var result = autocomplete(settings);
	return {
		destroy: () => {
			settings.input.removeEventListener('blur', handler);
			result.destroy();
		}
	}
}

Then just call extendedAutocomplete({...}) instead of autocomplete({...}) and it will work exactly as in the pull request.

You can test it here

@3rd1t
Copy link

3rd1t commented Apr 4, 2023

there is a way to get the option with "selected" class on blur?

@kraaden
Copy link
Owner

kraaden commented Apr 4, 2023

@3rd1t The widget doesn't support this out of the box, so a workaround is needed to achieve something like this. You can use the code below to achieve it:

function extendedAutocomplete(settings) {
	function handler(e) {
		var selected = settings.container.querySelector('.selected').item;
		console.log('The selected item was: ' + selected.label);
	}

	settings.input.addEventListener('blur', handler);
	settings.container ||= document.createElement('div');
	settings.render = function render(item) {
		var itemElement = document.createElement('div');
		itemElement.textContent = item.label || '';
		itemElement.item = item;
		return itemElement;
	}
	var result = autocomplete(settings);
	return {
		destroy: () => {
			settings.input.removeEventListener('blur', handler);
			result.destroy();
		}
	}
}

https://jsbin.com/liyixoxelo/edit?html,js,output

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

No branches or pull requests

3 participants