Merge pull request #87 from metonym/refactor

refactor(components): remove unnecessary dispatch, forward events
This commit is contained in:
Eric Liu 2019-12-29 19:54:12 -08:00 committed by GitHub
commit d55aacaf27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 114 additions and 127 deletions

View file

@ -3,9 +3,9 @@
export { className as class }; export { className as class };
export let style = undefined; export let style = undefined;
import { cx } from '../../lib';
import SkeletonText from '../SkeletonText'; import SkeletonText from '../SkeletonText';
import { ButtonSkeleton } from '../Button'; import { ButtonSkeleton } from '../Button';
import { cx } from '../../lib';
</script> </script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>

View file

@ -16,20 +16,12 @@
import { cx } from '../../lib'; import { cx } from '../../lib';
let inputRef = undefined; let inputRef = undefined;
$: _class = cx(
'--btn',
'--btn--sm',
kind && `--btn--${kind}`,
disabled && '--btn--disabled',
className
);
</script> </script>
<label <label
tabindex={disabled ? '-1' : tabindex} tabindex={disabled ? '-1' : tabindex}
aria-disabled={disabled} aria-disabled={disabled}
class={_class} class={cx('--btn', '--btn--sm', kind && `--btn--${kind}`, disabled && '--btn--disabled', className)}
for={id} for={id}
on:keydown on:keydown
on:keydown={({ key }) => { on:keydown={({ key }) => {
@ -54,8 +46,8 @@
} }
}} }}
on:click on:click
on:click={event => { on:click={({ target }) => {
event.target.value = null; target.value = null;
}} }}
{id} {id}
{disabled} {disabled}

View file

@ -26,9 +26,6 @@
<p class={cx('--file-filename')}>{name}</p> <p class={cx('--file-filename')}>{name}</p>
<span class={cx('--file__state-container')}> <span class={cx('--file__state-container')}>
<Filename <Filename
{iconDescription}
{status}
{invalid}
on:keydown={({ key }) => { on:keydown={({ key }) => {
if (key === ' ' || key === 'Enter') { if (key === ' ' || key === 'Enter') {
dispatch('delete', id); dispatch('delete', id);
@ -36,7 +33,10 @@
}} }}
on:click={() => { on:click={() => {
dispatch('delete', id); dispatch('delete', id);
}} /> }}
{iconDescription}
{status}
{invalid} />
</span> </span>
{#if invalid && errorSubject} {#if invalid && errorSubject}
<div class={cx('--form-requirement')}> <div class={cx('--form-requirement')}>

View file

@ -15,7 +15,7 @@
</script> </script>
{#if status === 'uploading'} {#if status === 'uploading'}
<Loading description={iconDescription} withOverlay={false} small class={className} {style} /> <Loading small description={iconDescription} withOverlay={false} class={className} {style} />
{/if} {/if}
{#if status === 'edit'} {#if status === 'edit'}

View file

@ -4,10 +4,8 @@
export let style = undefined; export let style = undefined;
import { cx } from '../../lib'; import { cx } from '../../lib';
$: _class = cx('--form', className);
</script> </script>
<form on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}> <form on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form', className)} {style}>
<slot /> <slot />
</form> </form>

View file

@ -8,20 +8,17 @@
export let style = undefined; export let style = undefined;
import { cx } from '../../lib'; import { cx } from '../../lib';
$: _class = cx('--fieldset', className);
$: _legendClass = cx('--label', className);
</script> </script>
<fieldset <fieldset
data-invalid={invalid ? '' : undefined} data-invalid={invalid || undefined}
class={cx('--fieldset', className)}
on:click on:click
on:mouseover on:mouseover
on:mouseenter on:mouseenter
on:mouseleave on:mouseleave
class={_class}
{style}> {style}>
<legend class={_legendClass}>{legendText}</legend> <legend class={cx('--label', className)}>{legendText}</legend>
<slot /> <slot />
{#if message} {#if message}
<div class={cx('--form__requirements')}>{messageText}</div> <div class={cx('--form__requirements')}>{messageText}</div>

View file

@ -6,6 +6,14 @@
import SearchSkeleton from './Search.Skeleton.svelte'; import SearchSkeleton from './Search.Skeleton.svelte';
let value = ''; let value = '';
$: {
if (!value.length) {
console.log('cleared');
} else {
console.log('value', value);
}
}
</script> </script>
<Layout> <Layout>

View file

@ -20,7 +20,7 @@ export const Default = () => ({
'The label text for the close button (closeButtonLabelText)', 'The label text for the close button (closeButtonLabelText)',
'Clear search input' 'Clear search input'
), ),
placeHolderText: text('Placeholder text (placeHolderText)', 'Search') placeholder: text('Placeholder text (placeholder)', 'Search')
} }
}); });

