budibase/packages/builder/src/userInterface/PropertyCascader.svelte

147 lines
3.2 KiB
Svelte
Raw Normal View History

2020-02-10 17:58:20 +01:00
<script>
import { ArrowDownIcon } from "../common/Icons/"
import { store } from "../builderStore"
import { buildStateOrigins } from "../builderStore/buildStateOrigins"
import { isBinding, getBinding, setBinding } from "../common/binding"
2020-02-10 17:58:20 +01:00
export let onChanged = () => {}
export let value = ""
2020-02-10 17:58:20 +01:00
let isOpen = false
let stateBindings = []
2020-02-10 17:58:20 +01:00
let bindingPath = ""
let bindingFallbackValue = ""
let bindingSource = "store"
let bindingValue = ""
2020-02-10 17:58:20 +01:00
const bind = (path, fallback, source) => {
if (!path) {
onChanged(fallback)
return
2020-02-10 17:58:20 +01:00
}
const binding = setBinding({ path, fallback, source })
onChanged(binding)
}
2020-02-10 17:58:20 +01:00
2020-02-10 18:11:22 +01:00
const setBindingPath = value =>
bind(value, bindingFallbackValue, bindingSource)
2020-02-10 17:58:20 +01:00
const setBindingFallback = value => bind(bindingPath, value, bindingSource)
2020-02-10 17:58:20 +01:00
const setBindingSource = value =>
bind(bindingPath, bindingFallbackValue, value)
2020-02-10 17:58:20 +01:00
$: {
const binding = getBinding(value)
if (bindingPath !== binding.path) isOpen = false
bindingPath = binding.path
bindingValue = typeof value === "object" ? "" : value
bindingFallbackValue = binding.fallback || bindingValue
2020-02-12 11:12:08 +01:00
2020-02-10 17:58:20 +01:00
const currentScreen = $store.screens.find(
2020-02-11 17:36:16 +01:00
({ name }) => name === $store.currentPreviewItem.name
)
stateBindings = currentScreen
? Object.keys(buildStateOrigins(currentScreen))
: []
2020-02-10 17:58:20 +01:00
}
</script>
<div class="cascader">
<div class="input-box">
<input
2020-02-12 11:12:08 +01:00
class:bold={!bindingFallbackValue && bindingPath}
2020-02-10 17:58:20 +01:00
class="uk-input uk-form-small"
2020-02-12 11:12:08 +01:00
value={bindingFallbackValue || bindingPath}
2020-02-10 18:11:22 +01:00
on:change={e => {
setBindingFallback(e.target.value)
onChanged(e.target.value)
2020-02-10 18:11:22 +01:00
}} />
{#if stateBindings.length}
<button on:click={() => (isOpen = !isOpen)}>
<div
class="icon"
class:highlighted={bindingPath}
style={`transform: rotate(${isOpen ? 0 : 90}deg);`}>
<ArrowDownIcon size={36} />
</div>
</button>
{/if}
2020-02-10 17:58:20 +01:00
</div>
{#if isOpen}
<ul class="options">
{#each stateBindings as stateBinding}
2020-02-10 17:58:20 +01:00
<li
2020-02-12 11:12:08 +01:00
class:bold={stateBinding === bindingPath}
2020-02-10 17:58:20 +01:00
on:click={() => {
setBindingPath(stateBinding === bindingPath ? null : stateBinding)
2020-02-10 17:58:20 +01:00
}}>
{stateBinding}
</li>
{/each}
</ul>
{/if}
</div>
<style>
2020-02-12 11:12:08 +01:00
.bold {
font-weight: bold;
}
.highlighted {
color: rgba(0, 85, 255, 0.8);
}
2020-02-10 17:58:20 +01:00
button {
cursor: pointer;
outline: none;
border: none;
border-radius: 5px;
background: rgba(249, 249, 249, 1);
font-size: 1.6rem;
font-weight: 700;
color: rgba(22, 48, 87, 1);
}
.cascader {
position: relative;
width: 100%;
}
.input-box {
display: flex;
align-items: center;
}
.options {
width: 172px;
margin: 0;
position: absolute;
top: 35px;
padding: 10px;
z-index: 1;
background: rgba(249, 249, 249, 1);
min-height: 50px;
border-radius: 2px;
}
li {
2020-02-10 22:04:11 +01:00
list-style-type: none;
2020-02-10 17:58:20 +01:00
}
li:hover {
cursor: pointer;
2020-02-10 22:04:11 +01:00
font-weight: bold;
2020-02-10 17:58:20 +01:00
}
input {
margin-right: 5px;
border: 1px solid #dbdbdb;
border-radius: 2px;
opacity: 0.5;
height: 40px;
}
</style>