/
Textfield.svelte
138 lines (127 loc) · 4.49 KB
/
Textfield.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{#if valued}
<label
bind:this={element}
use:useActions={use}
use:forwardEvents
class="mdc-text-field {className}"
class:mdc-text-field--disabled={disabled}
class:mdc-text-field--fullwidth={fullwidth}
class:mdc-text-field--textarea={textarea}
class:mdc-text-field--outlined={variant === 'outlined' && !fullwidth}
class:mdc-text-field--dense={dense}
class:mdc-text-field--no-label={noLabel || label == null}
class:mdc-text-field--with-leading-icon={withLeadingIcon}
class:mdc-text-field--with-trailing-icon={withTrailingIcon}
{...exclude($$props, ['use', 'class', 'ripple', 'disabled', 'fullwidth', 'textarea', 'variant', 'dense', 'withLeadingIcon', 'withTrailingIcon', 'noLabel', 'label', 'type', 'value', 'input$', 'label$', 'ripple$', 'outline$'])}
>
<slot></slot>
{#if textarea}
<Textarea
{disabled}
bind:value
bind:dirty
bind:invalid
on:change
on:input
{...prefixFilter($$props, 'input$')}
/>
{:else}
<Input
{type}
{disabled}
bind:value
bind:dirty
bind:invalid
on:change
on:input
{...placeholderProp}
{...prefixFilter($$props, 'input$')}
/>
{/if}
{#if !textarea && variant !== 'outlined'}
{#if !noLabel && label != null && !fullwidth}
<FloatingLabel wrapped {...prefixFilter($$props, 'label$')}>{label}<slot name="label"></slot></FloatingLabel>
{/if}
{#if ripple}
<LineRipple {...prefixFilter($$props, 'ripple$')} />
{/if}
{/if}
{#if textarea || (variant === 'outlined' && !fullwidth)}
<NotchedOutline noLabel={noLabel || label == null} {...prefixFilter($$props, 'outline$')}>
{#if !noLabel && label != null}
<FloatingLabel wrapped {...prefixFilter($$props, 'label$')}>{label}<slot name="label"></slot></FloatingLabel>
{/if}
</NotchedOutline>
{/if}
</label>
{:else}
<div
bind:this={element}
use:useActions={use}
use:forwardEvents
class="mdc-text-field {className}"
class:mdc-text-field--disabled={disabled}
class:mdc-text-field--fullwidth={fullwidth}
class:mdc-text-field--textarea={textarea}
class:mdc-text-field--outlined={variant === 'outlined' && !fullwidth}
class:mdc-text-field--dense={dense}
class:mdc-text-field--no-label={noLabel}
class:mdc-text-field--with-leading-icon={withLeadingIcon}
class:mdc-text-field--with-trailing-icon={withTrailingIcon}
{...exclude($$props, ['use', 'class', 'ripple', 'disabled', 'fullwidth', 'textarea', 'variant', 'dense', 'withLeadingIcon', 'withTrailingIcon', 'noLabel', 'label', 'type', 'value', 'input$', 'label$', 'ripple$', 'outline$'])}
>
<slot></slot>
</div>
{/if}
<script>
import {MDCTextField} from '@material/textfield';
import {onMount, onDestroy} from 'svelte';
import {current_component} from 'svelte/internal';
import FloatingLabel from '../floating-label/FloatingLabel.svelte';
import LineRipple from '../line-ripple/LineRipple.svelte';
import NotchedOutline from '../notched-outline/NotchedOutline.svelte';
import {forwardEventsBuilder} from '../forwardEvents.js';
import {exclude} from '../exclude.js';
import {prefixFilter} from '../prefixFilter.js';
import {useActions} from '../useActions.js';
import Input from './Input.svelte';
import Textarea from './Textarea.svelte';
const forwardEvents = forwardEventsBuilder(current_component);
let uninitializedValue = () => {};
export let use = [];
let className = '';
export {className as class};
export let ripple = true;
export let disabled = false;
export let fullwidth = false;
export let textarea = false;
export let variant = 'filled';
export let dense = false;
export let withLeadingIcon = false;
export let withTrailingIcon = false;
export let noLabel = false;
export let label = null;
export let type = 'text';
export let value = uninitializedValue;
export let dirty = false;
export let invalid = false;
let element;
let textField;
$: valued = value !== uninitializedValue;
$: placeholderProp = (fullwidth && label) ? {placeholder: label} : {};
$: if (textField && textField.disabled !== disabled) {
textField.disabled = disabled;
}
$: if (textField && valued && textField.value !== value) {
textField.value = value;
}
onMount(() => {
textField = new MDCTextField(element);
if (!ripple) {
textField.ripple.destroy();
}
});
onDestroy(() => {
textField.destroy();
});
</script>