budibase/packages/standard-components/src/forms/BooleanField.svelte

52 lines
948 B
Svelte
Raw Normal View History

<script>
import { CoreCheckbox } from "@budibase/bbui"
import Field from "./Field.svelte"
export let field
export let label
export let text
export let disabled = false
export let size
export let validation
2021-08-04 15:32:58 +02:00
export let defaultValue
let fieldState
let fieldApi
2021-08-04 15:32:58 +02:00
const isTruthy = value => {
if (!value) {
return false
}
if (value === true) {
return true
}
if (typeof value === "string" && value.toLowerCase() === "true") {
return true
}
return false
}
</script>
<Field
{label}
{field}
{disabled}
{validation}
2021-08-04 15:32:58 +02:00
defaultValue={isTruthy(defaultValue)}
type="boolean"
bind:fieldState
bind:fieldApi
>
{#if fieldState}
<CoreCheckbox
value={fieldState.value}
disabled={fieldState.disabled}
error={fieldState.error}
id={fieldState.fieldId}
2021-08-03 21:36:42 +02:00
{size}
2021-05-04 12:32:22 +02:00
on:change={e => fieldApi.setValue(e.detail)}
{text}
/>
{/if}
</Field>