-
Notifications
You must be signed in to change notification settings - Fork 20
/
Input.svelte
75 lines (64 loc) · 1.51 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
<script>
import {getEventsAction} from './utils';
import {getContext} from 'svelte';
export let value = '';
export let type = 'text';
export let error = null;
export let success = null;
export let password = false;
export let number = false;
export let textarea = false;
export let color = false;
export let date = false;
export let range = false;
const events = getEventsAction();
const onInput = e => {
const type = e.target.type;
const val = e.target.value;
if(type === 'number' || type === 'range')
value = val === '' ? undefined : +val;
else
value = val;
}
let getState = getContext('field:state');
let state_unsubscribe = false;
if(getState) {
state_unsubscribe = getState.subscribe(state => {
if(state === 'error')
error = true;
else if(state === 'success')
success = true;
else
success = error = false;
});
}
$: if(password) type = 'password';
$: if(number) type = 'number';
$: if(textarea) type = 'textarea';
$: if(color) type = 'color';
$: if(date) type = 'date';
$: if(range) type = 'range';
</script>
{#if type === 'textarea'}
<textarea
class:error
class:success
use:events
{...$$restProps}
on:input={onInput}
>{value}</textarea>
{:else}
<input type={type}
class:error
class:success
use:events
{...$$restProps}
on:input={onInput}
{value}
/>
{/if}
<style>
input[type=range]::-moz-range-track,input[type=range]::-webkit-slider-runnable-track {
background-color: var(--color-primary);
}
</style>