Skip to content

Instantly share code, notes, and snippets.

@heymarkreeves
Last active May 18, 2022 13:04
Show Gist options
  • Save heymarkreeves/1f973780c2d92d6996e73f120b4dcd8d to your computer and use it in GitHub Desktop.
Save heymarkreeves/1f973780c2d92d6996e73f120b4dcd8d to your computer and use it in GitHub Desktop.
// feSource/modules/services/inputUtils.js
export default class inputUtils {
// if we need any other params, add them to the constructor
constructor(el) {
this.el = el;
// if we need anything to fire at the document level, do it here:
document.addEventListener('keyup', function (e) {
const keyName = e.key;
if (keyName == 'Escape') {
// do stuff
}
}
}
this.el.on('focus', (e) => {
// do stuff
});
// or we could just have methods in here
// and if we instantiate this elsewhere as inputUtils, we could do:
// inputUtils.handleFocus(el);
handleFocus(el) {
// do stuff to el
}
}
// feSource/js/modules/fab-landing-page-search/search.js
import inputUtils from '../services/inputUtils';
class searchModule {
constructor(renderOptions) {
this.renderOptions = renderOptions;
this.el = $(renderOptions.container);
this.render(renderOptions);
this.els = {
latField: this.el.find('[data-js-hook="location-input-lat"]'),
lngField: this.el.find('[data-js-hook="location-input-lng"]'),
locationField: this.el.find('[data-js-hook="location-input-field"]'),
geoLocationButton: this.el.find('[data-js-hook="location-geolocation-button"]'),
searchSubmitButton: this.el.find('[data-js-hook="search-submit"]'),
brokerNameButton: this.el.find('[data-js-hook="broker-name-field-trigger"]'),
brokerNameField: this.el.find('[data-js-hook="broker-name-field"]'),
brokerNameFieldWrapper: this.el.find('[data-js-hook="broker-name-field-wrapper"]'),
};
const placesSearchInstance = new placesSearch(this.el.find('[data-js-hook="location-input-field"]')[0], {
onPlaceChanged: (instance) => {
savedAddress = instance.formattedAddress;
this.els.locationField.value = savedAddress;
this.els.locationField.parent().addClass('is-floating');
this.els.latField.value = instance.geo.lat;
this.els.lngField.value = instance.geo.lng;
},
});
// NEW:
// this will apply everything to our locationField
let inputUtils = new inputUtils(this.els.locationField);
// OR:
let inputUtils = new inputUtils();
inputUtils.handleFocus(this.els.locationField);
// feSource/js/search/support/widgets/location-input/location-input.js
import inputUtils from '../../../../modules/services/inputUtils';
const locationInput = connectConfigure((renderOptions, isFirstRender) => {
// ...
let inputUtils = new inputUtils($(widgetParams.container).find('[data-js-hook="location-input-field"]'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment