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

82 lines
1.9 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
2021-02-11 14:03:22 +01:00
export let filter
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)
2021-02-11 14:03:22 +01:00
$: filteredRows = filterRows(rows, filter)
$: actions = [
{
type: ActionTypes.RefreshDatasource,
callback: () => fetchData(datasource),
metadata: { datasource },
},
]
2021-01-11 21:17:56 +01:00
2021-02-11 14:03:22 +01:00
const fetchData = async datasource => {
if (!isEmpty(datasource)) {
rows = await API.fetchDatasource(datasource)
2020-06-03 00:26:06 +02:00
}
loaded = true
}
2021-02-11 14:03:22 +01:00
const filterRows = (rows, filter) => {
if (!Object.keys(filter || {}).length) {
return rows
}
let filteredData = [...rows]
Object.keys(filter).forEach(field => {
filteredData = filteredData.filter(row => {
return row[field] === filter[field].value
})
})
return filteredData
}
2020-06-03 00:26:06 +02:00
</script>
<Provider {actions}>
<div use:styleable={$component.styles}>
2021-02-11 14:03:22 +01:00
{#if filteredRows.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}
2021-02-11 14:03:22 +01:00
{#each filteredRows 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>