mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
refactor(tile): forward events, inline functions
Supports #7, #34 - Forward events, style prop - Remove unneeded dispatched events - Remove exported props
This commit is contained in:
parent
5ec9c0089f
commit
a828218d15
9 changed files with 87 additions and 75 deletions
|
@ -5,28 +5,10 @@
|
||||||
export let rel = undefined;
|
export let rel = undefined;
|
||||||
export let light = false;
|
export let light = false;
|
||||||
export let clicked = false;
|
export let clicked = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher, tick } from 'svelte';
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
|
|
||||||
async function handleClick(event) {
|
|
||||||
clicked = !clicked;
|
|
||||||
await tick();
|
|
||||||
dispatch('click', event);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleKeyDown(event) {
|
|
||||||
if (event.key === ' ' || event.key === 'Enter') {
|
|
||||||
clicked = !clicked;
|
|
||||||
await tick();
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch('keydown', event);
|
|
||||||
}
|
|
||||||
|
|
||||||
$: _class = cx(
|
$: _class = cx(
|
||||||
'--link',
|
'--link',
|
||||||
'--tile',
|
'--tile',
|
||||||
|
@ -37,6 +19,23 @@
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<a {...props} class={_class} on:click={handleClick} on:keydown={handleKeyDown} {href} {rel}>
|
<a
|
||||||
|
class={_class}
|
||||||
|
on:click
|
||||||
|
on:click={() => {
|
||||||
|
clicked = !clicked;
|
||||||
|
}}
|
||||||
|
on:keydown
|
||||||
|
on:keydown={event => {
|
||||||
|
if (event.key === ' ' || event.key === 'Enter') {
|
||||||
|
clicked = !clicked;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
{style}
|
||||||
|
{href}
|
||||||
|
{rel}>
|
||||||
<slot />
|
<slot />
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -7,9 +7,9 @@
|
||||||
export let tileExpandedIconText = 'Interact to collapse Tile';
|
export let tileExpandedIconText = 'Interact to collapse Tile';
|
||||||
export let tileMaxHeight = 0;
|
export let tileMaxHeight = 0;
|
||||||
export let tilePadding = 0;
|
export let tilePadding = 0;
|
||||||
export let tabIndex = 0;
|
export let tabindex = '0';
|
||||||
export let light = false;
|
export let light = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher, tick, onMount } from 'svelte';
|
import { createEventDispatcher, tick, onMount } from 'svelte';
|
||||||
import ChevronDown16 from 'carbon-icons-svelte/lib/ChevronDown16';
|
import ChevronDown16 from 'carbon-icons-svelte/lib/ChevronDown16';
|
||||||
|
@ -63,14 +63,19 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
{...props}
|
|
||||||
bind:this={tile}
|
bind:this={tile}
|
||||||
style={tileStyle}
|
style={tileStyle}
|
||||||
class={_class}
|
class={_class}
|
||||||
|
on:click
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
|
on:keypress
|
||||||
on:keypress={handleKeyPress}
|
on:keypress={handleKeyPress}
|
||||||
tabindex={tabIndex}
|
on:mouseover
|
||||||
{id}>
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
{tabindex}
|
||||||
|
{id}
|
||||||
|
{style}>
|
||||||
<div bind:this={tileContent}>
|
<div bind:this={tileContent}>
|
||||||
<div bind:this={aboveTheFold} class={cx('--tile-content')}>
|
<div bind:this={aboveTheFold} class={cx('--tile-content')}>
|
||||||
<slot name="above" />
|
<slot name="above" />
|
||||||
|
|
|
@ -7,9 +7,9 @@
|
||||||
export let name = '';
|
export let name = '';
|
||||||
export let iconDescription = 'Tile checkmark';
|
export let iconDescription = 'Tile checkmark';
|
||||||
export let value = '';
|
export let value = '';
|
||||||
export let tabIndex = 0;
|
export let tabindex = '0';
|
||||||
export let light = false;
|
export let light = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher } from 'svelte';
|
import { createEventDispatcher } from 'svelte';
|
||||||
import CheckmarkFilled16 from 'carbon-icons-svelte/lib/CheckmarkFilled16';
|
import CheckmarkFilled16 from 'carbon-icons-svelte/lib/CheckmarkFilled16';
|
||||||
|
@ -40,7 +40,6 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
{...props}
|
|
||||||
type="radio"
|
type="radio"
|
||||||
class={cx('--tile-input')}
|
class={cx('--tile-input')}
|
||||||
on:change
|
on:change
|
||||||
|
@ -49,7 +48,17 @@
|
||||||
{name}
|
{name}
|
||||||
{value}
|
{value}
|
||||||
{checked} />
|
{checked} />
|
||||||
<label for={id} class={_class} tabindex={tabIndex} on:keydown={handleKeyDown}>
|
<label
|
||||||
|
for={id}
|
||||||
|
class={_class}
|
||||||
|
on:click
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
on:keydown
|
||||||
|
on:keydown={handleKeyDown}
|
||||||
|
{tabindex}
|
||||||
|
{style}>
|
||||||
<span class={cx('--tile__checkmark')}>
|
<span class={cx('--tile__checkmark')}>
|
||||||
<CheckmarkFilled16 aria-label={iconDescription} title={iconDescription} />
|
<CheckmarkFilled16 aria-label={iconDescription} title={iconDescription} />
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -8,40 +8,13 @@
|
||||||
export let title = 'title';
|
export let title = 'title';
|
||||||
export let name = '';
|
export let name = '';
|
||||||
export let iconDescription = 'Tile checkmark';
|
export let iconDescription = 'Tile checkmark';
|
||||||
export let tabIndex = 0;
|
export let tabindex = '0';
|
||||||
export let light = false;
|
export let light = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher, tick } from 'svelte';
|
|
||||||
import CheckmarkFilled16 from 'carbon-icons-svelte/lib/CheckmarkFilled16';
|
import CheckmarkFilled16 from 'carbon-icons-svelte/lib/CheckmarkFilled16';
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
let input = undefined;
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
|
|
||||||
function handleChange(event) {
|
|
||||||
dispatch('change', event);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleClick(event) {
|
|
||||||
if (event.target !== input) {
|
|
||||||
selected = !selected;
|
|
||||||
await tick();
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch('click', event);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleKeyDown(event) {
|
|
||||||
if (event.key === ' ' || event.key === 'Enter') {
|
|
||||||
event.preventDefault();
|
|
||||||
selected = !selected;
|
|
||||||
await tick();
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch('keydown', event);
|
|
||||||
}
|
|
||||||
|
|
||||||
$: _class = cx(
|
$: _class = cx(
|
||||||
'--tile',
|
'--tile',
|
||||||
'--tile--selectable',
|
'--tile--selectable',
|
||||||
|
@ -52,23 +25,34 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
bind:this={input}
|
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
tabindex={-1}
|
tabindex="-1"
|
||||||
class={cx('--tile-input')}
|
class={cx('--tile-input')}
|
||||||
on:change={handleChange}
|
on:change
|
||||||
checked={selected}
|
checked={selected}
|
||||||
{id}
|
{id}
|
||||||
{value}
|
{value}
|
||||||
{name}
|
{name}
|
||||||
{title} />
|
{title} />
|
||||||
<label
|
<label
|
||||||
{...props}
|
|
||||||
for={id}
|
for={id}
|
||||||
class={_class}
|
class={_class}
|
||||||
tabindex={tabIndex}
|
on:click
|
||||||
on:click|preventDefault={handleClick}
|
on:click|preventDefault={() => {
|
||||||
on:keydown={handleKeyDown}>
|
selected = !selected;
|
||||||
|
}}
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
on:keydown
|
||||||
|
on:keydown={event => {
|
||||||
|
if (event.key === ' ' || event.key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
selected = !selected;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
{tabindex}
|
||||||
|
{style}>
|
||||||
<span class={cx('--tile__checkmark')}>
|
<span class={cx('--tile__checkmark')}>
|
||||||
<CheckmarkFilled16 aria-label={iconDescription} title={iconDescription} />
|
<CheckmarkFilled16 aria-label={iconDescription} title={iconDescription} />
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -28,9 +28,9 @@
|
||||||
<ClickableTile {...$$props}>Clickable Tile</ClickableTile>
|
<ClickableTile {...$$props}>Clickable Tile</ClickableTile>
|
||||||
{:else if story === 'multi-select'}
|
{:else if story === 'multi-select'}
|
||||||
<div role="group" aria-label="selectable tiles">
|
<div role="group" aria-label="selectable tiles">
|
||||||
<SelectableTile id="tile-1" name="tiles" {...$$props}>Multi-select Tile</SelectableTile>
|
<SelectableTile {...$$props} id="tile-1" name="tiles">Multi-select Tile</SelectableTile>
|
||||||
<SelectableTile id="tile-2" name="tiles" {...$$props}>Multi-select Tile</SelectableTile>
|
<SelectableTile {...$$props} id="tile-2" name="tiles">Multi-select Tile</SelectableTile>
|
||||||
<SelectableTile id="tile-3" name="tiles" {...$$props}>Multi-select Tile</SelectableTile>
|
<SelectableTile {...$$props} id="tile-3" name="tiles">Multi-select Tile</SelectableTile>
|
||||||
</div>
|
</div>
|
||||||
{:else if story === 'selectable'}
|
{:else if story === 'selectable'}
|
||||||
<TileGroup legend="Selectable Tile Group">
|
<TileGroup legend="Selectable Tile Group">
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
let className = undefined;
|
let className = undefined;
|
||||||
export { className as class };
|
export { className as class };
|
||||||
export let light = false;
|
export let light = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
$: _class = cx('--tile', light && '--tile--light', className);
|
const _class = cx('--tile', light && '--tile--light', className);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class={_class} {...props}>
|
<div on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
<script>
|
<script>
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
export let style = undefined;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<span class={cx('--tile-content__above-the-fold')}>
|
<span
|
||||||
|
on:click
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
class={cx('--tile-content__above-the-fold')}
|
||||||
|
{style}>
|
||||||
<slot />
|
<slot />
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
<script>
|
<script>
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
export let style = undefined;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<span class={cx('--tile-content__below-the-fold')}>
|
<span
|
||||||
|
on:click
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
class={cx('--tile-content__below-the-fold')}
|
||||||
|
{style}>
|
||||||
<slot />
|
<slot />
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -3,13 +3,14 @@
|
||||||
export { className as class };
|
export { className as class };
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
export let legend = '';
|
export let legend = '';
|
||||||
|
export let style = undefined;
|
||||||
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
$: _class = cx('--tile-group', className);
|
const _class = cx('--tile-group', className);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<fieldset class={_class} {disabled}>
|
<fieldset class={_class} {disabled} {style}>
|
||||||
{#if legend}
|
{#if legend}
|
||||||
<legend>{legend}</legend>
|
<legend>{legend}</legend>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue