-
Notifications
You must be signed in to change notification settings - Fork 19
/
select-search.blade.php
199 lines (174 loc) · 8.63 KB
/
select-search.blade.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
@props([
'data' => [],
'placeholder' => 'Select an option',
'limit' => 40,
])
<div
x-data="AlpineSelect({
data: {{ json_encode($data) }},
selected: @entangle($attributes->wire('model')),
placeholder: '{{ $placeholder }}',
multiple: {{ isset($attributes['multiple']) ? 'true':'false' }},
disabled: {{ isset($attributes['disabled']) ? 'true':'false' }},
limit: {{ $limit }},
})"
x-init="init()"
@click.away="closeSelect()"
@keydown.escape="closeSelect()"
@keydown.arrow-down.prevent="increaseIndex()"
@keydown.arrow-up.prevent="decreaseIndex()"
@keydown.enter="selectOption(Object.keys(options)[currentIndex])"
>
<div
class="relative content-center w-full p-1 text-left bg-white border rounded-md sm:text-sm sm:leading-5"
x-bind:class="{'border-blue-300 ring ring-blue-200 ring-opacity-50':open, 'bg-gray-200 cursor-default':disabled}"
@click.prevent="toggleSelect()"
>
<div id="placeholder">
<div class="inline-block m-1" x-show="selected.length === 0" x-text="placeholder"> </div>
</div>
@isset($attributes['multiple'])
<div class="flex flex-wrap space-x-1" x-cloak x-show="selected.length > 0">
<template x-for="(key, index) in selected" :key="index">
<div class="text-gray-800 rounded-full truncate bg-blue-300 px-2 py-0.5 my-0.5 flex flex-row items-center">
<div class="px-2 truncate" x-text="data[key]"></div>
<div x-show="!disabled" x-bind:class="{'cursor-pointer':!disabled}" class="w-4" @click.prevent.stop="deselectOption(index)"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class = 'h-4 fill-current'><path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 16.538l-4.592-4.548 4.546-4.587-1.416-1.403-4.545 4.589-4.588-4.543-1.405 1.405 4.593 4.552-4.547 4.592 1.405 1.405 4.555-4.596 4.591 4.55 1.403-1.416z"/></svg></div>
</div>
</template>
</div>
@else
<div class="flex flex-wrap" x-cloak x-show="selected">
<div class="text-gray-800 rounded-full truncate bg-blue-300 px-2 py-0.5 my-0.5 flex flex-row items-center">
<div class="px-2 truncate" x-text="data[selected]"></div>
<div x-show="!disabled" x-bind:class="{'cursor-pointer':!disabled}" class="h-4" @click.prevent.stop="deselectOption()"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class = 'h-4 fill-current'><path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 16.538l-4.592-4.548 4.546-4.587-1.416-1.403-4.545 4.589-4.588-4.543-1.405 1.405 4.593 4.552-4.547 4.592 1.405 1.405 4.555-4.596 4.591 4.55 1.403-1.416z"/></svg></div>
</div>
</div>
@endif
<div
class="mt-0.5 w-full bg-white border-gray-300 rounded-b-md border absolute top-full left-0 z-30"
x-show="open"
x-cloak
>
<div class="relative z-30 w-full p-2 bg-white">
<input type="search" x-model="search" x-on:click.prevent.stop="open=true" class="block w-full p-2 border border-gray-300 rounded-md focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50 sm:text-sm sm:leading-5">
</div>
<div x-ref="dropdown" class="relative z-30 p-2 overflow-y-auto max-h-60" >
<div x-cloak x-show="Object.keys(options).length === 0" x-text="emptyOptionsMessage">Gragr</div>
<template x-for="(key, index) in Object.keys(options)" :key="index" >
@isset($attributes['multiple'])
<div
class="px-2 py-1"
x-bind:class="{'bg-gray-300 text-white hover:none':selected.includes(key), 'hover:bg-blue-500 hover:text-white cursor-pointer':!(selected.includes(key)), 'bg-blue-500 text-white':currentIndex==index}"
@click.prevent.stop="selectOption(key)"
x-text="Object.values(options)[index]">
</div>
@else
<div
class="px-2 py-1"
x-bind:class="{'bg-gray-300 text-white hover:none':selected==key, 'hover:bg-blue-500 hover:text-white cursor-pointer':!(selected==key), 'bg-blue-500 text-white':currentIndex==index}"
@click.prevent.stop="selectOption(key)"
x-text="Object.values(options)[index]">
</div>
@endisset
</template>
</div>
</div>
</div>
</div>
@once
<script>
function AlpineSelect(config) {
return {
data: config.data ?? [],
open: false,
search: '',
options: {},
emptyOptionsMessage: 'No results match your search.',
placeholder: config.placeholder,
selected: config.selected,
multiple: config.multiple,
currentIndex: 0,
isLoading: false,
disabled: config.disabled ?? false,
limit: config.limit ?? 40,
init: function() {
if(this.selected == null ){
if(this.multiple)
this.selected = []
else
this.selected = ''
}
if(!this.data) this.data = {}
this.resetOptions()
this.$watch('search', ((values) => {
if (!this.open || !values) {
this.resetOptions()
return
}
this.options = Object.keys(this.data)
.filter((key) => this.data[key].toLowerCase().includes(values.toLowerCase()))
.slice(0, this.limit)
.reduce((options, key) => {
options[key] = this.data[key]
return options
}, {})
this.currentIndex=0
}))
},
resetOptions: function() {
this.options = Object.keys(this.data)
.slice(0,this.limit)
.reduce((options, key) => {
options[key] = this.data[key]
return options
}, {})
},
closeSelect: function() {
this.open = false
this.search = ''
},
toggleSelect: function() {
if(!this.disabled) {
if (this.open) return this.closeSelect()
this.open = true
}
},
deselectOption: function(index) {
if(this.multiple) {
this.selected.splice(index, 1)
}
else {
this.selected = ''
}
},
selectOption: function(value) {
if(!this.disabled) {
// If multiple push to the array, if not, keep that value and close menu
if(this.multiple){
// If it's not already in there
if (!this.selected.includes(value)) {
this.selected.push(value)
}
}
else {
this.selected=value
this.closeSelect()
}
}
},
increaseIndex: function() {
if(this.currentIndex == Object.keys(this.options).length)
this.currentIndex = 0
else
this.currentIndex++
},
decreaseIndex: function() {
if(this.currentIndex == 0)
this.currentIndex = Object.keys(this.options).length-1
else
this.currentIndex--;
},
}
}
</script>
@endonce