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

67 lines
1.5 KiB
Svelte
Raw Normal View History

2020-06-03 00:26:06 +02:00
<script>
import { getContext } from "svelte"
import { isEmpty } from "lodash/fp"
export let datasource
export let noRowsMessage
const { API, styleable, Provider, builderStore, ActionTypes } = getContext(
"sdk"
)
const component = getContext("component")
2020-11-18 12:24:01 +01:00
let rows = []
let loaded = false
2020-06-03 00:26:06 +02:00
$: fetchData(datasource)
$: actions = [
{
type: ActionTypes.RefreshDatasource,
callback: () => fetchData(datasource),
metadata: { datasource },
},
]
2021-01-11 21:17:56 +01:00
async function fetchData(datasource) {
if (!isEmpty(datasource)) {
rows = await API.fetchDatasource(datasource)
2020-06-03 00:26:06 +02:00
}
loaded = true
}
2020-06-03 00:26:06 +02:00
</script>
<Provider {actions}>
<div use:styleable={$component.styles}>
{#if rows.length > 0}
{#if $component.children === 0 && $builderStore.inBuilder}
2021-02-11 10:20:25 +01:00
<p><i class="ri-image-line" />Add some components to display.</p>
{:else}
{#each rows as row}
<Provider data={row}>
<slot />
</Provider>
{/each}
{/if}
{:else if loaded && noRowsMessage}
<p><i class="ri-list-check-2" />{noRowsMessage}</p>
{/if}
</div>
</Provider>
<style>
p {
margin: 0 var(--spacing-m);
background-color: var(--grey-2);
color: var(--grey-6);
font-size: var(--font-size-s);
padding: var(--spacing-l);
border-radius: var(--border-radius-s);
display: grid;
place-items: center;
}
p i {
margin-bottom: var(--spacing-m);
font-size: 1.5rem;
color: var(--grey-5);
}
</style>