-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): add createSearchAppInit utility function
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/search/util.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |