-
Notifications
You must be signed in to change notification settings - Fork 4
/
PropertySearchField.svelte
101 lines (85 loc) · 2.28 KB
/
PropertySearchField.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
<style>
.equals {
width: 70px;
}
.range {
width: 30px;
}
</style>
<span>
<select bind:value=op>
<option value=""></option>
<option value="equals">equals</option>
<!-- {#if datatype === 'number'} --> // removing this so we can sort things like 8601 dates
<option value="range">range</option>
<!-- {/if} -->
</select>
</span>
<span>
{#if op != null && op != ''}
{#if op === 'equals'}
<input
class="equals" type="text" placeholder="value(s)"
on:keydown="event.stopPropagation()"
bind:value=equals
>
{:elseif op === 'range'}
<input
class="range" type="text" placeholder="min"
on:keydown="event.stopPropagation()"
bind:value=min
>
<input
class="range" type="text" placeholder="max"
on:keydown="event.stopPropagation()"
bind:value=max
>
{/if}
{/if}
</span>
<script>
import _ from 'lodash';
// number of milliseconds to delay input update events
const INPUT_DEBOUNCE_TIME = 500;
export default {
data() {
return {
prop: null,
datatype: null,
initial: null,
op: null,
equals: '',
min: '',
max: ''
}
},
onstate({ changed, current, previous }) {
// first-time initialization with provided values
if (!previous && current.initial) {
const data = current.initial;
this.set({
op: data.op,
equals: data.equals != null ? data.equals : '',
min: data.min != null ? data.min : '',
max: data.max != null ? data.max : '',
});
}
// send updates to parent component
else if (changed.op || changed.equals || changed.min || changed.max) {
let { op, equals, min, max } = current;
const { datatype } = this.get();
// add explicit quotes to numeric searches on string fields
if (datatype === 'string' && equals != null && equals.match(/^\d+$/)) {
equals = `"${equals}"`;
}
this.updateField({ prop: current.prop, values: { op, equals, min, max } });
}
},
methods: {
// debounced update event to avoid spamming on rapid UI changes
updateField: _.debounce(function({ prop, values }) {
this.fire('update', { prop, values });
}, INPUT_DEBOUNCE_TIME)
}
};
</script>