Radiobutton Group update to work with builder specific logic

This commit is contained in:
Conor_Mack 2020-03-04 16:35:52 +00:00
parent a7b56966f3
commit c605d49949
7 changed files with 78 additions and 38 deletions

View File

@ -1,14 +1,14 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store"
function createItemsStore(componentOnSelect) { function createItemsStore(componentOnSelect) {
const { subscribe, set, update } = writable([]); const { subscribe, set, update } = writable([])
function addItem(item) { function addItem(item) {
update(items => { update(items => {
return [...items, item] return [...items, item]
}) })
if (componentOnSelect) { if (componentOnSelect) {
componentOnSelect(); componentOnSelect()
} }
} }

View File

@ -6,8 +6,6 @@
import ListItem from "./ListItem.svelte" import ListItem from "./ListItem.svelte"
import ClassBuilder from "../ClassBuilder.js" import ClassBuilder from "../ClassBuilder.js"
let selectedItems
export let _bb export let _bb
const cb = new ClassBuilder("list", ["one-line"]) const cb = new ClassBuilder("list", ["one-line"])

View File

@ -3,23 +3,29 @@
import Formfield from "../Common/Formfield.svelte" import Formfield from "../Common/Formfield.svelte"
import ClassBuilder from "../ClassBuilder.js" import ClassBuilder from "../ClassBuilder.js"
import { MDCRadio } from "@material/radio" import { MDCRadio } from "@material/radio"
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 label = "" export let label = ""
export let value = ""
export let name = "radios" export let name = "radios"
export let checked = false export let checked = false
export let disabled = false export let disabled = false
export let alignEnd = false export let alignEnd = false
let _id
let instance = null let instance = null
let radiobtn = null let radiobtn = null
let selectedItems
let context = _bb.getContext("BBMD:input:context") let context = _bb.getContext("BBMD:input:context")
onMount(() => { onMount(() => {
_id = generate()
if (!!radiobtn) { if (!!radiobtn) {
instance = new MDCRadio(radiobtn) instance = new MDCRadio(radiobtn)
if (context !== "list-item") { if (context !== "list-item") {
@ -27,8 +33,23 @@
fieldStore.setInput(instance) 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 let extras = null
if (context === "list-item") { if (context === "list-item") {
@ -40,6 +61,11 @@
let props = { modifiers, extras } let props = { modifiers, extras }
const blockClass = cb.build({ props }) const blockClass = cb.build({ props })
$: isChecked =
context === "radiobuttongroup"
? $selectedItems && $selectedItems.findIndex(i => i._id === _id) > -1
: checked
</script> </script>
{#if context !== 'list-item'} {#if context !== 'list-item'}
@ -52,7 +78,7 @@
{name} {name}
{checked} {checked}
{disabled} {disabled}
on:click={onClick} /> on:click={handleOnClick} />
<div class={cb.elem`background`}> <div class={cb.elem`background`}>
<div class={cb.elem`outer-circle`} /> <div class={cb.elem`outer-circle`} />
<div class={cb.elem`inner-circle`} /> <div class={cb.elem`inner-circle`} />

View File

@ -1,28 +1,39 @@
<script> <script>
import { onMount } from "svelte"
import createItemStore from "../Common/ItemStore.js"
import Radiobutton from "./Radiobutton.svelte" import Radiobutton from "./Radiobutton.svelte"
import Label from "../Common/Label.svelte" import Label from "../Common/Label.svelte"
export let onChange = selected => {}
export let _bb
export let name = "radio-group" export let name = "radio-group"
export let label = "" export let label = ""
export let orientation = "row" export let orientation = "row"
export let fullwidth = false export let fullwidth = false
export let alignEnd = false export let alignEnd = false
export let onChange = selected => {} let selectedItemsStore
export let items = []
let selected = null let selected = null
let radioItems
onMount(() => {
_bb.setContext("BBMD:input:context", "radiobuttongroup")
selectedItemsStore = createItemStore(() => onChange($selectedItemsStore))
_bb.setContext("BBMD:radiobutton:props", { name })
_bb.setContext("BBMD:radiobutton:selectedItemsStore", selectedItemsStore)
})
// function handleOnClick(item) {
// if (!!selected) selected.checked = false
// item.checked = true
// selected = item
// items = items
// onChange(selected)
// }
$: alignRight = orientation === "column" && alignEnd $: alignRight = orientation === "column" && alignEnd
$: radioItems && _bb.attachChildren(radioItems)
function handleOnClick(item) {
if (!!selected) selected.checked = false
item.checked = true
selected = item
items = items
onChange(selected)
}
</script> </script>
<div class="radiobutton-group"> <div class="radiobutton-group">
@ -30,17 +41,7 @@
<Label text={label} bold /> <Label text={label} bold />
</div> </div>
<div class={`radiobutton-group__radios ${orientation}`} class:alignRight> <div class={`radiobutton-group__radios ${orientation}`} class:alignRight>
{#each items as item, i} <div bind:this={radioItems} class:fullwidth />
<div class:fullwidth>
<Radiobutton
id={`${item.label}-${i}`}
{name}
{alignEnd}
label={item.label}
checked={item.checked || false}
onClick={() => handleOnClick(item)} />
</div>
{/each}
</div> </div>
</div> </div>

View File

@ -39,7 +39,6 @@
if (!!select) { if (!!select) {
instance = new MDCSelect(select) instance = new MDCSelect(select)
debugger
return () => { return () => {
instance && instance.destroy() instance && instance.destroy()
instance = null instance = null

View File

@ -38,6 +38,8 @@
CustomersIndexTable, CustomersIndexTable,
List, List,
Select, Select,
Radiobutton,
Radiobuttongroup,
], ],
}, },
} }

View File

@ -95,19 +95,33 @@ export const props = {
_children: [], _children: [],
label: "Hi radio", label: "Hi radio",
alignEnd: true, alignEnd: true,
onClick: () => alert`Roger That`, onClick: item => console.log(item),
}, },
Radiobuttongroup: { Radiobuttongroup: {
_component: "@budibase/materialdesign-components/Radiobuttongroup", _component: "@budibase/materialdesign-components/Radiobuttongroup",
_children: [],
label: "Preferred method of contact: ", label: "Preferred method of contact: ",
orientation: "column", orientation: "column",
items: [ onChange: selected => console.log("Radiobutton Group", selected),
{ label: "Email", value: 1 }, _children: [
{ label: "Phone", value: 2 }, {
{ label: "Social Media", value: 3 }, _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: { Datatable: {
_component: "@budibase/materialdesign-components/Datatable", _component: "@budibase/materialdesign-components/Datatable",