budibase/packages/builder/src/components/integration/KeyValueBuilder.svelte

99 lines
2.3 KiB
Svelte
Raw Normal View History

2021-02-15 19:41:56 +01:00
<script>
2021-12-06 18:39:51 +01:00
import {
Icon,
ActionButton,
Input,
Label,
Toggle,
Select,
} from "@budibase/bbui"
import { createEventDispatcher } from "svelte"
import { lowercase } from "helpers"
let dispatch = createEventDispatcher()
2021-02-15 19:41:56 +01:00
2021-02-24 17:31:47 +01:00
export let defaults
export let object = defaults || {}
2021-02-19 13:07:37 +01:00
export let readOnly
2021-09-29 14:02:30 +02:00
export let noAddButton
export let name
export let headings = false
export let activity = false
2021-12-06 18:39:51 +01:00
export let options
2021-02-15 19:41:56 +01:00
let fields = Object.entries(object).map(([name, value]) => ({ name, value }))
2021-02-15 19:41:56 +01:00
$: object = fields.reduce(
(acc, next) => ({ ...acc, [next.name]: next.value }),
{}
)
2021-09-29 14:02:30 +02:00
export function addEntry() {
2021-12-02 12:32:21 +01:00
fields = [...fields, { name: "", value: "" }]
changed()
2021-02-15 19:41:56 +01:00
}
function deleteEntry(idx) {
fields.splice(idx, 1)
changed()
}
function changed() {
2021-02-15 19:41:56 +01:00
fields = fields
dispatch("change", fields)
2021-02-15 19:41:56 +01:00
}
</script>
<!-- Builds Objects with Key Value Pairs. Useful for building things like Request Headers. -->
2021-12-01 14:09:16 +01:00
{#if Object.keys(object || {}).length > 0}
{#if headings}
<div class="container" class:container-active={activity}>
<Label>Key</Label>
<Label>Value</Label>
{#if activity}
<Label>Active</Label>
{/if}
</div>
{/if}
<div class="container" class:container-active={activity} class:readOnly>
2021-12-01 14:09:16 +01:00
{#each fields as field, idx}
<Input placeholder="Key" bind:value={field.name} on:change={changed} />
2021-12-06 18:39:51 +01:00
{#if options}
<Select bind:value={field.value} on:change={changed} {options} />
{:else}
<Input
placeholder="Value"
bind:value={field.value}
on:change={changed}
/>
{/if}
{#if activity}
<Toggle />
{/if}
2021-12-01 14:09:16 +01:00
{#if !readOnly}
<Icon hoverable name="Close" on:click={() => deleteEntry(idx)} />
{/if}
{/each}
</div>
{/if}
2021-09-29 14:02:30 +02:00
{#if !readOnly && !noAddButton}
2021-05-04 10:55:14 +02:00
<div>
<ActionButton icon="Add" secondary thin outline on:click={addEntry}
>Add{name ? ` ${lowercase(name)}` : ""}</ActionButton
>
2021-05-04 10:55:14 +02:00
</div>
2021-02-19 13:07:37 +01:00
{/if}
2021-02-15 19:41:56 +01:00
<style>
.container {
display: grid;
grid-template-columns: 1fr 1fr 20px;
grid-gap: var(--spacing-m);
align-items: center;
2021-02-15 19:59:21 +01:00
margin-bottom: var(--spacing-m);
2021-02-15 19:41:56 +01:00
}
.container-active {
grid-template-columns: 1fr 1fr 50px 20px;
}
2021-02-15 19:41:56 +01:00
</style>