From c605d49949a81f2354479060fe65064d249390ae Mon Sep 17 00:00:00 2001 From: Conor_Mack Date: Wed, 4 Mar 2020 16:35:52 +0000 Subject: [PATCH] Radiobutton Group update to work with builder specific logic --- .../src/Common/ItemStore.js | 8 ++-- .../src/List/List.svelte | 2 - .../src/Radiobutton/Radiobutton.svelte | 28 ++++++++++- .../src/Radiobutton/RadiobuttonGroup.svelte | 47 ++++++++++--------- .../src/Select/Select.svelte | 1 - .../src/Test/TestApp.svelte | 2 + .../src/Test/props.js | 28 ++++++++--- 7 files changed, 78 insertions(+), 38 deletions(-) diff --git a/packages/materialdesign-components/src/Common/ItemStore.js b/packages/materialdesign-components/src/Common/ItemStore.js index c523405dad..cf0b7de24e 100644 --- a/packages/materialdesign-components/src/Common/ItemStore.js +++ b/packages/materialdesign-components/src/Common/ItemStore.js @@ -1,14 +1,14 @@ -import { writable } from "svelte/store"; +import { writable } from "svelte/store" function createItemsStore(componentOnSelect) { - const { subscribe, set, update } = writable([]); + const { subscribe, set, update } = writable([]) function addItem(item) { update(items => { - return [...items, item] + return [...items, item] }) if (componentOnSelect) { - componentOnSelect(); + componentOnSelect() } } diff --git a/packages/materialdesign-components/src/List/List.svelte b/packages/materialdesign-components/src/List/List.svelte index eae7018d1b..d83ae15053 100644 --- a/packages/materialdesign-components/src/List/List.svelte +++ b/packages/materialdesign-components/src/List/List.svelte @@ -6,8 +6,6 @@ import ListItem from "./ListItem.svelte" import ClassBuilder from "../ClassBuilder.js" - let selectedItems - export let _bb const cb = new ClassBuilder("list", ["one-line"]) diff --git a/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte b/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte index cb83f88d40..9916b0f0f1 100644 --- a/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte +++ b/packages/materialdesign-components/src/Radiobutton/Radiobutton.svelte @@ -3,23 +3,29 @@ import Formfield from "../Common/Formfield.svelte" import ClassBuilder from "../ClassBuilder.js" import { MDCRadio } from "@material/radio" + import { generate } from "shortid" export let onClick = item => {} export let _bb export let id = "" export let label = "" + export let value = "" export let name = "radios" export let checked = false export let disabled = false export let alignEnd = false + let _id let instance = null let radiobtn = null + let selectedItems let context = _bb.getContext("BBMD:input:context") onMount(() => { + _id = generate() + if (!!radiobtn) { instance = new MDCRadio(radiobtn) if (context !== "list-item") { @@ -27,8 +33,23 @@ fieldStore.setInput(instance) } } + if (context === "radiobuttongroup") { + selectedItems = _bb.getContext("BBMD:radiobutton:selectedItemsStore") + let props = _bb.getContext("BBMD:radiobutton:props") + name = props.name + } }) + function handleOnClick() { + let item = { _id, label, value } + if (context === "radiobuttongroup") { + selectedItems.addSingleItem(item) + } else { + debugger + onClick(item) + } + } + let extras = null if (context === "list-item") { @@ -40,6 +61,11 @@ let props = { modifiers, extras } const blockClass = cb.build({ props }) + + $: isChecked = + context === "radiobuttongroup" + ? $selectedItems && $selectedItems.findIndex(i => i._id === _id) > -1 + : checked {#if context !== 'list-item'} @@ -52,7 +78,7 @@ {name} {checked} {disabled} - on:click={onClick} /> + on:click={handleOnClick} />
diff --git a/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte b/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte index c0b083db3c..ca9ca66618 100644 --- a/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte +++ b/packages/materialdesign-components/src/Radiobutton/RadiobuttonGroup.svelte @@ -1,28 +1,39 @@
@@ -30,17 +41,7 @@
- {#each items as item, i} -
- handleOnClick(item)} /> -
- {/each} +
diff --git a/packages/materialdesign-components/src/Select/Select.svelte b/packages/materialdesign-components/src/Select/Select.svelte index 28cf58697f..c69eaeeb1d 100644 --- a/packages/materialdesign-components/src/Select/Select.svelte +++ b/packages/materialdesign-components/src/Select/Select.svelte @@ -39,7 +39,6 @@ if (!!select) { instance = new MDCSelect(select) - debugger return () => { instance && instance.destroy() instance = null diff --git a/packages/materialdesign-components/src/Test/TestApp.svelte b/packages/materialdesign-components/src/Test/TestApp.svelte index bdea8413ed..03976d2ff0 100644 --- a/packages/materialdesign-components/src/Test/TestApp.svelte +++ b/packages/materialdesign-components/src/Test/TestApp.svelte @@ -38,6 +38,8 @@ CustomersIndexTable, List, Select, + Radiobutton, + Radiobuttongroup, ], }, } diff --git a/packages/materialdesign-components/src/Test/props.js b/packages/materialdesign-components/src/Test/props.js index 942b031117..16138ed3d5 100644 --- a/packages/materialdesign-components/src/Test/props.js +++ b/packages/materialdesign-components/src/Test/props.js @@ -95,19 +95,33 @@ export const props = { _children: [], label: "Hi radio", alignEnd: true, - onClick: () => alert`Roger That`, + onClick: item => console.log(item), }, Radiobuttongroup: { _component: "@budibase/materialdesign-components/Radiobuttongroup", - _children: [], label: "Preferred method of contact: ", orientation: "column", - items: [ - { label: "Email", value: 1 }, - { label: "Phone", value: 2 }, - { label: "Social Media", value: 3 }, + onChange: selected => console.log("Radiobutton Group", selected), + _children: [ + { + _component: "@budibase/materialdesign-components/Radiobutton", + _children: [], + label: "Email", + value: 1, + }, + { + _component: "@budibase/materialdesign-components/Radiobutton", + _children: [], + label: "Phone", + value: 2, + }, + { + _component: "@budibase/materialdesign-components/Radiobutton", + _children: [], + label: "Social Media", + value: 3, + }, ], - onChange: selected => console.log(selected), }, Datatable: { _component: "@budibase/materialdesign-components/Datatable",