Checkboxgroup with builder specific logic

This commit is contained in:
Conor_Mack 2020-03-05 11:56:23 +00:00
parent 72a77a035e
commit 0676151c6d
6 changed files with 71 additions and 38 deletions

View File

@ -4,33 +4,42 @@
import { fieldStore } from "../Common/FormfieldStore.js" import { fieldStore } from "../Common/FormfieldStore.js"
import ClassBuilder from "../ClassBuilder.js" import ClassBuilder from "../ClassBuilder.js"
import { MDCCheckbox } from "@material/checkbox" import { MDCCheckbox } from "@material/checkbox"
import { generate } from "shortid"
export let onClick = item => {} export let onClick = item => {}
export let _bb export let _bb
export let id = "" export let id = ""
export let value = null
export let label = "" export let label = ""
export let disabled = false export let disabled = false
export let alignEnd = false export let alignEnd = false
export let indeterminate = false export let indeterminate = false
export let checked = false export let checked = false
let _id
let instance = null let instance = null
let checkbox = null let checkbox = null
let selectedItems
let context = _bb.getContext("BBMD:input:context") let context = _bb.getContext("BBMD:input:context")
onMount(() => { onMount(() => {
_id = generate()
if (!!checkbox) { if (!!checkbox) {
instance = new MDCCheckbox(checkbox) instance = new MDCCheckbox(checkbox)
instance.indeterminate = indeterminate instance.indeterminate = indeterminate
if (context !== "list-item") { if (context !== "list-item") {
//TODO: Fix this connected to Formfield context issue
let fieldStore = _bb.getContext("BBMD:field-element") let fieldStore = _bb.getContext("BBMD:field-element")
if(fieldStore) if (fieldStore) fieldStore.setInput(instance)
fieldStore.setInput(instance)
} }
} }
if (context === "checkboxgroup") {
selectedItems = _bb.getContext("BBMD:checkbox:selectedItemsStore")
}
}) })
let extras = null let extras = null
@ -52,6 +61,25 @@
_bb.setStateFromBinding(_bb.props.checked, val) _bb.setStateFromBinding(_bb.props.checked, val)
} }
} }
function handleOnClick() {
let item = { _id, label, value }
if (context === "checkboxgroup") {
let idx = selectedItems.getItemIdx($selectedItems, _id)
if (idx > -1) {
selectedItems.removeItem(_id)
} else {
selectedItems.addItem(item)
}
} else {
onClick(item)
}
}
$: isChecked =
context === "checkboxgroup"
? $selectedItems && $selectedItems.findIndex(i => i._id === _id) > -1
: checked
</script> </script>
<!-- TODO: Customizing Colour and Density - What level of customization for these things does Budibase need here? --> <!-- TODO: Customizing Colour and Density - What level of customization for these things does Budibase need here? -->
@ -64,8 +92,8 @@
class={cb.elem`native-control`} class={cb.elem`native-control`}
{id} {id}
{disabled} {disabled}
{checked} {isChecked}
on:click={onClick} on:click={handleOnClick}
on:change={changed} /> on:change={changed} />
<div class={cb.elem`background`}> <div class={cb.elem`background`}>
<svg class={cb.elem`checkmark`} viewBox="0 0 24 24"> <svg class={cb.elem`checkmark`} viewBox="0 0 24 24">
@ -86,9 +114,9 @@
class={cb.elem`native-control`} class={cb.elem`native-control`}
{id} {id}
{disabled} {disabled}
{checked} {isChecked}
on:change={changed} on:change={changed}
on:click={onClick} /> on:click={handleOnClick} />
<div class={cb.elem`background`}> <div class={cb.elem`background`}>
<svg class={cb.elem`checkmark`} viewBox="0 0 24 24"> <svg class={cb.elem`checkmark`} viewBox="0 0 24 24">
<path <path

View File

@ -1,26 +1,29 @@
<script> <script>
import { onMount } from "svelte"
import Checkbox from "./Checkbox.svelte" import Checkbox from "./Checkbox.svelte"
import Label from "../Common/Label.svelte" import Label from "../Common/Label.svelte"
import createItemsStore from "../Common/ItemStore.js"
let selectedItemsStore
let checkItems
export let _bb
export let label = "" export let label = ""
export let orientation = "row" export let orientation = "row"
export let fullwidth = false export let fullwidth = false
export let onChange = selectedItems => {} export let onChange = selectedItems => {}
export let items = []
export let disabled = false export let disabled = false
export let alignEnd = false export let alignEnd = false
let selectedItems = [] let selectedItems = []
function handleonChange(item) { onMount(() => {
if (!!item.checked) { _bb.setContext("BBMD:input:context", "checkboxgroup")
item.checked = !item.checked selectedItemsStore = createItemsStore(() => onChange($selectedItemsStore))
} else { _bb.setContext("BBMD:checkbox:selectedItemsStore", selectedItemsStore)
item.checked = true })
}
onChange(items.filter(i => i.checked)) $: checkItems && _bb.attachChildren(checkItems)
}
</script> </script>
<div class="checkbox-group"> <div class="checkbox-group">
@ -28,18 +31,7 @@
<Label text={label} bold /> <Label text={label} bold />
</div> </div>
<div class={`checkbox-group__boxes ${orientation}`}> <div class={`checkbox-group__boxes ${orientation}`}>
{#each items as item, i} <div bind:this={checkItems} class:fullwidth />
<div class:fullwidth>
<Checkbox
id={`${item.label}-${i}`}
{disabled}
{alignEnd}
indeterminate={item.indeterminate || false}
label={item.label}
checked={item.checked || false}
onClick={() => handleonChange(item)} />
</div>
{/each}
</div> </div>
</div> </div>

View File

@ -45,7 +45,6 @@
if (context === "radiobuttongroup") { if (context === "radiobuttongroup") {
selectedItems.addSingleItem(item) selectedItems.addSingleItem(item)
} else { } else {
debugger
onClick(item) onClick(item)
} }
} }

View File

@ -29,7 +29,7 @@
// item.checked = true // item.checked = true
// selected = item // selected = item
// items = items // items = items
// onChange(selected) // onChange(selected)w
// } // }
$: alignRight = orientation === "column" && alignEnd $: alignRight = orientation === "column" && alignEnd

View File

@ -40,6 +40,8 @@
Select, Select,
Radiobutton, Radiobutton,
Radiobuttongroup, Radiobuttongroup,
Checkbox,
Checkboxgroup,
], ],
}, },
} }

View File

@ -82,14 +82,26 @@ export const props = {
}, },
Checkboxgroup: { Checkboxgroup: {
_component: "@budibase/materialdesign-components/Checkboxgroup", _component: "@budibase/materialdesign-components/Checkboxgroup",
_children: [],
label: "Whats your favourite?", label: "Whats your favourite?",
items: [
{ label: "Currys", indeterminate: true },
{ label: "Chips", checked: true },
{ label: "Pasties" },
],
onChange: selectedItems => console.log(selectedItems), onChange: selectedItems => console.log(selectedItems),
_children: [
{
_component: "@budibase/materialdesign-components/Checkbox",
_children: [],
label: "Currys",
indeterminate: true,
},
{
_component: "@budibase/materialdesign-components/Checkbox",
_children: [],
label: "Chips",
},
{
_component: "@budibase/materialdesign-components/Checkbox",
_children: [],
label: "Pasties",
},
],
}, },
Radiobutton: { Radiobutton: {
_component: "@budibase/materialdesign-components/Radiobutton", _component: "@budibase/materialdesign-components/Radiobutton",
@ -188,5 +200,5 @@ export const props = {
value: "2", value: "2",
}, },
], ],
}, }
} }