question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose Internal Methods and Properties

See original GitHub issue

I’ve hit an issue where I need to programmatically control other Autocomplete instances on the same page, but (at least from my understanding) this is impossible.

Having an initiation function like this, is great:

autocomplete({
  element: document.querySelector( '' ),
  source: [ 'option', 'option-1', 'options-2']
})

But there is no way to reference this input after it initiates. How hard would it be to have this return the input instance? i.e.

const input = autocomplete({
  element: document.querySelector( '' ),
  source: [ 'option', 'option-1', 'options-2']
})

// Later
input.hide();

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
walkermattcommented, Dec 31, 2021

@JakeChampion this change to the accessibleAutocomplete function returns the component instance.

1reaction
JakeChampioncommented, Apr 15, 2021

Another good use-case I have come across for returning the component from the wrapper module:

https://github.com/alphagov/accessible-autocomplete/blob/935f0d43aea1c606e6b38985e3fe7049ddbe98be/src/wrapper.js#L4

We wanted to implement a button to clear and re-focus the input field, this currently has a sub-optimal user-experience because it requires aligning to some timers internal to accessible-autocomplete. If we reset the input and then re-focus the input before the internal timer has fired, then the input’s value reverts to what it was before being reset. Those timers are there to integrate with Dragon NaturallySpeaking – https://github.com/alphagov/accessible-autocomplete/blob/935f0d43aea1c606e6b38985e3fe7049ddbe98be/src/autocomplete.js#L107-L125

If the component was returned, we would be able to call component.getDirectInputChanges() to update the internal state of the component and then immediately focus on the input, and have it work correctly.

This is the code we have to have right now to reset and re-focus the input:

let timeout = null;
clearButton.addEventListener('click', function clearInput() {
	input.value = '';
	clearButton.remove();
	// We need to wait longer than 100ms before focusing
	// onto the input element because accessible-autocomplete
	// only checks the value of the input every 100ms.
	// If we modify input.value and then focus into the input in less
	// than 100ms, accessible-autocomplete would not have the updated
	// value and would instead write the old value back into the input.
	// https://github.com/alphagov/accessible-autocomplete/blob/935f0d43aea1c606e6b38985e3fe7049ddbe98be/src/autocomplete.js#L107-L125
	if (!timeout) {
		// The user could press the button multiple times
		// whilst the setTimeout handler has yet to execute
		// We only want to call the handler once
		timeout = setTimeout(() => {
			input.focus();
			timeout = null;
		}, 110);
	}
});

If we had access to the component instance, we could refactor the above into:

clearButton.addEventListener('click', function clearInput() {
	input.value = '';
	clearButton.remove();
	// We need to update the internal state of accessible-autocomplete
	// with the new value in the input
	component.getDirectInputChanges();
	input.focus();
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose public properties of internal types - MSDN - Microsoft
Is it possible to expose one public class like this that exposes an instance of some internal classes? namespace MyNamespace
Read more >
Access to internal classes from another project - Stack Overflow
In Settings class I store internal variables (and methods) for app which cannot be visible from other projects. But those settings need to...
Read more >
Private and protected properties and methods
Internal interface – methods and properties, accessible from other methods of the class, but not from the outside.
Read more >
Hiding internal framework methods and properties from web ...
In hindsight, exposing framework internals that way (even prefixed with an underscore, etc.) seems like a bad idea:
Read more >
How to unit test Internal classes in .NET Core applications?
The creation of unit tests is an important step in ensuring the quality of a project. In general, most public methods are tested,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found