Skip to content

Commit

Permalink
feat(search): add createSearchAppInit utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
mirekys committed Jan 26, 2024
1 parent ef0bcdc commit 2355c25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { SearchAppResults } from "./SearchAppResults";
export { EmptyResultsElement } from "./EmptyResultsElement";
export { ResultsPerPageLabel } from "./ResultsPerPageLabel";
export { ClearFiltersButton } from "./ClearFiltersButton";
export * from './util'
46 changes: 46 additions & 0 deletions oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/search/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from "react";
import {
SearchApp
} from "@js/invenio_search_ui/components";
import { loadComponents } from "@js/invenio_theme/templates";
import _camelCase from "lodash/camelCase";
import ReactDOM from "react-dom";


export function createSearchAppInit ({
defaultComponentOverrides,
autoInit = true,
autoInitDataAttr = "invenio-search-config",
multi = false,
ContainerComponent = React.Fragment
}) {
const initSearchApp = (rootElement) => {
const { appId, ...config } = JSON.parse(
rootElement.dataset[_camelCase(autoInitDataAttr)]
);

loadComponents(appId, { ...config.defaultComponents, ...defaultComponentOverrides }).then((res) => {
ReactDOM.render(
<ContainerComponent>
<SearchApp
config={config}
// Use appName to namespace application components when overriding
{...(multi && { appName: appId })}
/>
</ContainerComponent>,
rootElement
);
});
};

if (autoInit) {
const searchAppElements = document.querySelectorAll(
`[data-${autoInitDataAttr}]`
);
for (const appRootElement of searchAppElements) {
initSearchApp(appRootElement);
}
} else {
return initSearchApp;
}
}

0 comments on commit 2355c25

Please sign in to comment.