budibase/packages/standard-components/src/Div.svelte

66 lines
1.3 KiB
Svelte
Raw Normal View History

2019-10-14 09:32:20 +02:00
<script>
export let children = [];
export let className="";
export let data=[];
export let dataItemComponent;
export let onLoad;
export let _bb;
let rootDiv;
let staticComponentsApplied=false;
let dataBoundComponents = [];
let previousData;
let onLoadCalled = false;
const hasData = () =>
Array.isArray(data) && data.length > 0;
$: {
2019-10-18 18:32:03 +02:00
if(rootDiv) {
if(children && children.length > 0 && !staticComponentsApplied) {
for(let child of children) {
_bb.appendComponent(
child.component,
rootDiv);
}
staticComponentsApplied = true;
2019-10-14 09:32:20 +02:00
}
2019-10-18 18:32:03 +02:00
2019-10-14 09:32:20 +02:00
2019-10-18 18:32:03 +02:00
if(previousData !== data) {
2019-10-14 09:32:20 +02:00
2019-10-18 18:32:03 +02:00
for(let c of dataBoundComponents) {
dataBoundComponents[c].$destroy();
}
dataBoundComponents = [];
2019-10-14 09:32:20 +02:00
2019-10-18 18:32:03 +02:00
if(hasData()) {
let index = 0;
for(let dataItem of data) {
_bb.appendComponent(
dataItemComponent,
rootDiv,
dataItem
);
}
2019-10-14 09:32:20 +02:00
}
}
2019-10-18 18:32:03 +02:00
if(!onLoadCalled && onLoad && !onLoad.isPlaceholder) {
_bb.call(onLoad);
onLoadCalled = true;
}
2019-10-14 09:32:20 +02:00
}
}
</script>
<div class="{className}" bind:this={rootDiv}>
</div>