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

128 lines
3.1 KiB
Svelte
Raw Normal View History

2021-02-01 22:02:54 +01:00
<script>
import { getContext } from "svelte"
import { isEmpty } from "lodash/fp"
2021-02-06 13:31:12 +01:00
import {
Button,
DatePicker,
Label,
Select,
Toggle,
Input,
} from "@budibase/bbui"
2021-02-01 22:02:54 +01:00
const { API, styleable, DataProvider, builderStore } = getContext("sdk")
const component = getContext("component")
2021-02-05 00:17:49 +01:00
export let table = []
export let columns = []
export let pageSize = 50
export let noRowsMessage = "Feed me some data"
2021-02-01 22:02:54 +01:00
let rows = []
let loaded = false
2021-02-05 00:17:49 +01:00
// let searchableFields = []
2021-02-01 22:02:54 +01:00
let search = {}
2021-02-05 00:17:49 +01:00
let tableDefinition
let schema = {}
2021-02-06 13:31:12 +01:00
let page = 1
2021-02-01 22:02:54 +01:00
2021-02-05 00:17:49 +01:00
$: columns = Object.keys(schema).filter(key => schema[key].searchable)
2021-02-06 13:31:12 +01:00
$: cursor = rows[rows.length - 1]?._id
$: page && fetchData(table)
2021-02-01 22:02:54 +01:00
2021-02-05 00:17:49 +01:00
async function fetchData(table) {
if (!isEmpty(table)) {
const tableDef = await API.fetchTableDefinition(table)
schema = tableDef.schema
rows = await API.searchTable({
tableId: table,
search,
pageSize,
2021-02-06 13:31:12 +01:00
cursor,
2021-02-01 22:02:54 +01:00
})
}
loaded = true
}
2021-02-06 13:31:12 +01:00
2021-02-01 22:02:54 +01:00
</script>
<div use:styleable={$component.styles}>
<div class="query-builder">
2021-02-05 00:17:49 +01:00
{#each columns as field}
2021-02-01 22:02:54 +01:00
<div class="form-field">
<Label extraSmall grey>{schema[field].name}</Label>
{#if schema[field].type === 'options'}
<Select secondary bind:value={search[field]}>
<option value="">Choose an option</option>
{#each schema[field].constraints.inclusion as opt}
<option>{opt}</option>
{/each}
</Select>
2021-02-06 13:31:12 +01:00
{:else if schema[field].type === 'datetime'}
<DatePicker bind:value={search[field]} />
2021-02-01 22:02:54 +01:00
{:else if schema[field].type === 'boolean'}
2021-02-05 00:17:49 +01:00
<Toggle text={schema[field].name} bind:checked={search[field]} />
2021-02-01 22:02:54 +01:00
{:else if schema[field].type === 'number'}
<Input type="number" bind:value={search[field]} />
{:else if schema[field].type === 'string'}
<Input bind:value={search[field]} />
{/if}
</div>
{/each}
2021-02-05 00:17:49 +01:00
<Button blue on:click={() => fetchData(table)}>Search</Button>
<Button
red
on:click={() => {
search = {}
fetchData(table)
}}>
Reset
</Button>
2021-02-01 22:02:54 +01:00
</div>
{#if rows.length > 0}
{#if $component.children === 0 && $builderStore.inBuilder}
<p>Add some components too</p>
{:else}
{#each rows as row}
<DataProvider {row}>
<slot />
</DataProvider>
{/each}
{/if}
{:else if loaded && $builderStore.inBuilder}
2021-02-05 00:17:49 +01:00
<p>{noRowsMessage}</p>
2021-02-01 22:02:54 +01:00
{/if}
2021-02-06 13:31:12 +01:00
<div class="pagination">
{#if page > 1}
<Button blue on:click={() => (page -= 1)}>Back</Button>
{/if}
<Button blue on:click={() => (page += 1)}>Next</Button>
</div>
2021-02-01 22:02:54 +01:00
</div>
<style>
p {
display: grid;
place-items: center;
background: #f5f5f5;
border: #ccc 1px solid;
padding: var(--spacing-m);
}
.query-builder {
padding: var(--spacing-m);
background: var(--background);
border-radius: var(--border-radius-s);
}
.form-field {
margin-bottom: var(--spacing-m);
}
2021-02-06 13:31:12 +01:00
.pagination {
display: flex;
justify-content: flex-end;
margin-top: var(--spacing-m);
}
2021-02-01 22:02:54 +01:00
</style>