Updating material components to reference bb apis properly

This commit is contained in:
Conor_Mack 2020-03-30 11:20:55 +01:00
parent 9cfbb34ba2
commit d157165e34
17 changed files with 101 additions and 122 deletions

View File

@ -6,6 +6,7 @@
const cb = new ClassBuilder("button", ["primary", "medium", "text"]) const cb = new ClassBuilder("button", ["primary", "medium", "text"])
export let _bb
export let onClick export let onClick
export let variant = "text" export let variant = "text"
@ -20,8 +21,6 @@
export let text = "" export let text = ""
export let disabled = false export let disabled = false
export let _bb
onMount(() => { onMount(() => {
let ctx = _bb.getContext("BBMD:button:context") let ctx = _bb.getContext("BBMD:button:context")
extras = [ctx] extras = [ctx]

View File

@ -2,9 +2,11 @@
export let _bb export let _bb
export let onClick = () => {} export let onClick = () => {}
export let clicked = () => _bb.call(onClick)
let cardBody let cardBody
$: cardBody && _bb.attachChildren(cardBody) $: cardBody && _bb.attachChildren(cardBody)
</script> </script>
<div bind:this={cardBody} class="mdc-card__primary-action" on:click={onClick} /> <div bind:this={cardBody} class="mdc-card__primary-action" on:click={clicked} />

View File

@ -6,7 +6,7 @@
import { MDCCheckbox } from "@material/checkbox" import { MDCCheckbox } from "@material/checkbox"
import { generate } from "shortid" import { generate } from "shortid"
export let onClick = item => {} export let onChange = item => {}
export let _bb export let _bb
@ -59,13 +59,15 @@
function changed(e) { function changed(e) {
const val = e.target.checked const val = e.target.checked
checked = val checked = val
handleOnClick()
if (_bb.isBound(_bb.props.checked)) { if (_bb.isBound(_bb.props.checked)) {
_bb.setStateFromBinding(_bb.props.checked, val) _bb.setStateFromBinding(_bb.props.checked, val)
} }
} }
function handleOnClick() { function handleOnClick() {
let item = { _id, label, value } let item = { _id, checked, label, value }
if (context === "checkboxgroup") { if (context === "checkboxgroup") {
let idx = selectedItems.getItemIdx($selectedItems, _id) let idx = selectedItems.getItemIdx($selectedItems, _id)
if (idx > -1) { if (idx > -1) {
@ -73,9 +75,8 @@
} else { } else {
selectedItems.addItem(item) selectedItems.addItem(item)
} }
} else {
onClick(item)
} }
_bb.call(onChange, item)
} }
$: isChecked = $: isChecked =
@ -101,7 +102,6 @@
{id} {id}
disabled={isDisabled} disabled={isDisabled}
{isChecked} {isChecked}
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">
@ -123,8 +123,7 @@
{id} {id}
disabled={isDisabled} disabled={isDisabled}
{isChecked} {isChecked}
on:change={changed} on:change={changed} />
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

@ -15,10 +15,17 @@
export let disabled = false export let disabled = false
export let alignEnd = false export let alignEnd = false
export let value = []
onMount(() => { onMount(() => {
_bb.setContext("BBMD:input:context", "checkboxgroup") _bb.setContext("BBMD:input:context", "checkboxgroup")
selectedItemsStore = createItemsStore(() => onChange($selectedItemsStore)) selectedItemsStore = createItemsStore(() => {
value = $selectedItemsStore
if (_bb.isBound(_bb.props.value)) {
_bb.setStateFromBinding(_bb.props.value, value)
}
_bb.call(onChange, value)
}, value)
_bb.setContext("BBMD:checkbox:selectedItemsStore", selectedItemsStore) _bb.setContext("BBMD:checkbox:selectedItemsStore", selectedItemsStore)
_bb.setContext("BBMD:checkbox:props", { alignEnd, disabled }) _bb.setContext("BBMD:checkbox:props", { alignEnd, disabled })
}) })

View File

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

View File

@ -38,11 +38,31 @@
} }
}) })
function handleSelect(date) {
if (_bb.isBound(_bb.props.date)) {
_bb.setStateFromBinding(_bb.props.date, date)
}
_bb.call(onSelect, date)
}
function selectDate(dayOfMonth) { function selectDate(dayOfMonth) {
let month = getMonth(navDate) let month = getMonth(navDate)
let year = getYear(navDate) let year = getYear(navDate)
date = new Date(year, month, dayOfMonth) date = new Date(year, month, dayOfMonth)
onSelect(date) handleSelect(date)
}
function dateFieldChange(value) {
const isDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/
if (isDate.test(value)) {
const [year, month, day] = value.split("/").reverse()
if (month > 0 && month <= 12 && (day > 0 && day <= 31)) {
date = new Date(year, month - 1, day)
navDate = date
openCalendar(true)
handleSelect(date)
}
}
} }
function addMonth() { function addMonth() {
@ -57,19 +77,6 @@
instance.open = isOpen === undefined ? !instance.open : isOpen instance.open = isOpen === undefined ? !instance.open : isOpen
} }
function textFieldChange(value) {
const isDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/
if (isDate.test(value)) {
const [year, month, day] = value.split("/").reverse()
if (month > 0 && month <= 12 && (day > 0 && day <= 31)) {
date = new Date(year, month - 1, day)
navDate = date
openCalendar(true)
onSelect(date)
}
}
}
$: dateMonthEnds = endOfMonth(navDate).getDate() $: dateMonthEnds = endOfMonth(navDate).getDate()
$: dateMonthBegins = startOfMonth(navDate).getDay() $: dateMonthBegins = startOfMonth(navDate).getDay()
$: dayStart = dateMonthBegins + 1 //1 = sunday $: dayStart = dateMonthBegins + 1 //1 = sunday
@ -92,7 +99,7 @@
<div class="mdc-menu-surface--anchor"> <div class="mdc-menu-surface--anchor">
<Textfield <Textfield
{label} {label}
onChange={textFieldChange} onChange={dateFieldChange}
value={selectedDate} value={selectedDate}
trailingIcon={true} trailingIcon={true}
useIconButton={true} useIconButton={true}

