Optional Mount Parameters (#123)
* adding optional parameters * updating bodyText mount prop to default * updating snapshot for new props * adding parent element for mounting * adding null default for parentElement * adding dist to gitignore * adding test case for mounting to element * adding div to document in test * unmount after test * updating test to pass * updating eslint to handle certain rules as warning and fix others
This commit is contained in:
committed by
Benjamin Boudreau
parent
d00d6af82a
commit
309bfd6a5b
@@ -6,8 +6,8 @@ import '../../vendor/cleanslate.css';
|
||||
export default class EmbeddableWidget {
|
||||
static el;
|
||||
|
||||
static mount() {
|
||||
const component = <Widget />;
|
||||
static mount({ parentElement = null, ...props } = {}) {
|
||||
const component = <Widget {...props} />;
|
||||
|
||||
function doRender() {
|
||||
if (EmbeddableWidget.el) {
|
||||
@@ -15,7 +15,12 @@ export default class EmbeddableWidget {
|
||||
}
|
||||
const el = document.createElement('div');
|
||||
el.setAttribute('class', 'cleanslate');
|
||||
document.body.appendChild(el);
|
||||
|
||||
if (parentElement) {
|
||||
document.querySelector(parentElement).appendChild(el);
|
||||
} else {
|
||||
document.body.appendChild(el);
|
||||
}
|
||||
ReactDOM.render(
|
||||
component,
|
||||
el,
|
||||
|
||||
@@ -21,6 +21,20 @@ describe('EmbeddableWidget', () => {
|
||||
await waitForSelection(document, 'div');
|
||||
});
|
||||
|
||||
test('#mount to document element', async () => {
|
||||
const newElement = document.createElement('span');
|
||||
newElement.setAttribute('id', 'widget-mount');
|
||||
document.body.appendChild(newElement);
|
||||
|
||||
EmbeddableWidget.mount({
|
||||
parentElement: '#widget-mount',
|
||||
});
|
||||
|
||||
await waitForSelection(document, 'div');
|
||||
|
||||
expect(document.querySelectorAll('#widget-mount')).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('#mount twice', async () => {
|
||||
EmbeddableWidget.mount();
|
||||
expect(() => EmbeddableWidget.mount()).toThrow('already mounted');
|
||||
|
||||
Reference in New Issue
Block a user