refactor(text-input): remove dispatch, forward events

Supports #7

- Forward events, inline functions
- Add style prop
- Remove exported props
- Bind value for TextInput
This commit is contained in:
Eric Liu 2019-12-20 17:34:47 -08:00
commit 865aeb0748
4 changed files with 18 additions and 40 deletions

View file

@ -16,15 +16,13 @@
export let tooltipAlignment = 'center'; export let tooltipAlignment = 'center';
export let hidePasswordLabel = 'Hide password'; export let hidePasswordLabel = 'Hide password';
export let showPasswordLabel = 'Show password'; export let showPasswordLabel = 'Show password';
export let props = {}; 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 View16 from 'carbon-icons-svelte/lib/View16'; import View16 from 'carbon-icons-svelte/lib/View16';
import ViewOff16 from 'carbon-icons-svelte/lib/ViewOff16'; import ViewOff16 from 'carbon-icons-svelte/lib/ViewOff16';
import { cx } from '../../lib'; import { cx } from '../../lib';
const dispatch = createEventDispatcher();
const errorId = `${id}-error`; const errorId = `${id}-error`;
const _labelClass = cx( const _labelClass = cx(
'--label', '--label',
@ -47,14 +45,14 @@
tooltipPosition && `--tooltip--${tooltipPosition}`, tooltipPosition && `--tooltip--${tooltipPosition}`,
tooltipAlignment && `--tooltip--align-${tooltipAlignment}` tooltipAlignment && `--tooltip--align-${tooltipAlignment}`
); );
$: passwordIsVisible = type === 'text'; $: passwordIsVisible = type === 'text';
</script> </script>
<div class={cx('--form-item', '--text-input-wrapper', '--password-input-wrapper')}> <div class={cx('--form-item', '--text-input-wrapper', '--password-input-wrapper')} {style}>
{#if labelText} {#if labelText}
<label for={id} class={_labelClass}>{labelText}</label> <label for={id} class={_labelClass}>{labelText}</label>
{/if} {/if}
{#if helperText} {#if helperText}
<div class={_helperTextClass}>{helperText}</div> <div class={_helperTextClass}>{helperText}</div>
{/if} {/if}
@ -63,23 +61,12 @@
<WarningFilled16 class={cx('--text-input__invalid-icon')} /> <WarningFilled16 class={cx('--text-input__invalid-icon')} />
{/if} {/if}
<input <input
{...props}
class={_textInputClass} class={_textInputClass}
on:click={event => { on:click
if (!disabled) { on:change
dispatch('click', event); on:input
}
}}
on:change={event => {
if (!disabled) {
dispatch('change', event);
}
}}
on:input={event => { on:input={event => {
value = event.target.value; value = event.target.value;
if (!disabled) {
dispatch('input', event);
}
}} }}
data-invalid={invalid || undefined} data-invalid={invalid || undefined}
aria-invalid={invalid || undefined} aria-invalid={invalid || undefined}

View file

@ -2,14 +2,14 @@
let className = undefined; let className = undefined;
export { className as class }; export { className as class };
export let hideLabel = false; export let hideLabel = false;
export let props = {}; export let style = undefined;
import { cx } from '../../lib'; import { cx } from '../../lib';
const _class = cx('--form-item', className); const _class = cx('--form-item', className);
</script> </script>
<div {...props} class={_class}> <div on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}>
{#if !hideLabel} {#if !hideLabel}
<span class={cx('--label', '--skeleton')} /> <span class={cx('--label', '--skeleton')} />
{/if} {/if}

View file

@ -36,6 +36,11 @@
</button> </button>
</div> </div>
{:else} {:else}
<TextInput {...$$props} bind:value /> <TextInput
{...$$props}
bind:value
on:change={() => {
console.log('change');
}} />
{/if} {/if}
</Layout> </Layout>

View file

@ -12,13 +12,11 @@
export let helperText = ''; export let helperText = '';
export let hideLabel = false; export let hideLabel = false;
export let light = false; export let light = false;
export let props = {}; 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();
const errorId = `${id}-error`; const errorId = `${id}-error`;
const _labelClass = cx( const _labelClass = cx(
'--label', '--label',
@ -34,7 +32,7 @@
); );
</script> </script>
<div class={cx('--form-item', '--text-input-wrapper')}> <div class={cx('--form-item', '--text-input-wrapper')} {style}>
{#if labelText} {#if labelText}
<label for={id} class={_labelClass}>{labelText}</label> <label for={id} class={_labelClass}>{labelText}</label>
{/if} {/if}
@ -46,23 +44,11 @@
<WarningFilled16 class={cx('--text-input__invalid-icon')} /> <WarningFilled16 class={cx('--text-input__invalid-icon')} />
{/if} {/if}
<input <input
{...props}
class={_textInputClass} class={_textInputClass}
on:click={event => { on:click
if (!disabled) { on:change
dispatch('click', event);
}
}}
on:change={event => {
if (!disabled) {
dispatch('change', event);
}
}}
on:input={event => { on:input={event => {
value = event.target.value; value = event.target.value;
if (!disabled) {
dispatch('input', event);
}
}} }}
data-invalid={invalid || undefined} data-invalid={invalid || undefined}
aria-invalid={invalid || undefined} aria-invalid={invalid || undefined}