View File

@ -26,7 +26,7 @@
function onButtonClick() { function onButtonClick() {
open = !open open = !open
onClick() _bb.call(onClick)
} }
$: isToggleButton = !!icon && !!onIcon $: isToggleButton = !!icon && !!onIcon

View File

@ -17,6 +17,7 @@
export let singleSelection = false export let singleSelection = false
export let variant = "one-line" export let variant = "one-line"
export let inputElement = null export let inputElement = null
export let value = []
let selectedItemsStore let selectedItemsStore
@ -25,7 +26,13 @@
onMount(() => { onMount(() => {
let ctx = getContext("BBMD:list:context") let ctx = getContext("BBMD:list:context")
selectedItemsStore = createItemsStore(() => onSelect($selectedItemsStore)) selectedItemsStore = createItemsStore(() => {
value = $selectedItemsStore
if (_bb.isBound(_bb.props.value)) {
_bb.setStateFromBinding(_bb.props.value, value)
}
_bb.call(onSelect, value)
}, value)
_bb.setContext("BBMD:list:selectItemStore", selectedItemsStore) _bb.setContext("BBMD:list:selectItemStore", selectedItemsStore)
_bb.setContext("BBMD:list:props", { _bb.setContext("BBMD:list:props", {

View File

@ -24,7 +24,14 @@
_bb.setContext("BBMD:list:context", "menu") _bb.setContext("BBMD:list:context", "menu")
_bb.setContext("BBMD:list:props", { singleSelection: true }) _bb.setContext("BBMD:list:props", { singleSelection: true })
selectedItemsStore = createItemsStore(() => onSelect($selectedItemsStore)) selectedItemsStore = createItemsStore(() => {
let value =
$selectedItemsStore && $selectedItemsStore.length > 0
? $selectedItemsStore[0]
: ""
_bb.call(onSelect, value)
})
_bb.setContext("BBMD:list:selectItemStore", selectedItemsStore) _bb.setContext("BBMD:list:selectItemStore", selectedItemsStore)
if (!!menu) { if (!!menu) {

View File

@ -46,7 +46,7 @@
if (context === "radiobuttongroup") { if (context === "radiobuttongroup") {
selectedItems.addSingleItem(item) selectedItems.addSingleItem(item)
} else { } else {
onClick(item) _bb.call(onClick, item)
} }
} }
@ -106,7 +106,7 @@
{name} {name}
{checked} {checked}
disabled={isDisabled} disabled={isDisabled}
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

@ -13,6 +13,9 @@
export let fullwidth = false export let fullwidth = false
export let alignEnd = false export let alignEnd = false
export let disabled = false export let disabled = false
export let value = []
$: safeValue = Array.isArray(value) ? value : [value]
let selectedItemsStore let selectedItemsStore
let selected = null let selected = null
@ -20,7 +23,13 @@
onMount(() => { onMount(() => {
_bb.setContext("BBMD:input:context", "radiobuttongroup") _bb.setContext("BBMD:input:context", "radiobuttongroup")
selectedItemsStore = createItemStore(() => onChange($selectedItemsStore)) selectedItemsStore = createItemStore(() => {
value = $selectedItemsStore
if (_bb.isBound(_bb.props.value)) {
_bb.setStateFromBinding(_bb.props.value, value)
}
_bb.call(onChange, value)
}, safeValue)
_bb.setContext("BBMD:radiobutton:props", { name, disabled, alignEnd }) _bb.setContext("BBMD:radiobutton:props", { name, disabled, alignEnd })
_bb.setContext("BBMD:radiobutton:selectedItemsStore", selectedItemsStore) _bb.setContext("BBMD:radiobutton:selectedItemsStore", selectedItemsStore)
}) })

View File

@ -30,21 +30,22 @@
export let persistent = false export let persistent = false
export let value = "" export let value = ""
$: safeValue = Array.isArray(value) ? value : [value]
onMount(() => { onMount(() => {
_bb.setContext("BBMD:list:props", { singleSelection: true }) _bb.setContext("BBMD:list:props", { singleSelection: true })
_bb.setContext("BBMD:list:addItem", i => listItems = [...listItems, i]) _bb.setContext("BBMD:list:addItem", i => (listItems = [...listItems, i]))
selectedItemsStore = createItemsStore(() => { selectedItemsStore = createItemsStore(() => {
const v =
const v = $selectedItemsStore && $selectedItemsStore.length > 0 $selectedItemsStore && $selectedItemsStore.length > 0
? $selectedItemsStore[0].value ? $selectedItemsStore[0].value
: ""; : ""
value = v value = v
_bb.setStateFromBinding(_bb.props.value, v) _bb.setStateFromBinding(_bb.props.value, v)
_bb.call(onSelect, v) _bb.call(onSelect, v)
}, safeValue)
})
_bb.setContext("BBMD:list:selectItemStore", selectedItemsStore) _bb.setContext("BBMD:list:selectItemStore", selectedItemsStore)
_helperId = generate() _helperId = generate()
@ -58,10 +59,8 @@
} }
}) })
$: useNotchedOutline = variant === "outlined" $: useNotchedOutline = variant === "outlined"
$: selectList && _bb.attachChildren(selectList) $: selectList && _bb.attachChildren(selectList)
$: modifiers = { variant, disabled, required, noLabel: !label } $: modifiers = { variant, disabled, required, noLabel: !label }
$: props = { modifiers } $: props = { modifiers }
@ -69,8 +68,6 @@
$: if (value !== undefined && instance && listItems.length > 0) { $: if (value !== undefined && instance && listItems.length > 0) {
instance.selectedIndex = listItems.findIndex(i => i.value === value) instance.selectedIndex = listItems.findIndex(i => i.value === value)
} }
</script> </script>
<div bind:this={select} id={_helperId} class={selectClass}> <div bind:this={select} id={_helperId} class={selectClass}>

View File

@ -8,6 +8,7 @@
let slider let slider
let instance let instance
export let _bb
export let variant = "continuous" //or discrete export let variant = "continuous" //or discrete
export let showTicks = false export let showTicks = false
export let min = 0 export let min = 0
@ -17,6 +18,14 @@
export let label = "" export let label = ""
export let disabled = false export let disabled = false
function handleChange(val) {
value = val
if (_bb.isBound(_bb.props.value)) {
_bb.setStateFromBinding(_bb.props.value, value)
}
_bb.call(onChange, val)
}
onMount(() => { onMount(() => {
let s = MDCSlider.attachTo(slider) let s = MDCSlider.attachTo(slider)
return () => instance.destroy() return () => instance.destroy()
@ -28,16 +37,12 @@
} }
} }
$: instance && console.log("instance dot value: ", instance.value)
$: isDiscrete = variant === "discrete" $: isDiscrete = variant === "discrete"
$: displayMarkers = isDiscrete && showTicks $: displayMarkers = isDiscrete && showTicks
$: modifiers = { variant, displayMarkers } $: modifiers = { variant, displayMarkers }
$: props = { modifiers } $: props = { modifiers }
$: sliderCls = cb.build({ props }) $: sliderCls = cb.build({ props })
$: console.log("VALUE", value)
</script> </script>
<div <div
@ -49,7 +54,8 @@
aria-valuemax="100" aria-valuemax="100"
aria-valuenow="0" aria-valuenow="0"
aria-label="Select Value" aria-label="Select Value"
on:MDCSlider:change={e => console.log('SLIDER VAL', e.detail.value)}> on:MDCSlider:input={e => handleChange(e.detail.value)}
on:MDCSlider:change={e => handleChange(e.detail.value)}>
<div class="mdc-slider__track-container"> <div class="mdc-slider__track-container">
<div class="mdc-slider__track" /> <div class="mdc-slider__track" />
</div> </div>
@ -63,57 +69,3 @@
<div class="mdc-slider__focus-ring" /> <div class="mdc-slider__focus-ring" />
</div> </div>
</div> </div>
<!--
<div
bind:this={slider}
class={sliderCls}
tabindex="0"
role="slider"
aria-valuemin={min}
aria-valuemax={max}
aria-valuenow={value}
data-step={step}
aria-label={label}
on:MDCSlider:input={e => console.log('INPUT', e.detail.value)}
on:MDCSlider:change={e => console.log('CHANGE', e.detail.value)}
aria-disabled={disabled}>
<div class="mdc-slider__track-container">
{#if displayMarkers}
<div class="mdc-slider__track-container">
<div class="mdc-slider__track" />
<div class="mdc-slider__track-marker-container" />
</div>
{:else}
<div class="mdc-slider__track" />
{/if}
</div>
<div class="mdc-slider__thumb-container">
{#if isDiscrete}
<div class="mdc-slider__pin">
<span class="mdc-slider__pin-value-marker" />
</div>
{/if}
<svg class="mdc-slider__thumb" width="21" height="21">
<circle cx="10.5" cy="10.5" r="7.875" />
</svg>
<div class="mdc-slider__focus-ring" />
</div>
</div> -->
<!-- <div class="mdc-slider mdc-slider--discrete" tabindex="0" role="slider"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"
aria-label="Select Value">
<div class="mdc-slider__track-container">
<div class="mdc-slider__track"></div>
</div>
<div class="mdc-slider__thumb-container">
<div class="mdc-slider__pin">
<span class="mdc-slider__pin-value-marker"></span>
</div>
<svg class="mdc-slider__thumb" width="21" height="21">
<circle cx="10.5" cy="10.5" r="7.875"></circle>
</svg>
<div class="mdc-slider__focus-ring"></div>
</div>
</div> -->

View File

@ -15,7 +15,10 @@
function handleChange() { function handleChange() {
checked = !checked checked = !checked
onChange(checked) if (_bb.isBound(_bb.props.checked)) {
_bb.setStateFromBinding(_bb.props.checked, checked)
}
_bb.call(onChange, checked)
} }
$: modifiers = { disabled, checked } $: modifiers = { disabled, checked }

View File

@ -33,17 +33,7 @@
const page = { const page = {
props: { props: {
_component: "testcomponents/rootComponent", _component: "testcomponents/rootComponent",
_children: [ _children: [Checkbox],
BodyBoundToStore,
Textfield,
Select,
Radiobutton,
Radiobuttongroup,
DatePicker,
IconButton,
Switch,
Slider,
],
}, },
} }
_appPromise.then(initialise => { _appPromise.then(initialise => {

View File

@ -79,7 +79,7 @@ export const props = {
id: "test-check", id: "test-check",
alignEnd: true, alignEnd: true,
label: "Check Yo Self", label: "Check Yo Self",
onClick: () => alert`Before ya reck yo'self`, onChange: item => console.log(item),
}, },
Checkboxgroup: { Checkboxgroup: {
_component: "@budibase/materialdesign-components/Checkboxgroup", _component: "@budibase/materialdesign-components/Checkboxgroup",

View File

@ -18,6 +18,7 @@
let tfInstance = null let tfInstance = null
onMount(() => { onMount(() => {
debugger
if (!!tf) tfInstance = new MDCTextField(tf) if (!!tf) tfInstance = new MDCTextField(tf)
return () => { return () => {
!!tfInstance && tf.tfInstance && tf.tfInstance.destroy() !!tfInstance && tf.tfInstance && tf.tfInstance.destroy()
@ -97,11 +98,10 @@
function changed(e) { function changed(e) {
const val = e.target.value const val = e.target.value
value = val value = val
onChange(value) if (_bb.isBound(_bb.props.value)) {
// if (_bb.isBound(_bb.props.value)) { _bb.setStateFromBinding(_bb.props.value, val)
// _bb.setStateFromBinding(_bb.props.value, val) }
// } _bb.call(onChange, val)
// _bb.call(onChange, val)
} }
</script> </script>