View file

@ -5,7 +5,7 @@
export let autofocus = false; export let autofocus = false;
export let type = 'text'; export let type = 'text';
export let small = false; export let small = false;
export let placeHolderText = ''; export let placeholder = '';
export let labelText = ''; export let labelText = '';
export let closeButtonLabelText = 'Clear search input'; export let closeButtonLabelText = 'Clear search input';
export let size = small ? 'sm' : 'xl'; export let size = small ? 'sm' : 'xl';
@ -13,14 +13,11 @@
export let id = Math.random(); export let id = Math.random();
export let style = undefined; export let style = undefined;
import { createEventDispatcher } from 'svelte';
import Search16 from 'carbon-icons-svelte/lib/Search16'; import Search16 from 'carbon-icons-svelte/lib/Search16';
import Close16 from 'carbon-icons-svelte/lib/Close16'; import Close16 from 'carbon-icons-svelte/lib/Close16';
import Close20 from 'carbon-icons-svelte/lib/Close20'; import Close20 from 'carbon-icons-svelte/lib/Close20';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
let inputRef = undefined; let inputRef = undefined;
</script> </script>
@ -34,7 +31,6 @@
bind:this={inputRef} bind:this={inputRef}
role="searchbox" role="searchbox"
class={cx('--search-input')} class={cx('--search-input')}
placeholder={placeHolderText}
on:change on:change
on:input on:input
on:input={({ target }) => { on:input={({ target }) => {
@ -43,14 +39,14 @@
{autofocus} {autofocus}
{type} {type}
{id} {id}
{value} /> {value}
{placeholder} />
<button <button
type="button" type="button"
class={cx('--search-close', value === '' && '--search-close--hidden')} class={cx('--search-close', value === '' && '--search-close--hidden')}
on:click on:click
on:click={() => { on:click={() => {
value = ''; value = '';
dispatch('change', value);
inputRef.focus(); inputRef.focus();
}} }}
aria-label={closeButtonLabelText}> aria-label={closeButtonLabelText}>

View file

@ -8,7 +8,7 @@ export const Default = () => ({
props: { props: {
heading: boolean('Skeleton text at a larger size (heading)'), heading: boolean('Skeleton text at a larger size (heading)'),
paragraph: boolean('Use multiple lines of text (paragraph)'), paragraph: boolean('Use multiple lines of text (paragraph)'),
lineCount: number('The number of lines in a paragraph (lineCount)', 3), lines: number('The number of lines in a paragraph (lines)', 3),
width: select( width: select(
'Width (in px or %) of single line of text or max-width of paragraph lines (width)', 'Width (in px or %) of single line of text or max-width of paragraph lines (width)',
{ '100%': '100%', '250px': '250px' }, { '100%': '100%', '250px': '250px' },

View file

@ -2,7 +2,7 @@
let className = undefined; let className = undefined;
export { className as class }; export { className as class };
export let paragraph = false; export let paragraph = false;
export let lineCount = 3; export let lines = 3;
export let width = '100%'; export let width = '100%';
export let heading = false; export let heading = false;
export let style = undefined; export let style = undefined;
@ -10,26 +10,27 @@
import { cx } from '../../lib'; import { cx } from '../../lib';
const randoms = [0.973051493507435, 0.15334737213558558, 0.5671034553053769]; const randoms = [0.973051493507435, 0.15334737213558558, 0.5671034553053769];
let lines = [];
let rows = [];
$: widthNum = parseInt(width, 10); $: widthNum = parseInt(width, 10);
$: widthPx = width.includes('px'); $: widthPx = width.includes('px');
$: widthPercent = width.includes('%'); $: widthPercent = width.includes('%');
$: if (paragraph) { $: if (paragraph) {
for (let i = 0; i < lineCount; i++) { for (let i = 0; i < lines; i++) {
const min = widthPx ? widthNum - 75 : 0; const min = widthPx ? widthNum - 75 : 0;
const max = widthPx ? widthNum : 75; const max = widthPx ? widthNum : 75;
const randomWidth = Math.floor(randoms[i % 3] * (max - min + 1)) + min + 'px'; const randomWidth = Math.floor(randoms[i % 3] * (max - min + 1)) + min + 'px';
lines = [...lines, { width: widthPx ? randomWidth : `calc(${width} - ${randomWidth})` }]; rows = [...rows, { width: widthPx ? randomWidth : `calc(${width} - ${randomWidth})` }];
} }
} }
</script> </script>
{#if paragraph} {#if paragraph}
<div on:click on:mouseover on:mouseenter on:mouseleave {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={className} {style}>
{#each lines as { width }} {#each rows as { width }, i (width)}
<p <p
class={cx('--skeleton__text', heading && '--skeleton__heading', className)} class={cx('--skeleton__text', heading && '--skeleton__heading')}
style={`width: ${width};`} /> style={`width: ${width};`} />
{/each} {/each}
</div> </div>

View file

@ -1,12 +1,12 @@
<script> <script>
let className = undefined; let className = undefined;
export { className as class }; export { className as class };
export let rows = 4;
export let cols = 50; export let cols = 50;
export let disabled = false; export let disabled = false;
export let id = Math.random(); export let id = Math.random();
export let labelText = ''; export let labelText = '';
export let placeholder = ''; export let placeholder = '';
export let rows = 4;
export let value = ''; export let value = '';
export let invalid = false; export let invalid = false;
export let invalidText = ''; export let invalidText = '';
@ -15,20 +15,17 @@
export let light = false; export let light = false;
export let style = undefined; export let style = undefined;
import { createEventDispatcher } from 'svelte';
import WarningFilled16 from 'carbon-icons-svelte/lib/WarningFilled16'; import WarningFilled16 from 'carbon-icons-svelte/lib/WarningFilled16';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
$: errorId = `error-${id}`; $: errorId = `error-${id}`;
</script> </script>
<div on:mouseover on:mouseenter on:mouseleave class={cx('--form-item')} {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item')} {style}>
{#if labelText && !hideLabel} {#if labelText && !hideLabel}
<label <label
for={id} class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}
class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}> for={id}>
{labelText} {labelText}
</label> </label>
{/if} {/if}
@ -42,32 +39,22 @@
<WarningFilled16 class={cx('--text-area__invalid-icon')} /> <WarningFilled16 class={cx('--text-area__invalid-icon')} />
{/if} {/if}
<textarea <textarea
on:click={event => {
if (!disabled) {
dispatch('click', event);
}
}}
on:change={event => {
if (!disabled) {
dispatch('change', event);
}
}}
on:input={event => {
value = event.target.value;
if (!disabled) {
dispatch('input', event);
}
}}
class={cx('--text-area', light && '--text-area--light', invalid && '--text-area--invalid', className)}
aria-invalid={invalid || undefined} aria-invalid={invalid || undefined}
aria-describedby={invalid ? errorId : undefined} aria-describedby={invalid ? errorId : undefined}
class={cx('--text-area', light && '--text-area--light', invalid && '--text-area--invalid', className)}
on:change
on:input
on:input={({ target }) => {
value = target.value;
}}
on:focus
on:blur
{disabled} {disabled}
{id} {id}
{cols} {cols}
{rows} {rows}
{value} {value}
{placeholder} {placeholder} />
{value} />
</div> </div>
{#if invalid} {#if invalid}
<div class={cx('--form-requirement')} id={errorId}>{invalidText}</div> <div class={cx('--form-requirement')} id={errorId}>{invalidText}</div>

View file

@ -26,11 +26,17 @@
$: errorId = `error-${id}`; $: errorId = `error-${id}`;
</script> </script>
<div class={cx('--form-item', '--text-input-wrapper', '--password-input-wrapper')} {style}> <div
on:click
on:mouseover
on:mouseenter
on:mouseleave
class={cx('--form-item', '--text-input-wrapper', '--password-input-wrapper', className)}
{style}>
{#if labelText} {#if labelText}
<label <label
for={id} class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}
class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}> for={id}>
{labelText} {labelText}
</label> </label>
{/if} {/if}
@ -44,16 +50,17 @@
<WarningFilled16 class={cx('--text-input__invalid-icon')} /> <WarningFilled16 class={cx('--text-input__invalid-icon')} />
{/if} {/if}
<input <input
class={cx('--text-input', '--password-input', light && '--text-input--light', invalid && '--text-input--invalid', className)}
on:click
on:change
on:input
on:input={event => {
value = event.target.value;
}}
data-invalid={invalid || undefined} data-invalid={invalid || undefined}
aria-invalid={invalid || undefined} aria-invalid={invalid || undefined}
aria-describedby={invalid ? errorId : undefined} aria-describedby={invalid ? errorId : undefined}
class={cx('--text-input', '--password-input', light && '--text-input--light', invalid && '--text-input--invalid')}
on:change
on:input
on:input={({ target }) => {
value = target.value;
}}
on:focus
on:blur
{id} {id}
{placeholder} {placeholder}
{type} {type}

View file

@ -20,7 +20,13 @@
$: errorId = `error-${id}`; $: errorId = `error-${id}`;
</script> </script>
<div class={cx('--form-item', '--text-input-wrapper')} {style}> <div
on:click
on:mouseover
on:mouseenter
on:mouseleave
class={cx('--form-item', '--text-input-wrapper', className)}
{style}>
{#if labelText} {#if labelText}
<label <label
for={id} for={id}
@ -38,16 +44,17 @@
<WarningFilled16 class={cx('--text-input__invalid-icon')} /> <WarningFilled16 class={cx('--text-input__invalid-icon')} />
{/if} {/if}
<input <input
class={cx('--text-input', light && '--text-input--light', invalid && '--text-input--invalid', className)}
on:click
on:change
on:input
on:input={event => {
value = event.target.value;
}}
data-invalid={invalid || undefined} data-invalid={invalid || undefined}
aria-invalid={invalid || undefined} aria-invalid={invalid || undefined}
aria-describedby={invalid ? errorId : undefined} aria-describedby={invalid ? errorId : undefined}
class={cx('--text-input', light && '--text-input--light', invalid && '--text-input--invalid')}
on:change
on:input
on:input={({ target }) => {
value = target.value;
}}
on:focus
on:blur
{id} {id}
{placeholder} {placeholder}
{type} {type}

View file

@ -1,12 +1,16 @@
<script> <script>
import Layout from '../../internal/ui/Layout.svelte'; import Layout from '../../internal/ui/Layout.svelte';
import { SelectItem } from '../Select';
import TimePicker from './TimePicker.svelte'; import TimePicker from './TimePicker.svelte';
import TimePickerSelect from './TimePickerSelect.svelte'; import TimePickerSelect from './TimePickerSelect.svelte';
import { SelectItem } from '../Select';
let value = ''; let value = '';
let select1 = 'PM'; let select1 = 'PM';
let select2 = 'Time zone 1'; let select2 = 'Time zone 1';
$: {
console.log(value, select1, select2);
}
</script> </script>
<Layout> <Layout>

View file

@ -23,18 +23,21 @@
<div class={cx('--time-picker__input')}> <div class={cx('--time-picker__input')}>
{#if labelText} {#if labelText}
<label <label
for={id} class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}
class={cx('--label', hideLabel && '--visually-hidden', disabled && '--label--disabled')}> for={id}>
{labelText} {labelText}
</label> </label>
{/if} {/if}
<input <input
data-invalid={invalid || undefined} data-invalid={invalid || undefined}
class={cx('--time-picker__input-field', '--text-input', light && '--text-input--light', invalid && '--text-input--invalid')} class={cx('--time-picker__input-field', '--text-input', light && '--text-input--light', invalid && '--text-input--invalid')}
on:change
on:input on:input
on:input={({ target }) => { on:input={({ target }) => {
value = target.value; value = target.value;
}} }}
on:focus
on:blur
{pattern} {pattern}
{placeholder} {placeholder}
{maxlength} {maxlength}
@ -42,7 +45,6 @@
{type} {type}
{value} {value}
{disabled} /> {disabled} />
</div> </div>
<slot /> <slot />
</div> </div>

View file

@ -9,21 +9,16 @@
export let disabled = false; export let disabled = false;
export let style = undefined; export let style = undefined;
import { createEventDispatcher, setContext } from 'svelte'; import { setContext } from 'svelte';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import ChevronDownGlyph from 'carbon-icons-svelte/lib/ChevronDownGlyph'; import ChevronDownGlyph from 'carbon-icons-svelte/lib/ChevronDownGlyph';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
let selected = writable(value); let selected = writable(value);
setContext('TimePickerSelect', { selected }); setContext('TimePickerSelect', { selected });
$: { $: selected.set(value);
selected.set(value);
dispatch('change', $selected);
}
$: value = $selected; $: value = $selected;
</script> </script>
@ -39,12 +34,12 @@
{/if} {/if}
<select <select
class={cx('--select-input')} class={cx('--select-input')}
{id}
{disabled}
{value}
on:change={({ target }) => { on:change={({ target }) => {
selected.set(target.value); selected.set(target.value);
}}> }}
{id}
{disabled}
{value}>
<slot /> <slot />
</select> </select>
<ChevronDownGlyph <ChevronDownGlyph

View file

@ -11,7 +11,7 @@
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>
<input type="checkbox" class={cx('--toggle --skeleton')} {id} /> <input type="checkbox" class={cx('--toggle --skeleton')} {id} />
<label <label
aria-label={labelText ? null : $$props['aria-label'] || 'Toggle is loading'} aria-label={labelText ? undefined : $$props['aria-label'] || 'Toggle is loading'}
class={cx('--toggle__label', '--skeleton')} class={cx('--toggle__label', '--skeleton')}
for={id}> for={id}>
{#if labelText} {#if labelText}

View file

@ -4,11 +4,17 @@
import Layout from '../../internal/ui/Layout.svelte'; import Layout from '../../internal/ui/Layout.svelte';
import Toggle from './Toggle.svelte'; import Toggle from './Toggle.svelte';
import ToggleSkeleton from './Toggle.Skeleton.svelte'; import ToggleSkeleton from './Toggle.Skeleton.svelte';
let toggled = true;
$: {
console.log('toggled', toggled);
}
</script> </script>
<Layout> <Layout>
{#if story === 'toggled'} {#if story === 'toggled'}
<Toggle {...$$props} id="toggle-1" toggled /> <Toggle {...$$props} id="toggle-1" bind:toggled />
{:else if story === 'skeleton'} {:else if story === 'skeleton'}
<ToggleSkeleton /> <ToggleSkeleton />
{:else} {:else}

View file

@ -9,22 +9,11 @@
export let labelB = 'On'; export let labelB = 'On';
export let style = undefined; export let style = undefined;
import { createEventDispatcher, afterUpdate } from 'svelte';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
let inputRef = undefined;
afterUpdate(() => {
inputRef.checked = toggled;
dispatch('toggle', { id, toggled });
});
</script> </script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>
<input <input
bind:this={inputRef}
type="checkbox" type="checkbox"
class={cx('--toggle-input')} class={cx('--toggle-input')}
checked={toggled} checked={toggled}
@ -39,6 +28,8 @@
toggled = !toggled; toggled = !toggled;
} }
}} }}
on:focus
on:blur
{disabled} {disabled}
{id} /> {id} />
<label <label

View file

@ -4,11 +4,17 @@
import Layout from '../../internal/ui/Layout.svelte'; import Layout from '../../internal/ui/Layout.svelte';
import ToggleSmall from './ToggleSmall.svelte'; import ToggleSmall from './ToggleSmall.svelte';
import ToggleSmallSkeleton from './ToggleSmall.Skeleton.svelte'; import ToggleSmallSkeleton from './ToggleSmall.Skeleton.svelte';
let toggled = true;
$: {
console.log('toggled', toggled);
}
</script> </script>
<Layout> <Layout>
{#if story === 'toggled'} {#if story === 'toggled'}
<ToggleSmall {...$$props} id="toggle-1" toggled /> <ToggleSmall {...$$props} id="toggle-1" bind:toggled />
{:else if story === 'skeleton'} {:else if story === 'skeleton'}
<ToggleSmallSkeleton /> <ToggleSmallSkeleton />
{:else} {:else}

View file

@ -9,22 +9,11 @@
export let labelB = 'On'; export let labelB = 'On';
export let style = undefined; export let style = undefined;
import { createEventDispatcher, afterUpdate } from 'svelte';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
let inputRef = undefined;
afterUpdate(() => {
inputRef.checked = toggled;
dispatch('toggle', { id, toggled });
});
</script> </script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}> <div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>
<input <input
bind:this={inputRef}
type="checkbox" type="checkbox"
class={cx('--toggle-input', '--toggle-input--small')} class={cx('--toggle-input', '--toggle-input--small')}
checked={toggled} checked={toggled}
@ -39,9 +28,10 @@
toggled = !toggled; toggled = !toggled;
} }
}} }}
on:focus
on:blur
{disabled} {disabled}
{id} /> {id} />
<label <label
aria-label={labelText ? undefined : $$props['aria-label'] || 'Toggle'} aria-label={labelText ? undefined : $$props['aria-label'] || 'Toggle'}
class={cx('--toggle-input__label')} class={cx('--toggle-input__label')}