carbon-components-svelte/src/Tile/SelectableTile.svelte
metonym ba58ba8f00
refactor: use icons from carbon-icons-svelte@11 (#1227)
* chore: update ignore rules, remove unused files

* refactor(icons): use icons from carbon-icons-svelte@11

* docs(time-picker): fix default value

* chore: upgrade carbon-icons-svelte to v11

* docs: update examples to use icons from carbon-icons-svelte@11

* docs: update number of icons [ci skip]
2022-04-03 11:57:28 -07:00

88 lines
2.1 KiB
Svelte

<script>
/** Set to `true` to select the tile */
export let selected = false;
/** Set to `true` to enable the light variant */
export let light = false;
/** Set to `true` to disable the tile */
export let disabled = false;
/** Specify the title of the selectable tile */
export let title = "title";
/** Specify the value of the selectable tile */
export let value = "value";
/** Specify the tabindex */
export let tabindex = "0";
/** Specify the ARIA label for the selectable tile checkmark icon */
export let iconDescription = "Tile checkmark";
/** Set an id for the input element */
export let id = "ccs-" + Math.random().toString(36);
/**
* Specify a name attribute for the input
* @type {string}
*/
export let name = "";
/** Obtain a reference to the input HTML element */
export let ref = null;
import { createEventDispatcher } from "svelte";
import CheckmarkFilled from "../icons/CheckmarkFilled.svelte";
const dispatch = createEventDispatcher();
$: if (!disabled) dispatch(selected ? "select" : "deselect", id);
</script>
<input
bind:this="{ref}"
type="checkbox"
tabindex="-1"
class:bx--tile-input="{true}"
checked="{selected}"
id="{id}"
value="{value}"
name="{name}"
title="{title}"
disabled="{disabled}"
/>
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<label
for="{id}"
tabindex="{disabled ? undefined : tabindex}"
class:bx--tile="{true}"
class:bx--tile--selectable="{true}"
class:bx--tile--is-selected="{selected}"
class:bx--tile--light="{light}"
class:bx--tile--disabled="{disabled}"
{...$$restProps}
on:click
on:click|preventDefault="{() => {
if (disabled) return;
selected = !selected;
}}"
on:mouseover
on:mouseenter
on:mouseleave
on:keydown
on:keydown="{(e) => {
if (disabled) return;
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
selected = !selected;
}
}}"
>
<span class:bx--tile__checkmark="{true}">
<CheckmarkFilled aria-label="{iconDescription}" title="{iconDescription}" />
</span>
<span class:bx--tile-content="{true}">
<slot />
</span>
</label>