bugfix: MD Select - ensure value is selected via value prop

This commit is contained in:
Michael Shanks 2020-03-04 23:03:51 +00:00
parent b43c6fb81d
commit 6be190e5dd
3 changed files with 35 additions and 23 deletions

View File

@ -27,6 +27,15 @@
let role = "option" let role = "option"
const itemData = () => ({
_id,
value,
text,
secondaryText,
selected,
disabled,
})
onMount(() => { onMount(() => {
_id = generate() _id = generate()
@ -35,20 +44,19 @@
listProps = _bb.getContext("BBMD:list:props") listProps = _bb.getContext("BBMD:list:props")
let context = _bb.getContext("BBMD:list:context") let context = _bb.getContext("BBMD:list:context")
const _addItem = _bb.getContext("BBMD:list:addItem")
if (context === "menu") { if (context === "menu") {
role = "menuitem" role = "menuitem"
} }
if (_addItem) {
_addItem(itemData())
}
}) })
function handleClick() { function handleClick() {
let item = { let item = itemData()
_id,
value,
text,
secondaryText,
selected,
disabled,
}
if (!disabled) { if (!disabled) {
if ( if (
listProps.singleSelection || listProps.singleSelection ||

View File

@ -16,8 +16,8 @@
let select let select
let selectList let selectList
let instance let instance
let _helperId = "" let _helperId = ""
let listItems = []
export let _bb export let _bb
export let onSelect = items => {} export let onSelect = items => {}
@ -32,19 +32,18 @@
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])
const setValue = v => {
_bb.setStateFromBinding(_bb.props.value, v)
value = v
}
selectedItemsStore = createItemsStore(() => { selectedItemsStore = createItemsStore(() => {
if($selectedItemsStore && $selectedItemsStore.length > 0) const v = $selectedItemsStore && $selectedItemsStore.length > 0
setValue($selectedItemsStore[0].value) ? $selectedItemsStore[0].value
else : "";
setValue("")
value = v
_bb.setStateFromBinding(_bb.props.value, v)
_bb.call(onSelect, v)
_bb.call(onSelect, $selectedItemsStore)
}) })
_bb.setContext("BBMD:list:selectItemStore", selectedItemsStore) _bb.setContext("BBMD:list:selectItemStore", selectedItemsStore)
@ -59,15 +58,19 @@
} }
}) })
$: 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 }
$: selectClass = cb.build({ props }) $: selectClass = cb.build({ props })
$: if (value !== undefined && selectedItemsStore) { $: if (value !== undefined && instance && listItems.length > 0) {
selectedItemsStore.addSingleItem(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

@ -166,25 +166,26 @@ export const props = {
label: "Choose a Milkshake", label: "Choose a Milkshake",
helperText: "Choose a flavour", helperText: "Choose a flavour",
persistent: true, persistent: true,
value: "1",
onSelect: selectedItem => console.log("SELECT ITEM", selectedItem), onSelect: selectedItem => console.log("SELECT ITEM", selectedItem),
_children: [ _children: [
{ {
_component: "@budibase/materialdesign-components/ListItem", _component: "@budibase/materialdesign-components/ListItem",
_children: [], _children: [],
text: "Orange", text: "Orange",
value: 0, value: "0",
}, },
{ {
_component: "@budibase/materialdesign-components/ListItem", _component: "@budibase/materialdesign-components/ListItem",
_children: [], _children: [],
text: "Apple", text: "Apple",
value: 1, value: "1",
}, },
{ {
_component: "@budibase/materialdesign-components/ListItem", _component: "@budibase/materialdesign-components/ListItem",
_children: [], _children: [],
text: "Berry", text: "Berry",
value: 0, value: "2",
}, },
], ],
}, },