budibase/packages/builder/src/components/userInterface/PropertyControl.svelte

64 lines
1.3 KiB
Svelte
Raw Normal View History

2020-05-07 15:30:04 +02:00
<script>
2020-05-18 17:32:00 +02:00
import { onMount, getContext } from "svelte"
2020-05-07 15:30:04 +02:00
export let label = ""
export let control = null
export let key = ""
2020-05-21 15:28:32 +02:00
export let value
2020-05-07 15:30:04 +02:00
export let props = {}
export let onChange = () => {}
function handleChange(key, v) {
2020-05-20 12:55:25 +02:00
if (v.target) {
2020-05-21 15:28:32 +02:00
let val = props.valueKey ? v.target[props.valueKey] : v.target.value
2020-05-20 12:55:25 +02:00
onChange(key, val)
} else {
onChange(key, v)
}
}
2020-05-25 16:23:56 +02:00
const safeValue = () => {
return value === undefined && props.defaultValue !== undefined
? props.defaultValue
: value
}
2020-05-21 15:28:32 +02:00
//Incase the component has a different value key name
const handlevalueKey = value =>
2020-05-25 16:23:56 +02:00
props.valueKey ? { [props.valueKey]: safeValue() } : { value: safeValue() }
2020-05-07 15:30:04 +02:00
</script>
<div class="property-control">
<div class="label">{label}</div>
<div class="control">
<svelte:component
this={control}
2020-05-21 15:28:32 +02:00
{...handlevalueKey(value)}
on:change={val => handleChange(key, val)}
onChange={val => handleChange(key, val)}
2020-05-07 15:30:04 +02:00
{...props} />
</div>
</div>
<style>
.property-control {
display: flex;
flex-flow: row nowrap;
margin: 8px 0px;
}
.label {
flex: 0 0 50px;
padding: 0px 5px;
font-size: 12px;
font-weight: 500;
letter-spacing: 0.12px;
text-align: left;
}
.control {
flex: 1;
padding-left: 5px;
}
</style>