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

Publicly expose a method for opening Instant Results. #2987

Merged
merged 14 commits into from
Oct 12, 2022
Next Next commit
WIP: Open Instant Results with an action.
  • Loading branch information
JakePT committed Sep 6, 2022
commit 378fc869fa6a7dac1bab77fbf3f2f2bb8858c9c6
54 changes: 43 additions & 11 deletions assets/js/instant-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useRef,
WPElement,
} from '@wordpress/element';
import { addAction, doAction, removeAction } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -165,6 +166,15 @@ const App = () => {
dispatch({ type: 'APPLY_ARGS', payload: { search, post_type } });
}, []);

/**
* Handle open action.
*
* @param {Event} event Input event.
*/
const onOpen = useCallback((args) => {
dispatch({ type: 'APPLY_ARGS', payload: args });
}, []);

/**
* Handle changes to search parameters.
*/
Expand All @@ -186,20 +196,13 @@ const App = () => {
* @returns {Function} A cleanup function that unbinds the events.
*/
const handleEvents = () => {
const inputs = document.querySelectorAll('form input[type="search"');
const modal = modalRef.current;

inputs.forEach((input) => {
input.form.addEventListener('submit', onSubmit);
});

addAction('ep.InstantResults.open', 'ep/onOpenInstantResults', onOpen);
modal.ownerDocument.defaultView.addEventListener('popstate', onPopState);

return () => {
inputs.forEach((input) => {
input.form.removeEventListener('submit', onSubmit);
});

removeAction('ep.InstantResults.open', 'ep/onOpenInstantResults');
modal.ownerDocument.defaultView.removeEventListener('popstate', onPopState);
};
};
Expand All @@ -220,7 +223,7 @@ const App = () => {
* Effects.
*/
useEffect(handleInit, []);
useEffect(handleEvents, [onEscape, onPopState, onSubmit]);
useEffect(handleEvents, [onEscape, onOpen, onPopState]);
useEffect(handleChanges, [
doSearch,
pushState,
Expand Down Expand Up @@ -250,4 +253,33 @@ const App = () => {
);
};

render(<App />, document.getElementById('ep-instant-results'));
/**
* Handle submission of search forms.
*
* @param {Event} event Submit.
*/
const onSubmit = (event) => {
event.preventDefault();

const search = event.currentTarget.s.value;
const post_type = getPostTypesFromForm(event.currentTarget);

doAction('ep.InstantResults.open', { search, post_type });
};

/**
* Initialize Instant Results.
*
* @returns {void}
*/
const init = () => {
const inputs = document.querySelectorAll('form input[name="s"');

inputs.forEach((input) => {
input.form.addEventListener('submit', onSubmit);
});

render(<App />, document.getElementById('ep-instant-results'));
};

window.addEventListener('DOMContentLoaded', init);