Merge pull request #118 from Conor-Mack/feature/mdc-list

Adding List divider
This commit is contained in:
Conor_Mack 2020-02-21 14:02:27 +00:00 committed by GitHub
commit eaf9283f0f
2 changed files with 34 additions and 31 deletions

View File

@ -1,59 +1,59 @@
<script> <script>
import { onMount } from "svelte"; import { onMount } from "svelte"
import { MDCList } from "@material/list"; import { MDCList } from "@material/list"
import { MDCRipple } from "@material/ripple"; import { MDCRipple } from "@material/ripple"
import ListItem from "./ListItem.svelte"; import ListItem from "./ListItem.svelte"
import ClassBuilder from "../ClassBuilder.js"; import ClassBuilder from "../ClassBuilder.js"
const cb = new ClassBuilder("list"); const cb = new ClassBuilder("list")
let list = null; let list = null
let instance = null; let instance = null
export let onSelect = selectedItems => {}; export let onSelect = selectedItems => {}
export let variant = ""; export let variant = ""
//items: [{text: string | {primary: string, secondary: string}, value: any, selected: bool}...n] //items: [{text: string | {primary: string, secondary: string}, value: any, selected: bool}...n]
export let items = []; export let items = []
export let singleSelection = false; export let singleSelection = false
export let inputElement = null; export let inputElement = null
onMount(() => { onMount(() => {
if (!!list) { if (!!list) {
instance = new MDCList(list); instance = new MDCList(list)
instance.singleSelection = singleSelection; instance.singleSelection = singleSelection
instance.listElements.map(element => new MDCRipple(element)); instance.listElements.map(element => new MDCRipple(element))
} }
return () => { return () => {
instance && instance.destroy(); instance && instance.destroy()
instance = null; instance = null
}; }
}); })
function handleSelectedItem(item) { function handleSelectedItem(item) {
if (singleSelection || inputElement === "radiobutton") { if (singleSelection || inputElement === "radiobutton") {
items.forEach(i => { items.forEach(i => {
if (i.selected) i.selected = false; if (i.selected) i.selected = false
}); })
} }
let idx = items.indexOf(item); let idx = items.indexOf(item)
if (!!item.selected) { if (!!item.selected) {
items[idx].selected = !item.selected; items[idx].selected = !item.selected
} else { } else {
items[idx].selected = true; items[idx].selected = true
} }
onSelect(items.filter(item => item.selected)); onSelect(items.filter(item => item.selected))
} }
$: useDoubleLine = $: useDoubleLine =
variant == "two-line" && variant == "two-line" &&
items.every(i => typeof i.text == "object" && "primary" in i.text); items.every(i => typeof i.text == "object" && "primary" in i.text)
$: modifiers = { variant }; $: modifiers = { variant }
$: props = { modifiers }; $: props = { modifiers }
$: listClass = cb.build({ props }); $: listClass = cb.build({ props })
</script> </script>
<div class={listClass} role="listbox"> <div class={listClass} role="listbox">
@ -63,5 +63,8 @@
{useDoubleLine} {useDoubleLine}
{inputElement} {inputElement}
onClick={() => handleSelectedItem(item)} /> onClick={() => handleSelectedItem(item)} />
{#if item.divider}
<li role="separator" class="mdc-list-divider" />
{/if}
{/each} {/each}
</div> </div>

View File

@ -112,11 +112,11 @@ export const props = {
{ {
text: { primary: "Curry", secondary: "Chicken or Beef" }, text: { primary: "Curry", secondary: "Chicken or Beef" },
value: 0, value: 0,
divider: true,
}, },
{ {
text: { primary: "Pastie", secondary: "Bap with Mayo" }, text: { primary: "Pastie", secondary: "Bap with Mayo" },
value: 1, value: 1,
selected: true,
}, },
{ text: { primary: "Fish", secondary: "Salmon or Cod" }, value: 2 }, { text: { primary: "Fish", secondary: "Salmon or Cod" }, value: 2 },
], ],