/
Input.svelte
159 lines (140 loc) · 3.74 KB
/
Input.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<script>
import { onMount, getContext, tick } from 'svelte'
import { omit } from '../utils'
import Icon from './Icon.svelte'
/** Binding value
* @svelte-prop {String|Number} [value]
* */
export let value = ''
/** Input type, or <code>textarea</code>
* @svelte-prop {String} [type=text]
* @values Any native type, <code>textarea</code>
* */
export let type = 'text'
/** Size of input
* @svelte-prop {String} [size]
* @values $$sizes$$
* */
export let size = ''
/** Show the password reveal toggle button
* @svelte-prop {boolean} [passwordReveal]
* */
export let passwordReveal = false
/** Set input maxlength and show a counter
* @svelte-prop {Number} [maxlength]
* */
export let maxlength = null
/** Show the character counter when <code>maxlength<code> is set
* @svelte-prop {boolean} [hasCounter=true]
* */
export let hasCounter = true
/** Show loading indicator
* @svelte-prop {boolean} [loading=false]
* */
export let loading = false
/** Input is disabled
* @svelte-prop {boolean} [disabled=false]
* */
export let disabled = false
let input
let isFocused
let isPasswordVisible = false
let newType = 'text'
let statusType = ''
let statusTypeIcon = ''
let valueLength = null
const getType = getContext('type')
if (getType) statusType = getType() || ''
$: props = {
...omit($$props, 'class', 'value', 'type', 'size', 'passwordReveal', 'hasCounter', 'loading', 'disabled'),
}
$: hasIconRight = passwordReveal || loading || statusType
$: passwordVisibleIcon = isPasswordVisible ? 'eye-slash' : 'eye'
$: {
switch (statusType) {
case 'is-success':
statusTypeIcon = 'check'
break
case 'is-danger':
statusTypeIcon = 'exclamation-circle'
break
case 'is-info':
statusTypeIcon = 'info-circle'
break
case 'is-warning':
statusTypeIcon = 'exclamation-triangle'
break
}
}
$: {
if (typeof value === 'string') {
valueLength = value.length
} else if (typeof value === 'number') {
valueLength = value.toString().length
} else {
valueLength = 0
}
}
onMount(() => {
newType = type
})
async function togglePasswordVisibility() {
isPasswordVisible = !isPasswordVisible
newType = isPasswordVisible ? 'text' : 'password'
await tick()
input.focus()
}
const onInput = e => {
value = e.target.value
$$props.value = value
}
const onFocus = () => (isFocused = true)
const onBlur = () => (isFocused = false)
</script>
<style>
.control .help.counter {
float: right;
margin-left: 0.5em;
}
</style>
<div class="control" class:has-icons-right={hasIconRight} class:is-loading={loading}>
{#if type !== 'textarea'}
<input
{...props}
type={newType}
{value}
class="input {statusType}
{size}
{$$props.class || ''}"
bind:this={input}
on:input={onInput}
on:focus={onFocus}
on:blur={onBlur}
{disabled} />
{:else}
<textarea
{...props}
{value}
class="textarea {statusType}
{size}"
bind:this={input}
on:input={onInput}
on:focus={onFocus}
on:blur={onBlur}
{disabled} />
{/if}
{#if !loading && (passwordReveal || statusType)}
<!-- pack={iconPack}
size={iconSize} -->
<Icon
pack="fas"
isRight={true}
isClickable={passwordReveal}
icon={passwordReveal ? passwordVisibleIcon : statusTypeIcon}
type={!passwordReveal ? statusType : 'is-primary'}
on:click={togglePasswordVisibility} />
{/if}
{#if maxlength && hasCounter && type !== 'number'}
<small class="help counter" class:is-invisible={!isFocused}>{valueLength} / {maxlength}</small>
{/if}
</div>