From 0676151c6d6f1372e9189e263b4db724e5945b26 Mon Sep 17 00:00:00 2001 From: Conor_Mack Date: Thu, 5 Mar 2020 11:56:23 +0000 Subject: [PATCH] Checkboxgroup with builder specific logic --- .../src/Checkbox/Checkbox.svelte | 42 +++++++++++++++---- .../src/Checkbox/CheckboxGroup.svelte | 36 +++++++--------- .../src/Radiobutton/Radiobutton.svelte | 1 - .../src/Radiobutton/RadiobuttonGroup.svelte | 2 +- .../src/Test/TestApp.svelte | 2 + .../src/Test/props.js | 26 ++++++++---- 6 files changed, 71 insertions(+), 38 deletions(-) diff --git a/packages/materialdesign-components/src/Checkbox/Checkbox.svelte b/packages/materialdesign-components/src/Checkbox/Checkbox.svelte index ceced6af9c..eb7939a784 100644 --- a/packages/materialdesign-components/src/Checkbox/Checkbox.svelte +++ b/packages/materialdesign-components/src/Checkbox/Checkbox.svelte @@ -4,33 +4,42 @@ import { fieldStore } from "../Common/FormfieldStore.js" import ClassBuilder from "../ClassBuilder.js" import { MDCCheckbox } from "@material/checkbox" + import { generate } from "shortid" export let onClick = item => {} export let _bb export let id = "" + export let value = null export let label = "" export let disabled = false export let alignEnd = false export let indeterminate = false export let checked = false + let _id let instance = null let checkbox = null + let selectedItems + let context = _bb.getContext("BBMD:input:context") onMount(() => { + _id = generate() + if (!!checkbox) { instance = new MDCCheckbox(checkbox) instance.indeterminate = indeterminate if (context !== "list-item") { - //TODO: Fix this connected to Formfield context issue let fieldStore = _bb.getContext("BBMD:field-element") - if(fieldStore) - fieldStore.setInput(instance) + if (fieldStore) fieldStore.setInput(instance) } } + + if (context === "checkboxgroup") { + selectedItems = _bb.getContext("BBMD:checkbox:selectedItemsStore") + } }) let extras = null @@ -52,6 +61,25 @@ _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 @@ -64,8 +92,8 @@ class={cb.elem`native-control`} {id} {disabled} - {checked} - on:click={onClick} + {isChecked} + on:click={handleOnClick} on:change={changed} />
@@ -86,9 +114,9 @@ class={cb.elem`native-control`} {id} {disabled} - {checked} + {isChecked} on:change={changed} - on:click={onClick} /> + on:click={handleOnClick} />
+ import { onMount } from "svelte" import Checkbox from "./Checkbox.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 orientation = "row" export let fullwidth = false export let onChange = selectedItems => {} - export let items = [] - export let disabled = false export let alignEnd = false let selectedItems = [] - function handleonChange(item) { - if (!!item.checked) { - item.checked = !item.checked - } else { - item.checked = true - } - onChange(items.filter(i => i.checked)) - } + onMount(() => { + _bb.setContext("BBMD:input:context", "checkboxgroup") + selectedItemsStore = createItemsStore(() => onChange($selectedItemsStore)) + _bb.setContext("BBMD:checkbox:selectedItemsStore", selectedItemsStore) + }) + + $: checkItems && _bb.attachChildren(checkItems)
@@ -28,18 +31,7 @@
- {#each items as item, i} -
- handleonChange(item)} /> -
- {/each} +
diff --git a/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte b/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte index 9916b0f0f1..0ed0902322 100644 --- a/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte +++ b/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte @@ -45,7 +45,6 @@ if (context === "radiobuttongroup") { selectedItems.addSingleItem(item) } else { - debugger onClick(item) } } diff --git a/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte b/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte index ca9ca66618..b72c043e6a 100644 --- a/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte +++ b/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte @@ -29,7 +29,7 @@ // item.checked = true // selected = item // items = items - // onChange(selected) + // onChange(selected)w // } $: alignRight = orientation === "column" && alignEnd diff --git a/packages/materialdesign-components/src/Test/TestApp.svelte b/packages/materialdesign-components/src/Test/TestApp.svelte index 03976d2ff0..ebb478d858 100644 --- a/packages/materialdesign-components/src/Test/TestApp.svelte +++ b/packages/materialdesign-components/src/Test/TestApp.svelte @@ -40,6 +40,8 @@ Select, Radiobutton, Radiobuttongroup, + Checkbox, + Checkboxgroup, ], }, } diff --git a/packages/materialdesign-components/src/Test/props.js b/packages/materialdesign-components/src/Test/props.js index aa55c680fb..5d01e1e43d 100644 --- a/packages/materialdesign-components/src/Test/props.js +++ b/packages/materialdesign-components/src/Test/props.js @@ -82,14 +82,26 @@ export const props = { }, Checkboxgroup: { _component: "@budibase/materialdesign-components/Checkboxgroup", - _children: [], label: "Whats your favourite?", - items: [ - { label: "Currys", indeterminate: true }, - { label: "Chips", checked: true }, - { label: "Pasties" }, - ], 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: { _component: "@budibase/materialdesign-components/Radiobutton", @@ -188,5 +200,5 @@ export const props = { value: "2", }, ], - }, + } }