carbon-components-svelte/src/Tile/SelectableTileGroup.svelte
Richard O'flynn d8741808e5 Some fixes
2020-11-30 22:02:44 +00:00

52 lines
1.2 KiB
Svelte

<script>
/**
* Specify the selected tile's
* @type {string[]}
*/
export let selectedValues = [];
/** Set to `true` to disable the tile group */
export let disabled = false;
/** Set to `true` to enable the light variant */
export let light = false;
/** Specify the legend text */
export let legend = "";
import { createEventDispatcher, setContext } from "svelte";
import { writable } from "svelte/store";
const dispatch = createEventDispatcher();
const _selectedValues = writable(selectedValues);
function newArray(value, selected) {
let a = $_selectedValues;
const i = a.indexOf(value);
if (selected && i === -1) {
a = a.concat([value]);
} else if (!selected && i > -1) {
a = a.splice(i, 1);
}
return a;
}
setContext("SelectableTileGroup", {
_light: light,
selectedValues: _selectedValues,
update: ({ selected, value }) =>
_selectedValues.set(newArray(value, selected)),
});
$: selectedValues = $_selectedValues;
$: dispatch("select", $_selectedValues);
</script>
<fieldset disabled="{disabled}" class:bx--tile-group="{true}" {...$$restProps}>
{#if legend}
<legend class:bx--label="{true}">{legend}</legend>
{/if}
<div>
<slot />
</div>
</fieldset>